<?php

namespace App\Filament\Resources\Transactions\Transactions\Pages;

use App\Filament\Resources\Transactions\Transactions\TransactionResource;
use Filament\Actions\DeleteAction;
use Filament\Resources\Pages\EditRecord;

class EditTransaction extends EditRecord
{
    protected static string $resource = TransactionResource::class;

    protected function getHeaderActions(): array
    {
        return [
            DeleteAction::make()
                ->visible(fn() => auth()->user()?->hasRole(['super_admin', 'Admin'])),
        ];
    }

    // LOGIKA PENGUNCIAN TOMBOL SIMPAN
    // LOGIKA PENGUNCIAN TOMBOL SIMPAN
    protected function getFormActions(): array
    {
        $record = $this->getRecord();
        $user = auth()->user();

        // TAMBAHKAN 'batal_no_show' DI DALAM ARRAY INI
        $finalSurveyStatuses = ['deal', 'batal_tenant', 'batal_owner', 'batal_gagal_bayar', 'batal_no_show'];

        $isLocked = ($record->payment_status === 'failed') ||
            in_array($record->survey_status, $finalSurveyStatuses);

        if ($user->hasRole('Owner') && $isLocked) {
            return [
                $this->getCancelFormAction()->label('Kembali'),
            ];
        }

        return parent::getFormActions();
    }

    protected function getRedirectUrl(): string
    {
        return $this->getResource()::getUrl('index');
    }
}
