<?php

namespace App\Filament\Resources\PlanFeatureResource\Pages;

use App\Filament\Resources\PlanFeatureResource;
use App\Models\PlanFeature;
use Filament\Actions;
use Filament\Notifications\Notification;
use Filament\Resources\Pages\EditRecord;

class EditPlanFeature extends EditRecord
{
    protected static string $resource = PlanFeatureResource::class;

    protected function getHeaderActions(): array
    {
        return [
            Actions\DeleteAction::make()
                ->before(function (PlanFeature $record) {
                    // Prevent deletion if feature is used in plans
                    if ($record->planFeatureLimits()->exists()) {
                        throw new \Exception('Cannot delete feature that is used in plans');
                    }
                }),
        ];
    }

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

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