<?php

namespace App\Http\Controllers;

use App\Models\Dependant;
use App\Models\Member;
use App\Models\UserLog;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class DependantController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth');
    }

    public function index($id)
    {
        $member = Member::find($id);
        return view('dependants/new_dependant', compact('member'));
    }

    public function all($id)
    {
        $dependants = Dependant::where('member_id', $id)->get();
        $member = Member::find($id);
        return view('dependants/alldependants', compact('dependants', 'member'));
    }

    public function store(Request $request)
    {
        $this->validate($request, [
            'name' => 'required',
            'surname' => 'required',
            'gender' => 'required',
            'national_id' => 'required',
            'd_o_b' => 'required',
            'relationship' => 'required',
            'member_id' => 'required',
        ]);

        $date_birth = Carbon::parse($request->d_o_b);
        $now = Carbon::now();
        $age = $date_birth->diffInYears($now);

        if (($request->relationship != "Spouse") && ($age > 21)) {
            return redirect()->back()->with('warning', 'Dependant is above 21 years old!');
        } else {
            $dependant = new Dependant();
            $dependant->name = $request->name;
            $dependant->surname = $request->surname;
            $dependant->gender = $request->gender;
            $dependant->national_id = $request->national_id;
            $dependant->d_o_b = $request->d_o_b;
            $dependant->relationship = $request->relationship;
            $dependant->member_id = $request->member_id;
            $dependant->user_id = Auth::id();
            $dependant->save();

            $history = new UserLog();
            $history->user_id = Auth::id();
            $history->action = Auth::user()->name . " added a new dependent under " . $request->member_id . " on " . Carbon::now();
            $history->comment = "Dependent Registration";
            $history->save();

            return redirect('alldependants/' . $dependant->member_id)->with('success', 'Saved Successfully!');
        }
    }

    public function view($id)
    {
        $dependant = Dependant::find($id);
        return view('dependants.dependantView', compact('dependant'));
    }

    public function update(Request $request, $id)
    {
        $this->validate($request, [
            'name' => 'required',
            'surname' => 'required',
            'gender' => 'required',
            'national_id' => 'required',
            'd_o_b' => 'required',
            'relationship' => 'required'
        ]);
        $date_birth = Carbon::parse($request->d_o_b);
        $now = Carbon::now();
        $age = $date_birth->diffInYears($now);

        if (($request->relationship != "Spouse") && ($age > 80)) {
            return redirect()->back()->with('warning', 'Dependant is above 18 years old!');
        } else {
            $dependant = Dependant::find($id);
            $dependant->name = $request->name;
            $dependant->surname = $request->surname;
            $dependant->gender = $request->gender;
            $dependant->national_id = $request->national_id;
            $dependant->d_o_b = $request->d_o_b;
            $dependant->relationship = $request->relationship;
            $dependant->save();

            $history = new UserLog();
            $history->user_id = Auth::id();
            $history->action = Auth::user()->name . " updated dependent # " . $id . " on " . Carbon::now();
            $history->comment = "Dependant Update";
            $history->save();
            return redirect()->back()->with('success', 'Updated Successfully!');
        }
    }

    public function delete($id)
    {
        Dependant::find($id)->delete();

        $history = new UserLog();
        $history->user_id = Auth::id();
        $history->action = Auth::user()->name . " deleted dependant #" . $id . " on " . Carbon::now();
        $history->comment = "Beneficiary Deletion";
        $history->save();
        return redirect()->back()->with('success', 'Deleted Successfully!');
    }
}
