<?php

namespace App\Livewire\Applicant;

use App\Enums\ApplicationStep;
use App\Livewire\Applicant\Concerns\WizardStep;
use App\Models\Application;
use App\Support\Uganda;
use Livewire\Component;

class ContactInformationForm extends Component
{
    use WizardStep;

    public string $phone = '';
    public string $alternate_phone = '';
    public string $address = '';
    public string $district = '';
    public string $country = 'Uganda';
    public string $postal_address = '';

    public string $guardian_name = '';
    public string $guardian_relationship = '';
    public string $guardian_phone = '';
    public string $guardian_email = '';

    protected function step(): ApplicationStep
    {
        return ApplicationStep::Contact;
    }

    public function mount(Application $application): void
    {
        $this->mountWizardStep($application);

        $applicant = $application->applicant;

        foreach ([
            'phone', 'alternate_phone', 'address', 'district', 'postal_address',
            'guardian_name', 'guardian_relationship', 'guardian_phone', 'guardian_email',
        ] as $field) {
            $this->{$field} = $applicant->{$field} ?? '';
        }

        $this->phone = $this->phone ?: ($applicant->user->phone ?? '');
        $this->country = $applicant->country ?: 'Uganda';
    }

    protected function rules(): array
    {
        $phone = ['string', 'max:30', 'regex:/^[0-9+\-\s()]{7,30}$/'];

        return [
            'phone' => array_merge(['required'], $phone),
            'alternate_phone' => array_merge(['nullable'], $phone),
            'address' => ['required', 'string', 'max:255'],
            'district' => ['required', 'string', 'max:100'],
            'country' => ['required', 'string', 'max:100'],
            'postal_address' => ['nullable', 'string', 'max:120'],
            'guardian_name' => ['required', 'string', 'max:150'],
            'guardian_relationship' => ['required', 'string', 'max:60'],
            'guardian_phone' => array_merge(['required'], $phone),
            'guardian_email' => ['nullable', 'email:rfc', 'max:190'],
        ];
    }

    protected function messages(): array
    {
        return [
            'phone.regex' => 'Enter a valid phone number, for example 0771 234 567.',
            'alternate_phone.regex' => 'Enter a valid phone number, for example 0771 234 567.',
            'guardian_phone.regex' => 'Enter a valid phone number, for example 0771 234 567.',
            'guardian_name.required' => 'Please give us a next of kin we can contact.',
        ];
    }

    public function updated(string $field): void
    {
        $this->validateOnly($field);
    }

    public function save(): void
    {
        $data = $this->validate();

        $this->application->applicant->update(array_map(
            fn ($value) => $value === '' ? null : $value,
            $data,
        ));

        $this->completeStep();
    }

    public function render()
    {
        return view('livewire.applicant.contact-information-form', [
            'districts' => Uganda::districts(),
            'relationships' => ['Parent', 'Guardian', 'Sibling', 'Spouse', 'Relative', 'Sponsor', 'Other'],
        ]);
    }
}
