<?php

namespace App\Filament\Resources\RetentionOverrideResource\Pages;

use App\Filament\Resources\Pages\AuditedEditRecord;
use App\Filament\Resources\RetentionOverrideResource;
use App\Models\RetentionOverride;
use App\Services\Audit\SuperAdminAuditLogger;
use Filament\Actions;
use Filament\Facades\Filament;

class EditRetentionOverride extends AuditedEditRecord
{
    protected static string $resource = RetentionOverrideResource::class;

    protected function getHeaderActions(): array
    {
        return [
            Actions\Action::make('release')
                ->label(__('admin.retention_overrides.actions.release'))
                ->icon('heroicon-o-check-circle')
                ->color('success')
                ->requiresConfirmation()
                ->visible(fn () => $this->record instanceof RetentionOverride && $this->record->released_at === null)
                ->action(function (): void {
                    if (! ($this->record instanceof RetentionOverride) || $this->record->released_at !== null) {
                        return;
                    }

                    $this->record->forceFill([
                        'released_at' => now(),
                        'released_by_id' => Filament::auth()->id(),
                    ])->save();

                    app(SuperAdminAuditLogger::class)->recordModelMutation(
                        'updated',
                        $this->record,
                        SuperAdminAuditLogger::fieldsMetadata(['released_at', 'released_by_id']),
                        static::class
                    );
                }),
        ];
    }
}
