<?php

namespace App\Http\Controllers\Prospects;

use App\Http\Controllers\Controller;
use App\Models\Prospect;
use Illuminate\Http\Request;

class ProspectsController extends Controller
{
    /**View Dashboard with available Data**/
    public function index()
    {
        $year = date('Y');
        $jan = Prospect::whereBetween('created_at', [$year.'-01-01', $year.'-01-31'])->get();
        $feb = Prospect::whereBetween('created_at', [$year.'-02-01', $year.'-02-28'])->get();
        $mar = Prospect::whereBetween('created_at', [$year.'-03-01', $year.'-03-31'])->get();
        $apr = Prospect::whereBetween('created_at', [$year.'-04-01', $year.'-04-30'])->get();
        $may = Prospect::whereBetween('created_at', [$year.'-05-01', $year.'-05-31'])->get();
        $jun = Prospect::whereBetween('created_at', [$year.'-06-01', $year.'-06-30'])->get();
        $jul = Prospect::whereBetween('created_at', [$year.'-07-01', $year.'-07-31'])->get();
        $aug = Prospect::whereBetween('created_at', [$year.'-08-01', $year.'-08-31'])->get();
        $sep = Prospect::whereBetween('created_at', [$year.'-09-01', $year.'-09-30'])->get();
        $oct = Prospect::whereBetween('created_at', [$year.'-10-01', $year.'-10-31'])->get();
        $nov = Prospect::whereBetween('created_at', [$year.'-11-01', $year.'-11-30'])->get();
        $dec = Prospect::whereBetween('created_at', [$year.'-12-01', $year.'-12-31'])->get();

        return view('modules.prospects', compact('year','jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug', 'sep', 'oct', 'nov', 'dec'));
    }

    /**Create a Function that will be used to display the Prospect's Details**/
    public function showProspect($id)
    {
        $prospect = Prospect::findOrFail($id);

        return view('prospects.prospect', compact('prospect'));
    }


    /**Create a function that will be used to View a Page that will be used to create a new prospect**/
    public function showAddNewProspect()
    {
        return view('prospects.add-new-prospect');
    }

    /**Store Prospect Details into the Database**/
    public function storeAddNewProspect(Request $request){
        $title = $request['title'];
        $name = $request['name'];
        $phone = $request['phone'];
        $email = $request['email-address'];
        $location = $request['location'];
        $reminderDate = $request['reminder-date'];
        $notes = $request['narration'];

        $res = 'empty client';
        if(!empty($name)){
            /**Once Premium is more than 0, we can store Quote items in the Database**/
            $createProspect = Prospect::create(
                [
                    'title' => $title,
                    'name' => $name,
                    'phone' => $phone,
                    'email' => $email,
                    'location' => $location,
                    'reminder_date' => $reminderDate,
                    'details' => $notes,
                    'created_by' => auth()->user()->id,
                ]
            );
            $res = 'error';
            if($createProspect){
                /**Use the Number to Open Prospect File**/
                $res = $createProspect->id;
            }
        }
        return $res;
    }

    /**Create a FUnction that will be used to approve a Prospect into the System**/
    public function authorizeProspect($id)
    {
        $approveProspect = Prospect::where('id', $id)->update(
            [
                'confirmed_to_client' => 1,
                'confirmed_to_policy' => 1,
            ]
        );
        $res = 'error';
        if($approveProspect){
            $res = 'success';
        }
        return $res;
    }

    /**Create a function that will be used to Delete a Prospect**/
    public function deleteProspect($id)
    {
        $res = 'error';
        if(Prospect::findOrFail($id)->delete()){
            $res = 'success';
        }
        return $res;
    }


    /**Create a function that will be used to view a List of upcoming Prospects in the company**/
    public function upcomingProspectApprovals()
    {
        return view('prospects.upcoming-prospect-approvals');
    }

    /**Create a function that will be used to view a List of missed Prospects in the company**/
    public function missedProspectApprovals()
    {
        return view('prospects.missed-prospect-approvals');
    }


}
