<?php

namespace App\Filament\Resources\UserDeviceResource\Pages;

use App\Filament\Resources\UserDeviceResource;
use App\Models\UserDevice;
use Filament\Actions;
use Filament\Notifications\Notification;
use Filament\Resources\Pages\ViewRecord;

class ViewUserDevice extends ViewRecord
{
    protected static string $resource = UserDeviceResource::class;

    protected function getHeaderActions(): array
    {
        return [
            Actions\Action::make('approve')
                ->label('Setujui')
                ->icon('heroicon-o-check-circle')
                ->color('success')
                ->visible(fn (): bool => $this->record->status === 'pending')
                ->requiresConfirmation()
                ->action(function () {
                    $this->record->approve(auth()->id());

                    Notification::make()
                        ->success()
                        ->title('Perangkat Disetujui')
                        ->body("Perangkat {$this->record->formatted_name} berhasil disetujui")
                        ->send();

                    $this->redirect($this->getResource()::getUrl('index'));
                }),

            Actions\Action::make('reject')
                ->label('Tolak')
                ->icon('heroicon-o-x-circle')
                ->color('danger')
                ->visible(fn (): bool => $this->record->status === 'pending')
                ->form([
                    \Filament\Forms\Components\Textarea::make('reason')
                        ->label('Alasan Penolakan')
                        ->placeholder('Masukkan alasan penolakan...')
                        ->required(),
                ])
                ->action(function (array $data) {
                    $this->record->reject(auth()->id(), $data['reason']);

                    Notification::make()
                        ->warning()
                        ->title('Perangkat Ditolak')
                        ->body("Perangkat {$this->record->formatted_name} berhasil ditolak")
                        ->send();

                    $this->redirect($this->getResource()::getUrl('index'));
                }),

            Actions\DeleteAction::make()
                ->visible(fn (): bool => $this->record->status !== 'approved'),
        ];
    }
}
