<?php

declare(strict_types=1);

namespace App\Filament\Resources\Admin;

use App\Enums\Filament\NavigationGroup;
use App\Filament\Actions\Repositories\Storage\Admin\Dump\ReconcileDumpAction;
use App\Filament\Actions\Storage\Admin\DownloadDumpAction;
use App\Filament\Actions\Storage\Admin\DumpContentAction;
use App\Filament\Actions\Storage\Admin\DumpDocumentAction;
use App\Filament\Actions\Storage\Admin\PruneDumpAction;
use App\Filament\Components\Columns\TextColumn;
use App\Filament\Components\Fields\TextInput;
use App\Filament\Components\Infolist\TextEntry;
use App\Filament\Components\Infolist\TimestampSection;
use App\Filament\Resources\Admin\Dump\Pages\ManageDumps;
use App\Filament\Resources\BaseResource;
use App\Models\Admin\Dump;
use Filament\Actions\ActionGroup;
use Filament\Infolists\Components\IconEntry;
use Filament\Schemas\Components\Section;
use Filament\Schemas\Schema;
use Filament\Support\Icons\Heroicon;
use Filament\Tables\Columns\IconColumn;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Model;

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

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

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

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

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

    public static function getRecordSlug(): string
    {
        return 'dumps';
    }

    public static function getRecordTitleAttribute(): string
    {
        return Dump::ATTRIBUTE_PATH;
    }

    public static function form(Schema $schema): Schema
    {
        return $schema
            ->components([
                TextInput::make(Dump::ATTRIBUTE_PATH)
                    ->label(__('filament.fields.dump.path'))
                    ->required()
                    ->maxLength(192)
                    ->hiddenOn(['create', 'edit']),
            ])
            ->columns(1);
    }

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

                TextColumn::make(Dump::ATTRIBUTE_PATH)
                    ->label(__('filament.fields.dump.path'))
                    ->searchable()
                    ->copyableWithMessage(),

                IconColumn::make(Dump::ATTRIBUTE_PUBLIC)
                    ->label(__('filament.fields.dump.public'))
                    ->boolean(),
            ]);
    }

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

                        TextEntry::make(Dump::ATTRIBUTE_PATH)
                            ->label(__('filament.fields.dump.path'))
                            ->copyableWithMessage(),

                        IconEntry::make(Dump::ATTRIBUTE_PUBLIC)
                            ->label(__('filament.fields.dump.public'))
                            ->boolean(),
                    ])
                    ->columns(3),

                TimestampSection::make(),
            ]);
    }

    /**
     * @return array<int, \Filament\Actions\Action|ActionGroup>
     */
    public static function getRecordActions(): array
    {
        return [
            DownloadDumpAction::make(),
        ];
    }

    /**
     * @return array<int, ActionGroup|\Filament\Actions\Action>
     */
    public static function getTableActions(): array
    {
        return [
            ActionGroup::make([
                DumpContentAction::make(),

                DumpDocumentAction::make(),

                PruneDumpAction::make(),

                ReconcileDumpAction::make(),
            ]),
        ];
    }

    public static function canCreate(): bool
    {
        return false;
    }

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