<?php

namespace App\Components\Settings;

use App\Models\Setting\EmailSender;
use Livewire\Component;

class EmailSenders extends Component {
    public bool $show      = false;
    public bool $editing   = false;
    public ?int $editingId = null;

    public string $display_name  = '';
    public string $email_address = '';
    public string $purpose       = 'general';
    public bool   $is_default    = false;
    public string $signature     = '';

    protected function rules(): array {
        return [
            'display_name'  => ['required', 'string', 'max:255'],
            'email_address' => ['required', 'email', 'max:255'],
            'purpose'       => ['required', 'string', 'in:' . implode(',', array_keys(EmailSender::$purposes))],
            'is_default'    => ['boolean'],
            'signature'     => ['nullable', 'string'],
        ];
    }

    public function showAdd(): void {
        $this->reset('display_name', 'email_address', 'purpose', 'is_default', 'signature', 'editing', 'editingId');
        $this->purpose = 'general';
        $this->show    = true;
    }

    public function hideAdd(): void {
        $this->reset('display_name', 'email_address', 'purpose', 'is_default', 'signature', 'editing', 'editingId');
        $this->show = false;
    }

    public function loadSender(int $id): void {
        $sender = EmailSender::findOrFail($id);

        $this->display_name  = $sender->display_name;
        $this->email_address = (string) $sender->email_address;
        $this->purpose       = $sender->purpose;
        $this->is_default    = $sender->is_default;
        $this->signature     = $sender->signature ?? '';
        $this->editing       = true;
        $this->editingId     = $id;
        $this->show          = true;
    }

    public function save(): void {
        $this->authorize('settings.edit');
        $data = $this->validate();

        if ($data['is_default']) {
            EmailSender::where('purpose', $data['purpose'])
                ->where('id', '!=', $this->editingId ?? 0)
                ->update(['is_default' => false]);
        }

        if ($this->editing && $this->editingId) {
            EmailSender::findOrFail($this->editingId)->update($data);
        } else {
            EmailSender::create($data);
        }

        $this->hideAdd();
    }

    public function makeDefault(int $id): void {
        $this->authorize('settings.edit');
        $sender = EmailSender::findOrFail($id);

        EmailSender::where('purpose', $sender->purpose)->update(['is_default' => false]);
        $sender->update(['is_default' => true]);
    }

    public function deleteSender(int $id): void {
        $this->authorize('settings.edit');
        EmailSender::findOrFail($id)->delete();
        $this->hideAdd();
    }

    public function render() {
        return view('components.settings.email-senders', [
            'senders'  => EmailSender::orderBy('purpose')->orderByDesc('is_default')->get(),
            'purposes' => EmailSender::$purposes,
        ]);
    }
}
