<?php

declare(strict_types=1);

namespace App\Filament\Resources\Admin;

use App\Enums\Filament\NavigationGroup;
use App\Filament\Components\Columns\BelongsToColumn;
use App\Filament\Components\Columns\TextColumn;
use App\Filament\Components\Fields\BelongsTo;
use App\Filament\Components\Fields\Select;
use App\Filament\Components\Infolist\BelongsToEntry;
use App\Filament\Components\Infolist\TextEntry;
use App\Filament\Components\Infolist\TimestampSection;
use App\Filament\Resources\Admin\FeaturedTheme\Pages\ListFeaturedThemes;
use App\Filament\Resources\Admin\FeaturedTheme\Pages\ViewFeaturedTheme;
use App\Filament\Resources\Auth\User as UserResource;
use App\Filament\Resources\BaseResource;
use App\Filament\Resources\Wiki\Anime\Theme\Entry as EntryResource;
use App\Filament\Resources\Wiki\Video as VideoResource;
use App\Models\Admin\FeaturedTheme as FeaturedThemeModel;
use App\Models\Wiki\Video;
use App\Pivots\Wiki\AnimeThemeEntryVideo;
use Closure;
use Filament\Forms\Components\DatePicker;
use Filament\Resources\RelationManagers\RelationGroup;
use Filament\Schemas\Components\Section;
use Filament\Schemas\Components\Utilities\Get;
use Filament\Schemas\Schema;
use Filament\Support\Icons\Heroicon;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Auth;
use Illuminate\Validation\Rule;

class FeaturedTheme extends BaseResource
{
    /**
     * The model the resource corresponds to.
     *
     * @var class-string<Model>|null
     */
    protected static ?string $model = FeaturedThemeModel::class;

    public static function getModelLabel(): string
    {
        return __('filament.resources.singularLabel.featured_theme');
    }

    public static function getPluralModelLabel(): string
    {
        return __('filament.resources.label.featured_themes');
    }

    public static function getNavigationGroup(): NavigationGroup
    {
        return NavigationGroup::ADMIN;
    }

    public static function getNavigationIcon(): Heroicon
    {
        return Heroicon::OutlinedCalendarDays;
    }

    public static function getRecordSlug(): string
    {
        return 'featured-themes';
    }

    public static function getEloquentQuery(): Builder
    {
        $query = parent::getEloquentQuery();

        // Necessary to prevent lazy loading when loading related resources
        return $query->with([
            FeaturedThemeModel::RELATION_USER,
            FeaturedThemeModel::RELATION_VIDEO,
            'animethemeentry.anime',
            'animethemeentry.animetheme.group',
        ]);
    }

    public static function form(Schema $schema): Schema
    {
        return $schema
            ->components([
                DatePicker::make(FeaturedThemeModel::ATTRIBUTE_START_AT)
                    ->label(__('filament.fields.featured_theme.start_at.name'))
                    ->helperText(__('filament.fields.featured_theme.start_at.help'))
                    ->native(false)
                    ->required()
                    ->before(FeaturedThemeModel::ATTRIBUTE_END_AT),

                DatePicker::make(FeaturedThemeModel::ATTRIBUTE_END_AT)
                    ->label(__('filament.fields.featured_theme.end_at.name'))
                    ->helperText(__('filament.fields.featured_theme.end_at.help'))
                    ->native(false)
                    ->required()
                    ->after(FeaturedThemeModel::ATTRIBUTE_START_AT),

                BelongsTo::make(FeaturedThemeModel::ATTRIBUTE_ENTRY)
                    ->resource(EntryResource::class)
                    ->live(true)
                    ->required()
                    ->rules([
                        fn (Get $get): Closure => (fn (): array => [
                            Rule::when(
                                filled($get(FeaturedThemeModel::RELATION_ENTRY)) && filled($get(FeaturedThemeModel::RELATION_VIDEO)),
                                [
                                    Rule::exists(AnimeThemeEntryVideo::class, AnimeThemeEntryVideo::ATTRIBUTE_ENTRY)
                                        ->where(AnimeThemeEntryVideo::ATTRIBUTE_VIDEO, $get(FeaturedThemeModel::RELATION_VIDEO)),
                                ]
                            )]),
                    ]),

                Select::make(FeaturedThemeModel::ATTRIBUTE_VIDEO)
                    ->label(__('filament.resources.singularLabel.video'))
                    ->relationship(FeaturedThemeModel::RELATION_VIDEO, Video::ATTRIBUTE_FILENAME)
                    ->required()
                    ->rules([
                        fn (Get $get): Closure => (fn (): array => [
                            Rule::when(
                                filled($get(FeaturedThemeModel::RELATION_ENTRY)) && filled($get(FeaturedThemeModel::RELATION_VIDEO)),
                                [
                                    Rule::exists(AnimeThemeEntryVideo::class, AnimeThemeEntryVideo::ATTRIBUTE_VIDEO)
                                        ->where(AnimeThemeEntryVideo::ATTRIBUTE_ENTRY, $get(FeaturedThemeModel::RELATION_ENTRY)),
                                ]
                            )]),
                    ])
                    ->options(fn (Get $get) => Video::query()
                        ->whereRelation(Video::RELATION_ANIMETHEMEENTRIES, function ($query) use ($get): void {
                            $query->whereKey($get(FeaturedThemeModel::ATTRIBUTE_ENTRY));
                        })
                        ->get()
                        ->mapWithKeys(fn (Video $video): array => [$video->getKey() => $video->getName()])
                        ->toArray()),

                BelongsTo::make(FeaturedThemeModel::ATTRIBUTE_USER)
                    ->resource(UserResource::class)
                    ->default(Auth::id()),
            ])
            ->columns(1);
    }

    public static function table(Table $table): Table
    {
        return parent::table($table)
            ->columns([
                TextColumn::make(FeaturedThemeModel::ATTRIBUTE_ID)
                    ->label(__('filament.fields.base.id')),

                TextColumn::make(FeaturedThemeModel::ATTRIBUTE_START_AT)
                    ->label(__('filament.fields.featured_theme.start_at.name'))
                    ->date(),

                TextColumn::make(FeaturedThemeModel::ATTRIBUTE_END_AT)
                    ->label(__('filament.fields.featured_theme.end_at.name'))
                    ->date(),

                BelongsToColumn::make(FeaturedThemeModel::RELATION_VIDEO, VideoResource::class),

                BelongsToColumn::make(FeaturedThemeModel::RELATION_ENTRY, EntryResource::class),

                BelongsToColumn::make(FeaturedThemeModel::RELATION_USER, UserResource::class),
            ]);
    }

    public static function infolist(Schema $schema): Schema
    {
        return $schema
            ->components([
                Section::make(static::getRecordTitle($schema->getRecord()))
                    ->schema([
                        TextEntry::make(FeaturedThemeModel::ATTRIBUTE_ID)
                            ->label(__('filament.fields.base.id')),

                        TextEntry::make(FeaturedThemeModel::ATTRIBUTE_START_AT)
                            ->label(__('filament.fields.featured_theme.start_at.name'))
                            ->date(),

                        TextEntry::make(FeaturedThemeModel::ATTRIBUTE_END_AT)
                            ->label(__('filament.fields.featured_theme.end_at.name'))
                            ->date(),

                        BelongsToEntry::make(FeaturedThemeModel::RELATION_VIDEO, VideoResource::class),

                        BelongsToEntry::make(FeaturedThemeModel::RELATION_ENTRY, EntryResource::class),

                        BelongsToEntry::make(FeaturedThemeModel::RELATION_USER, UserResource::class),
                    ])
                    ->columns(3),

                TimestampSection::make(),
            ]);
    }

    /**
     * @return array<int, RelationGroup|class-string<\Filament\Resources\RelationManagers\RelationManager>>
     */
    public static function getRelations(): array
    {
        return [
            RelationGroup::make(static::getModelLabel(), [
                ...parent::getBaseRelations(),
            ]),
        ];
    }

    /**
     * @return array<string, \Filament\Resources\Pages\PageRegistration>
     */
    public static function getPages(): array
    {
        return [
            'index' => ListFeaturedThemes::route('/'),
            'view' => ViewFeaturedTheme::route('/{record:featured_theme_id}'),
        ];
    }
}
