<?php

namespace App\Components\Client;

use App\Models\Client\Client;
use App\Models\Client\PreviousName;
use App\Services\HistoryService;
use Livewire\Component;

class PreviousNameManager extends Component {
    public Client $client;

    public bool $show = false;

    public ?string $name_first = null;
    public ?string $name_middle = null;
    public ?string $name_last = null;
    public bool $editing = false;

    public ?PreviousName $previous_name = null;

    protected array $rules = [
        'name_first' => 'string|nullable|max:255',
        'name_middle' => 'string|nullable|max:255',
        'name_last' => 'string|nullable|max:255',
    ];

    public function showAdd(?PreviousName $name = null) {
        $this->show = true;
    }

    public function hideAdd() {
        $this->reset('name_first', 'name_middle', 'name_last');
        $this->show = false;
    }

    public function addName(HistoryService $historyService) {
        $data = $this->validate($this->rules);

        $this->client->previousNames()->create([
            'name_first' => $data['name_first'] ?: $this->client->name_first,
            'name_middle' => $data['name_middle'] ?: $this->client->name_middle,
            'name_last' => $data['name_last'] ?: $this->client->name_last,
        ]);

        $addedFirst  = $data['name_first']  ?: $this->client->name_first;
        $addedMiddle = $data['name_middle'] ?: $this->client->name_middle;
        $addedLast   = $data['name_last']   ?: $this->client->name_last;
        $addedFull   = trim("$addedFirst $addedMiddle $addedLast");
        $historyService->add($this->client, 'Previous name "' . $addedFull . '" added');

        $this->hideAdd();
    }

    public function loadEdit(int $id) {
        $name = $this->client->previousNames()->find($id);

        $this->name_first = $name->name_first;
        $this->name_middle = $name->name_middle;
        $this->name_last = $name->name_last;

        $this->editing = true;
        $this->previous_name = $name;

        $this->showAdd();
    }

    public function editName(HistoryService $historyService, int $id) {
        $data = $this->validate($this->rules);

        $name = $this->client->previousNames()->find($id);

        $old = $name->getAttributes();

        $name->update([
            'name_first' => $data['name_first'],
            'name_middle' => $data['name_middle'],
            'name_last' => $data['name_last'],
        ]);

        $new = $name->fresh()->getAttributes();

        $this->editing = false;
        $this->previous_name = null;

        $oldFull = trim("{$old['name_first']} {$old['name_middle']} {$old['name_last']}");
        $newFull = trim("{$new['name_first']} {$new['name_middle']} {$new['name_last']}");
        $historyService->add($this->client, 'Previous name changed from "' . $oldFull . '" to "' . $newFull . '"');

        $this->hideAdd();
    }

    public function deleteName(HistoryService $historyService, int $id) {
        $name = $this->client->previousNames()->find($id);
        $name->delete();
        $deletedFull = trim("{$name['name_first']} {$name['name_middle']} {$name['name_last']}");
        $historyService->add($this->client, 'Previous name "' . $deletedFull . '" deleted');

        $this->hideAdd();
    }

    public function render() {
        return view('components.client.previous-name-manager');
    }
}
