<?php

namespace App\Filament\Resources\Resellers\Pages;

use App\Filament\Resources\Resellers\ResellerResource;
use App\Filament\Resources\Resellers\Widgets\ResellerStatsWidget;
use Filament\Actions\DeleteAction;
use Filament\Resources\Pages\EditRecord;

class EditReseller extends EditRecord
{
    protected static string $resource = ResellerResource::class;

    protected function getHeaderActions(): array
    {
        return [
            \Filament\Actions\Action::make('approve')
                ->label('Approve')
                ->color('success')
                ->icon('heroicon-o-check-circle')
                ->requiresConfirmation()
                ->visible(fn () => $this->record->reseller_status !== 'active')
                ->action(function () {
                    $this->record->update([
                        'reseller_status' => 'active',
                        'role' => 'reseller',
                        'reseller_rejection_reason' => null,
                    ]);
                    $this->refreshFormData(['reseller_status', 'reseller_rejection_reason']);
                    \Filament\Notifications\Notification::make()
                        ->title('Reseller Approved')
                        ->body("{$this->record->name} has been approved.")
                        ->success()
                        ->send();
                }),
            \Filament\Actions\Action::make('reject')
                ->label('Reject')
                ->color('danger')
                ->icon('heroicon-o-x-circle')
                ->form([
                    \Filament\Forms\Components\Textarea::make('reason')
                        ->label('Rejection Reason')
                        ->required()
                        ->placeholder('e.g. NID image is blurry, please re-upload clear photos.'),
                ])
                ->visible(fn () => in_array($this->record->reseller_status, ['pending', 'under_review']))
                ->action(function (array $data) {
                    $this->record->update([
                        'reseller_status' => 'rejected',
                        'reseller_rejection_reason' => $data['reason'],
                    ]);
                    $this->refreshFormData(['reseller_status', 'reseller_rejection_reason']);
                    \Filament\Notifications\Notification::make()
                        ->title('Reseller Rejected')
                        ->body("{$this->record->name}'s registration has been rejected.")
                        ->danger()
                        ->send();
                }),
            DeleteAction::make(),
        ];
    }

    protected function getHeaderWidgets(): array
    {
        return [
            ResellerStatsWidget::class,
        ];
    }
}
