<?php

namespace Modules\Booking\Filament\Resources\Bookings\Actions;

use Filament\Actions\Action;
use Filament\Forms\Components\Textarea;
use Filament\Notifications\Notification;
use Filament\Support\Icons\Heroicon;
use Modules\Booking\Actions\SetRemarkAction;
use Modules\Booking\Models\Booking;

/**
 * Shared between BookingsTable (row action) and ViewBooking (header action)
 * so both surfaces stay in sync — one definition, not two.
 */
class SetRemarkTableAction
{
    public static function make(): Action
    {
        return Action::make('setRemark')
            ->label('Remark')
            ->icon(Heroicon::OutlinedPencilSquare)
            ->color('gray')
            ->visible(fn (): bool => auth()->user()?->can('manage_bookings') ?? false)
            ->schema([
                Textarea::make('remark')->maxLength(1000),
            ])
            ->fillForm(fn (Booking $record): array => [
                'remark' => $record->remark,
            ])
            ->action(function (array $data, Booking $record, SetRemarkAction $setRemarkAction) {
                $setRemarkAction->handle($record, $data['remark'] ?: null);

                Notification::make()
                    ->title('Remark saved')
                    ->success()
                    ->send();
            });
    }
}
