<?php

namespace App\Components\Solicitor;

use App\Models\Solicitor\Solicitor;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Livewire\Component;

class Edit extends Component {
    use AuthorizesRequests;
    public Solicitor $solicitor;

    public string $name                    = '';
    public string $domain                  = '';
    public string $company_number          = '';
    public string $sra_number              = '';
    public string $help_contact_first_name = '';
    public string $help_contact_last_name  = '';
    public string $signature               = '';
    public string $signature_text          = '';

    public function mount(Solicitor $solicitor): void {
        $this->authorize('solicitors.edit');
        $this->solicitor = $solicitor;

        $this->name                    = (string) $solicitor->name;
        $this->domain                  = (string) ($solicitor->domain ?? '');
        $this->company_number          = (string) ($solicitor->company_number ?? '');
        $this->sra_number              = (string) ($solicitor->sra_number ?? '');
        $this->help_contact_first_name = (string) ($solicitor->help_contact_first_name ?? '');
        $this->help_contact_last_name  = (string) ($solicitor->help_contact_last_name ?? '');
        $this->signature               = (string) ($solicitor->signature ?? '');
        $this->signature_text          = (string) ($solicitor->signature_text ?? '');
    }

    public function save() {
        $this->authorize('solicitors.edit');
        $validated = $this->validate();

        $this->solicitor->update([
            'name'                    => $validated['name'],
            'domain'                  => $validated['domain'] ?: null,
            'company_number'          => $validated['company_number'] ?: null,
            'sra_number'              => $validated['sra_number'] ?: null,
            'help_contact_first_name' => $validated['help_contact_first_name'] ?: null,
            'help_contact_last_name'  => $validated['help_contact_last_name'] ?: null,
            'signature'               => $validated['signature'] ?: null,
            'signature_text'          => $validated['signature_text'] ?: null,
        ]);

        session()->flash('success', 'Solicitor updated successfully.');

        return redirect()->route('solicitors.index');
    }

    public function render() {
        return view('components.solicitor.edit');
    }

    protected function rules(): array {
        return [
            'name'                    => ['required', 'string', 'max:255'],
            'domain'                  => ['nullable', 'string', 'max:255'],
            'company_number'          => ['nullable', 'string', 'max:50'],
            'sra_number'              => ['nullable', 'string', 'max:50'],
            'help_contact_first_name' => ['nullable', 'string', 'max:255'],
            'help_contact_last_name'  => ['nullable', 'string', 'max:255'],
            'signature'               => ['nullable', 'string'],
            'signature_text'          => ['nullable', 'string'],
        ];
    }

    protected function messages(): array {
        return [
            'name.required' => 'Please enter a solicitor name.',
        ];
    }
}
