<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Enrollment;
use App\Models\Benificier;
use App\Models\Guardian;
use App\Models\Payment;
use App\Models\plan;
use App\Models\Subscriber;
use App\Models\Client;
use App\Models\EnrollmentHistory;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\Auth;

class EnrollmentController extends Controller
{
    /**
     * All Enrollments
     */
    public function viewEnrollment(Request $request)
    {
        if (Auth::user()->role == 'admin') {
            $enrollments = Enrollment::with('client', 'payment', 'subscriber', 'plan', 'guardian', 'benificier', 'manager')->paginate(15);
            return view('admin.enrollment.index', compact('enrollments'));
        } else {
            $enrollments = Enrollment::with('client', 'payment', 'subscriber', 'plan', 'guardian', 'benificier', 'manager')->where('branch_manager_id', Auth::user()->id)->paginate(15);
            return view('admin.enrollment.index', compact('enrollments'));
        }
    }
    /**
     * Approved Enrollments
     */
    public function approvedEnrollment(Request $request)
    {
        if (Auth::user()->role == 'admin') {
            $enrollments = Enrollment::with('client', 'payment', 'subscriber', 'plan', 'guardian', 'benificier', 'manager')->where('is_admin_review', 'approved')->paginate(15);
            return view('admin.enrollment.approved', compact('enrollments'));
        } else {
            $enrollments = Enrollment::with('client', 'payment', 'subscriber', 'plan', 'guardian', 'benificier', 'manager')->where('branch_manager_id', Auth::user()->id)->where('is_admin_review', 'approved')->paginate(15);
            return view('admin.enrollment.approved', compact('enrollments'));
        }
    }
    /**
     * Pending Enrollments
     */
    public function pendingEnrollment(Request $request)
    {
        if (Auth::user()->role == 'admin') {
            $enrollments = Enrollment::with('client', 'payment', 'subscriber', 'plan', 'guardian', 'benificier', 'manager')->where('is_admin_review', 'no')->paginate(15);
            return view('admin.enrollment.pending', compact('enrollments'));
        } else {
            $enrollments = Enrollment::with('client', 'payment', 'subscriber', 'plan', 'guardian', 'benificier', 'manager')->where('branch_manager_id', Auth::user()->id)->where('is_admin_review', 'no')->paginate(15);
            return view('admin.enrollment.pending', compact('enrollments'));
        }
    }

    /**
     * Enrollment status change
     * @param mixed entollment id
     */
    public function changeStatus(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'id' => 'required',
        ]);

        if ($validator->passes()) {
            $reviewPerson = Auth::user()->role == 'admin' ? 'is_admin_review' : 'is_branch_review';
            Enrollment::where('id', $request->id)->update([$reviewPerson => $request->status, "note" => $request->note]);
           
            $reviewBy = Auth::user()->role == 'admin' ? 'Company Admin' : 'Branch Manger';
            EnrollmentHistory::create([
                'en_id' => $request->id,
                'note' => $request->note,
                'status' => $request->status,
                'reviewedBy' => $reviewBy
            ]);

            return redirect()->back()->with("success", "Enrollment Status update successfully.");
        }

        return redirect()->back()->with("error", "Something Went Wrong.");
    }

    /**
     * Get Enrollment History
     */
    public function getHistory(Request $request, $id)
    {
       $enHistory = EnrollmentHistory::where('en_id', $id)->get();
       return view('admin.enrollment.history', compact('enHistory'));
    }

    /**
     * Delete Enrollment from all tables
     */
    public function deleteEnrollment(Request $request, $id)
    {
        Enrollment::whereId($id)->delete();
        Guardian::where('en_id', $id)->delete();
        Client::where('en_id', $id)->delete();
        Subscriber::where('en_id', $id)->delete();
        Payment::where('en_id', $id)->delete();
        Benificier::where('en_id', $id)->delete();
        Plan::where('en_id', $id)->delete();

        return redirect()->back();
    }

}