<?php

namespace App\Http\Controllers\CRM;

use App\Enums\AddressType;
use App\Models\CRM\Address;
use App\Models\CRM\Company;
use App\Models\CRM\Individual;
use App\Models\INSURANCE\Insurer;
use App\Http\Controllers\Controller;
use Illuminate\Http\RedirectResponse;
use App\Http\Requests\CRM\StoreAddressRequest;
use App\Http\Requests\CRM\UpdateAddressRequest;

class AddressController extends Controller
{
    public function storeResidence(StoreAddressRequest $request, Individual $individual): RedirectResponse
    {
        // Only 'residence' fields will be filled out by the form
        $data = $request->validated()['residence'] ?? [];

        // Force the type to Residence
        $data['type'] = AddressType::Residence->value;
        $data['status'] = 'active';

        $individual->addresses()->create($data);

        return redirect()
            ->route('crm.individuals.edit', $individual)
            ->with('status', __('Residence address added successfully!'));
    }

    public function storeResidenceOsiguranje(StoreAddressRequest $request, Insurer $insurer): RedirectResponse
    {
        // Only 'residence' fields will be filled out by the form
        $data = $request->validated()['residence'] ?? [];

        // Force the type to Residence
        $data['type'] = AddressType::Residence->value;
        $data['status'] = 'active';

        $insurer->addresses()->create($data);

        return redirect()
            ->route('osiguranje.dashboard')
            ->with('status', __('Residence address added successfully!'));
    }

    public function storeResidenceComapny(StoreAddressRequest $request, Company $company): RedirectResponse
    {
        // Only 'residence' fields will be filled out by the form
        $data = $request->validated()['residence'] ?? [];

        // Force the type to Residence
        $data['type'] = AddressType::Residence->value;
        $data['status'] = 'active';

        $company->addresses()->create($data);

        return redirect()
            ->route('crm.dashboard')
            ->with('status', __('Residence address added successfully!'));
    }

    public function storeMailing(StoreAddressRequest $request, Individual $individual): RedirectResponse
    {
        // Only 'mailing' fields will be filled out by the form
        $data = $request->validated()['mailing'] ?? [];

        // Force the type to Mailing
        $data['type'] = AddressType::Mailing->value;
        $data['status'] = 'active';

        $individual->addresses()->create($data);

        return redirect()
            ->route('crm.individuals.edit', $individual)
            ->with('status', __('Mailing address added successfully!'));
    }

    public function update(UpdateAddressRequest $request, Individual $individual, Address $address): RedirectResponse
    {
        // You may wish to authorize that $address belongs to $individual, for security:
        // abort_unless($address->addressable_id === $individual->id && $address->addressable_type === $individual->getMorphClass(), 403);

        // The request may contain `residence` or `mailing`, or both
        // But we know from `type` hidden input which address to update:
        $type = $request->input('type');

        if ($type === 'Residence') {
            $address->update($request->validated()['residence'] ?? []);
        }

        if ($type === 'Mailing') {
            $address->update($request->validated()['mailing'] ?? []);
        }

        return redirect()->route('crm.individuals.edit', $individual)
            ->with('status', __('Address updated successfully!'));
    }

    public function updateOsiguranje(UpdateAddressRequest $request, insurer $insurer, Address $address): RedirectResponse
    {
        // You may wish to authorize that $address belongs to $insurer, for security:
        // abort_unless($address->addressable_id === $insurer->id && $address->addressable_type === $insurer->getMorphClass(), 403);

        // The request may contain `residence` or `mailing`, or both
        // But we know from `type` hidden input which address to update:
        $type = $request->input('type');

        if ($type === 'Residence') {
            $address->update($request->validated()['residence'] ?? []);
        }

        if ($type === 'Mailing') {
            $address->update($request->validated()['mailing'] ?? []);
        }

        return redirect()->route('osiguranje.insurers.edit', $insurer)
            ->with('status', __('Address updated successfully!'));
    }

    public function updateAddressComapny(UpdateAddressRequest $request, Company $company, Address $address): RedirectResponse
    {
        // You may wish to authorize that $address belongs to $insurer, for security:
        // abort_unless($address->addressable_id === $insurer->id && $address->addressable_type === $insurer->getMorphClass(), 403);

        // The request may contain `residence` or `mailing`, or both
        // But we know from `type` hidden input which address to update:
        $type = $request->input('type');

        if ($type === 'Residence') {
            $address->update($request->validated()['residence'] ?? []);
        }

        if ($type === 'Mailing') {
            $address->update($request->validated()['mailing'] ?? []);
        }

        return redirect()->route('crm.companies.edit', $company)
            ->with('status', __('Address updated successfully!'));
    }
}
