<?php

declare(strict_types=1);

namespace Plugins\Larablog\Membership\Filament\Resources\MembershipPlanResource\Pages;

use Filament\Actions\DeleteAction;
use Filament\Notifications\Notification;
use Filament\Resources\Pages\EditRecord;
use Plugins\Larablog\Membership\Filament\Resources\MembershipPlanResource;
use Plugins\Larablog\Membership\Models\MembershipPlan;

class EditMembershipPlan extends EditRecord
{
    protected static string $resource = MembershipPlanResource::class;

    protected function getHeaderActions(): array
    {
        return [
            DeleteAction::make()
                ->before(function (DeleteAction $action): void {
                    /** @var MembershipPlan $record */
                    $record = $this->getRecord();

                    if ($record->articleGates()->exists()) {
                        Notification::make()
                            ->title(__('admin.messages.membership_plan_in_use_articles'))
                            ->danger()
                            ->send();
                        $action->cancel();
                    }

                    if ($record->hasEntitlements()) {
                        Notification::make()
                            ->title(__('admin.messages.membership_plan_has_entitlements'))
                            ->danger()
                            ->send();
                        $action->cancel();
                    }
                }),
        ];
    }
}
