<?php

namespace App\Filament\Resources\PlanTierResource\Pages;

use App\Filament\Resources\PlanTierResource;
use App\Models\PlanTier;
use Filament\Actions;
use Filament\Notifications\Notification;
use Filament\Resources\Pages\EditRecord;

class EditPlanTier extends EditRecord
{
    protected static string $resource = PlanTierResource::class;

    protected function getHeaderActions(): array
    {
        return [
            Actions\DeleteAction::make()
                ->before(function (PlanTier $record) {
                    // Prevent deletion if tier has child tiers or plans
                    if ($record->childTiers()->exists()) {
                        throw new \Exception('Cannot delete tier that has child tiers');
                    }
                    if ($record->plans()->exists()) {
                        throw new \Exception('Cannot delete tier that has plans assigned');
                    }
                }),
        ];
    }

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

    protected function getSavedNotification(): ?Notification
    {
        return Notification::make()
            ->success()
            ->title('Tier updated')
            ->body('Plan tier updated successfully');
    }
}
