<?php

namespace App\Http\Controllers\Front;

use App\Http\Controllers\Controller;
use App\Models\NotificationSetting;
use App\Models\ParentApproval;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use Kreait\Firebase\Contract\Database;

class ParentController extends Controller
{
    public $database;
    public function __construct(Database $database)
    {
        $this->database = $database;
    }
    public function show($token)
    {
        $parent = ParentApproval::where('token', $token)->firstOrFail();

        return view('front.parent_approval', compact('token','parent'));
    }

    public function update(Request $request, $id)
    {
        $parent = ParentApproval::where('id', $id)->where('status',0)->firstOrFail();

        $validator = Validator::make($request->all(), [
            'status' => 'required|in:approved,rejected',
        ]);

        if ($validator->fails()) {
            return response()->json(['errors' => $validator->errors()], 422);
        }

        if ($request->status === 'rejected') {
            $parent->update([
                'status' => 2,
                'rejected_at' => now(),
            ]);

            $parent->user->parent_approval = 2;
            $parent->user->save();

            $description = "Your Luqia Profile Has Been Rejected.";
            $ntype = 'profile_rejected';
            $notification_id = time();
            $nTitle = "Profile Rejected";

            send_notification_to_user(
                $this->database,
                $parent->user,
                ['userId' => "", "imageBlur" => "0"],
                NotificationSetting::ALERT,
                "profile_rejected",
                time(),
                trans('notification.profile_rejected_title', [], 'en') . "\n" . trans('notification.profile_rejected_title', [], 'ar'),
                trans('notification.profile_rejected_message', [], 'en') . "\n" . trans('notification.profile_rejected_message', [], 'ar')
                
            );
            
            return response()->json(['message' => 'Parent rejected ❌ تم رفض ولي الأمر', 'status' => 'faild']);
        }

        if( $request->status === 'approved') {
            $parent->update([
                'status' => 1,
                'approved_at' => now(),
            ]);

            $parent->user->parent_approval = 1;
            $parent->user->save();

            send_notification_to_user(
                $this->database,
                $parent->user,
                ['userId' => "", "imageBlur" => "0"],
                NotificationSetting::ALERT,
                "profile_approved",
                time(),
                trans('notification.profile_approved_title', [], 'en') . "\n" . trans('notification.profile_approved_title', [], 'ar'),
                trans('notification.profile_approved_message', [], 'en') . "\n" . trans('notification.profile_approved_message', [], 'ar')
                
            );

            return response()->json(['message' => 'Parent approved successfully ✅ تمت موافقة ولي الأمر بنجاح', 'status' => 'success']);
        }
    }

    public function success()
    {
        return view('front.parent_approval_success');
    }

    public function failed()
    {
        return view('front.parent_approval_reject');
    }
}