<?php

namespace App\Http\Controllers\CRM;

use Illuminate\Http\Request;
use App\Models\CRM\Individual;
use Illuminate\Contracts\View\View;
use App\Http\Controllers\Controller;
use Illuminate\Http\RedirectResponse;
use App\Http\Requests\CRM\StoreIndividualRequest;
use App\Http\Requests\CRM\UpdateIndividualRequest;

class IndividualController extends Controller
{

    public function index(): View
    {
        $individuals = Individual::latest()->take(10)->get();
        return view('backend.crm.individuals.index', compact('individuals'));
    }

    public function create(): View
    {
        $readonly = null;
        return view('backend.crm.individuals.dashboard', [
            'individual' => null,
            'readonly' => $readonly
        ]);
    }

    public function store(StoreIndividualRequest $request): RedirectResponse
    {
        $data = $request->validated();
        $individual = Individual::create($data);

        return redirect()->route('crm.individuals.edit', $individual)
            ->with('status', 'Success');
    }


    public function edit(Individual $individual): View
    {
        $individual->load(['lastActiveIdCard', 'lastActivePassport', 'entity']);
        $readonly = null;
        return view('backend.crm.individuals.dashboard', compact('individual', 'readonly'));
    }

    public function update(UpdateIndividualRequest $request, Individual $individual): RedirectResponse
    {
        $data = $request->validated();
        $individual->update($data);
        return redirect()->route('crm.individuals.edit', $individual)
            ->with('status', 'Success');
    }

    // public function destroy(Individual $individual): RedirectResponse
    // {
    //     $individual->delete();
    //     return redirect()->route('crm.individuals.index')
    //         ->with('status', 'Success');
    // }



    // public function show(Individual $individual): View
    // {
    //     return view('backend.crm.individuals.show', compact('individual'));
    // }









    // public function create(Individual $individual = null): View
    // {
    //     $readonly = null;
    //     return view('backend.crm.individuals.dashboard', compact('individual', 'readonly'));
    // }

    // public function store(StoreIndividualRequest $request): View
    // {
    //     $individual = Individual::create($request->validated());
    //     $readonly = null;
    //     return view('backend.crm.individuals.dashboard', compact('individual', 'readonly'));
    // }


    // /**
    //  * Handle an incoming request to update an existing individual.
    //  *
    //  * @param  \App\Http\Requests\CRM\UpdateIndividualRequest  $request
    //  * @return \Illuminate\Contracts\View\View
    //  */

    // public function update(UpdateIndividualRequest $request): View
    // {
    //     $individual = Individual::create($request->validated());
    //     $readonly = null;
    //     return view('backend.crm.individuals.dashboard', compact('individual', 'readonly'));
    // }
}
