<?php

namespace App\Filament\Resources\BusinessTripResource\Pages;

use App\Filament\Resources\BusinessTripResource;
use App\Models\BusinessTrip;
use Filament\Actions;
use Filament\Notifications\Notification;
use Filament\Resources\Pages\ViewRecord;

class ViewBusinessTrip extends ViewRecord
{
    protected static string $resource = BusinessTripResource::class;

    protected function getHeaderActions(): array
    {
        return [
            Actions\Action::make('approve')
                ->label('Approve')
                ->icon('heroicon-o-check-circle')
                ->color('success')
                ->requiresConfirmation()
                ->modalIcon('heroicon-o-check-circle')
                ->modalIconColor('success')
                ->modalHeading('Approve Perjalanan Dinas')
                ->modalDescription('Apakah Anda yakin ingin menyetujui perjalanan dinas ini?')
                ->visible(fn(): bool => $this->record->status === 'pending' && auth()->user()->hasPermissionTo('approve_business_trips'))
                ->action(function () {
                    $this->record->update([
                        'status' => 'approved',
                        'approved_by' => auth()->id(),
                        'approved_at' => now(),
                    ]);
                    Notification::make()
                        ->success()
                        ->title('Perjalanan Dinas Disetujui')
                        ->send();
                    $this->refreshFormData(['status']);
                }),
            Actions\Action::make('startTrip')
                ->label('Mulai Perjalanan')
                ->icon('heroicon-o-play')
                ->color('primary')
                ->requiresConfirmation()
                ->visible(fn(): bool => $this->record->status === 'approved')
                ->action(function () {
                    $this->record->update(['status' => 'ongoing']);
                    Notification::make()
                        ->success()
                        ->title('Perjalanan Dinas Dimulai')
                        ->send();
                    $this->refreshFormData(['status']);
                }),
            Actions\Action::make('completeTrip')
                ->label('Selesaikan')
                ->icon('heroicon-o-check-badge')
                ->color('success')
                ->requiresConfirmation()
                ->visible(fn(): bool => $this->record->status === 'ongoing')
                ->action(function () {
                    $this->record->update(['status' => 'completed']);
                    Notification::make()
                        ->success()
                        ->title('Perjalanan Dinas Selesai')
                        ->send();
                    $this->refreshFormData(['status']);
                }),
            Actions\EditAction::make()
                ->icon('heroicon-o-pencil-square'),
            Actions\DeleteAction::make()
                ->icon('heroicon-o-trash'),
        ];
    }
}
