<?php

namespace App\Livewire\Modals;

use Illuminate\View\View;
use LivewireUI\Modal\ModalComponent;

class CreateApiToken extends ModalComponent
{
    public string $name = '';

    public ?string $plainTextToken = null;

    /** @return array<string, string> */
    protected function rules(): array
    {
        return [
            'name' => 'required|string|max:100',
        ];
    }

    public function create(): void
    {
        $this->validate();

        $token = auth()->user()->createToken($this->name);
        $this->plainTextToken = $token->plainTextToken;

        $this->dispatch('apiTokenCreated');
    }

    public static function closeModalOnClickAway(): bool
    {
        return false;
    }

    public function render(): View
    {
        return view('livewire.modals.create-api-token');
    }
}
