<?php

namespace App\Filament\Resources\PurchaseResource\Pages;

use App\Filament\Resources\PurchaseResource;
use App\Services\Audit\SuperAdminAuditLogger;
use Filament\Actions;
use Filament\Resources\Pages\ViewRecord;

class ViewPurchase extends ViewRecord
{
    protected static string $resource = PurchaseResource::class;

    protected function getHeaderActions(): array
    {
        return [
            Actions\EditAction::make()
                ->after(fn (array $data, $record) => app(SuperAdminAuditLogger::class)->recordModelMutation(
                    'updated',
                    $record,
                    SuperAdminAuditLogger::fieldsMetadata($data),
                    static::class
                )),
            Actions\DeleteAction::make()
                ->after(fn ($record) => app(SuperAdminAuditLogger::class)->recordModelMutation(
                    'deleted',
                    $record,
                    source: static::class
                )),
            Actions\Action::make('refund')
                ->label('Refund')
                ->color('danger')
                ->icon('heroicon-o-arrow-uturn-left')
                ->requiresConfirmation()
                ->visible(fn ($record): bool => ! $record->refunded)
                ->action(function ($record) {
                    $record->update(['refunded' => true]);
                    // TODO: Call Paddle API for actual refund

                    app(SuperAdminAuditLogger::class)->record(
                        'purchase.refunded',
                        $record,
                        SuperAdminAuditLogger::fieldsMetadata(['refunded']),
                        source: static::class
                    );
                }),
        ];
    }
}
