<?php

namespace App\Filament\Resources;

use App\Filament\Clusters\RareAdmin\RareAdminCluster;
use App\Filament\Resources\LegalPageResource\Pages;
use App\Models\LegalPage;
use App\Services\Audit\SuperAdminAuditLogger;
use BackedEnum;
use Filament\Actions;
use Filament\Forms\Components\DatePicker;
use Filament\Forms\Components\KeyValue;
use Filament\Forms\Components\MarkdownEditor;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Components\Toggle;
use Filament\Resources\Resource;
use Filament\Schemas\Components\Tabs as SchemaTabs;
use Filament\Schemas\Components\Tabs\Tab as SchemaTab;
use Filament\Schemas\Schema;
use Filament\Tables;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Collection;
use UnitEnum;

class LegalPageResource extends Resource
{
    protected static ?string $model = LegalPage::class;

    protected static ?string $cluster = RareAdminCluster::class;

    protected static BackedEnum|string|null $navigationIcon = 'heroicon-o-scale';

    protected static UnitEnum|string|null $navigationGroup = null;

    public static function getNavigationGroup(): UnitEnum|string|null
    {
        return __('admin.nav.content');
    }

    protected static ?int $navigationSort = 40;

    public static function form(Schema $form): Schema
    {
        return $form->schema([
            TextInput::make('slug')
                ->label(__('admin.legal_pages.fields.slug'))
                ->required()
                ->unique(ignoreRecord: true)
                ->maxLength(255),
            KeyValue::make('title')
                ->label(__('admin.legal_pages.fields.title_localized'))
                ->keyLabel(__('admin.common.locale'))
                ->valueLabel(__('admin.common.value'))
                ->default(['de' => '', 'en' => ''])
                ->required(),
            SchemaTabs::make('content_tabs')
                ->label(__('admin.legal_pages.fields.content_localization'))
                ->tabs([
                    SchemaTab::make(__('admin.common.german'))
                        ->icon('heroicon-o-language')
                        ->schema([
                            MarkdownEditor::make('body_markdown.de')
                                ->label(__('admin.legal_pages.fields.content_de'))
                                ->required()
                                ->columnSpanFull(),
                        ]),
                    SchemaTab::make(__('admin.common.english'))
                        ->icon('heroicon-o-language')
                        ->schema([
                            MarkdownEditor::make('body_markdown.en')
                                ->label(__('admin.legal_pages.fields.content_en'))
                                ->required()
                                ->columnSpanFull(),
                        ]),
                ])
                ->columnSpanFull(),
            Toggle::make('is_published')
                ->label(__('admin.legal_pages.fields.is_published'))
                ->default(true),
            DatePicker::make('effective_from')
                ->label(__('admin.legal_pages.fields.effective_from'))
                ->required(),
            TextInput::make('version')
                ->label(__('admin.legal_pages.fields.version'))
                ->required()
                ->default('1.0')
                ->maxLength(20),
        ])->columns(2);
    }

    public static function table(Table $table): Table
    {
        return $table
            ->columns([
                Tables\Columns\TextColumn::make('id')->sortable(),
                Tables\Columns\TextColumn::make('slug')->badge(),
                Tables\Columns\TextColumn::make('version')->badge(),
                Tables\Columns\IconColumn::make('is_published')->boolean(),
                Tables\Columns\TextColumn::make('effective_from')->date(),
                Tables\Columns\TextColumn::make('created_at')->since(),
            ])
            ->filters([])
            ->actions([
                Actions\EditAction::make()
                    ->after(fn (array $data, LegalPage $record) => app(SuperAdminAuditLogger::class)->recordModelMutation(
                        'updated',
                        $record,
                        SuperAdminAuditLogger::fieldsMetadata($data),
                        static::class
                    )),
            ])
            ->bulkActions([
                Actions\DeleteBulkAction::make()
                    ->after(function (Collection $records): void {
                        $logger = app(SuperAdminAuditLogger::class);

                        foreach ($records as $record) {
                            $logger->recordModelMutation(
                                'deleted',
                                $record,
                                source: static::class
                            );
                        }
                    }),
            ]);
    }

    public static function getPages(): array
    {
        return [
            'index' => Pages\ListLegalPages::route('/'),
            'view' => Pages\ViewLegalPage::route('/{record}'),
            'edit' => Pages\EditLegalPage::route('/{record}/edit'),
        ];
    }
}
