<?php

namespace App\Filament\Resources\Consultants\Pages;

use App\Filament\Resources\Consultants\ConsultantResource;
use App\Mail\ConsultantInviteMail;
use Filament\Actions\Action;
use Filament\Actions\DeleteAction;
use Filament\Notifications\Notification;
use Filament\Resources\Pages\EditRecord;
use Illuminate\Support\Facades\Mail;

class EditConsultant extends EditRecord
{
    protected static string $resource = ConsultantResource::class;

    protected function getHeaderActions(): array
    {
        return [
            Action::make('approve')
                ->label('Approve consultant')
                ->icon('heroicon-o-check-badge')
                ->color('success')
                ->visible(fn () => $this->record->status === 'submitted')
                ->requiresConfirmation()
                ->action(function () {
                    $this->record->update(['status' => 'approved', 'approved_at' => now()]);
                    $this->refreshFormData(['status']);
                    Notification::make()->title('Consultant approved')->success()->send();
                }),
            Action::make('resend_invite')
                ->label('Resend invite email')
                ->icon('heroicon-o-envelope')
                ->visible(fn () => in_array($this->record->status, ['invited', 'onboarding'], true))
                ->action(function () {
                    try {
                        Mail::to($this->record->email)->send(new ConsultantInviteMail($this->record));
                        Notification::make()->title('Invite re-sent')->success()->send();
                    } catch (\Throwable) {
                        Notification::make()->title('Email could not be sent, copy the link instead')->warning()->send();
                    }
                }),
            DeleteAction::make(),
        ];
    }
}
