<?php

namespace App\Http\Controllers;

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

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

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

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

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

        $ben = Beneficiary::where('member_id', $request->member_id)->first();
        if (!$ben) {
        $beneficiary = new Beneficiary();
        $beneficiary->name = $request->name;
        $beneficiary->surname = $request->surname;
        $beneficiary->gender = $request->gender;
        $beneficiary->cell_number = $request->cell_number;
        $beneficiary->national_id = $request->national_id;
        $beneficiary->date_of_birth = $request->date_of_birth;
        $beneficiary->relationship = $request->relationship;
        $beneficiary->member_id = $request->member_id;
        $beneficiary->user_id = Auth::id();

        $history = new UserLog();
        $history->user_id = Auth::id();
        $history->action = Auth::user()->name . " added a new beneficiary under " . $request->member_id . " on " . Carbon::now();
        $history->comment = "Beneficiary Registration";
        $history->save();
        
        $beneficiary->save();
        return redirect('allbeneficiaries/'.$beneficiary->member_id)->with('success', 'Saved Successfully!');
        }
        else {
            return redirect('allbeneficiaries/'.$request->member_id)->with('warning', 'Already have a Beneficiary!');   
        }
    }

    public function view($id)
    {
        $beneficiary = Beneficiary::find($id);
        return view('beneficiaries.beneficiaryView', compact('beneficiary'));
    }

    public function update(Request $request, $id)
    {
        $this->validate($request, [
            'name' => 'required',
            'surname' => 'required',
            'gender' => 'required',
            'cell_number' => 'required',
            'national_id' => 'required',
            'date_of_birth' => 'required',
            'relationship' => 'required' 
        ]);
        $beneficiary = Beneficiary::find($id);
        $beneficiary->name = $request->name;
        $beneficiary->surname = $request->surname;
        $beneficiary->gender = $request->gender;
        $beneficiary->cell_number = $request->cell_number;
        $beneficiary->national_id = $request->national_id;
        $beneficiary->date_of_birth = $request->date_of_birth;
        $beneficiary->relationship = $request->relationship;        
        $beneficiary->save();

        $history = new UserLog();
        $history->user_id = Auth::id();
        $history->action = Auth::user()->name . " added a new beneficiary # " . $id . " on " . Carbon::now();
        $history->comment = "Beneficiary Update.";
        $history->save();

        return redirect()->back()->with('success', 'Updated Successfully!');
    }

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

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