<?php

declare(strict_types=1);

namespace App\Filament\Pages;

use App\Models\Plugin;
use BackedEnum;
use Filament\Pages\Page;

abstract class PluginSkeletonPage extends Page
{
    protected static ?int $navigationSort = 100;

    protected string $view = 'filament.pages.plugin-skeleton';

    abstract protected static function pluginName(): string;

    abstract protected static function navKey(): string;

    public static function getNavigationGroup(): ?string
    {
        return __('admin.groups.plugins');
    }

    public static function getNavigationLabel(): string
    {
        return __('admin.nav.'.static::navKey());
    }

    public function getTitle(): string
    {
        return static::translatedTitle();
    }

    public function getHeading(): string
    {
        return static::translatedTitle();
    }

    protected static function translatedTitle(): string
    {
        $key = 'admin.plugins.'.static::pluginName().'.title';

        return __($key) !== $key ? __($key) : __('admin.nav.'.static::navKey());
    }

    protected static function translatedDescription(): string
    {
        $key = 'admin.plugins.'.static::pluginName().'.description';

        return __($key) !== $key ? __($key) : '';
    }

    public static function shouldRegisterNavigation(): bool
    {
        return Plugin::query()
            ->where('name', static::pluginName())
            ->where('enabled', true)
            ->exists();
    }

    public function getViewData(): array
    {
        return [
            'pluginName' => static::pluginName(),
            'pluginTitle' => static::translatedTitle(),
            'pluginDescription' => static::translatedDescription(),
            'enabled' => Plugin::query()
                ->where('name', static::pluginName())
                ->where('enabled', true)
                ->exists(),
        ];
    }
}
