<?php

namespace App\Livewire\Modals;

use App\Domains\Workspace\Actions\CreateWorkspace as CreateWorkspaceAction;
use Illuminate\View\View;
use LivewireUI\Modal\ModalComponent;

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

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

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

        $workspace = (new CreateWorkspaceAction)->handle(auth()->user(), ['name' => $this->name]);

        $this->dispatch('workspaceCreated', workspaceUlid: $workspace->ulid);
        $this->closeModal();
    }

    public static function modalMaxWidth(): string
    {
        return 'sm';
    }

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