<?php

declare(strict_types=1);

namespace App\Support\Filament\ContentCrud;

use Filament\Forms\Components\DateTimePicker;
use Filament\Forms\Components\FileUpload;
use Filament\Forms\Components\Hidden;
use Filament\Forms\Components\RichEditor;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Components\Textarea;
use Filament\Forms\Components\Toggle;
use Filament\Forms\Components\BaseFileUpload;
use Filament\Schemas\Components\Grid;
use Filament\Schemas\Components\Section;
use Filament\Tables\Columns\ImageColumn;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Columns\ToggleColumn;
use Livewire\Features\SupportFileUploads\TemporaryUploadedFile;

final class ContentFields
{
    /**
     * Standard content form block used by our "content-type" resources.
     *
     * @return array<int, mixed>
     */
    public static function defaultFormComponents(string $section, string $uploadDirectory): array
    {
        return [
            Grid::make()
                ->schema([
                    Section::make()
                        ->schema([
                            Hidden::make('section')
                                ->default($section)
                                ->dehydrated(true),

                            TextInput::make('title')
                                ->label('標題')
                                ->required()
                                ->maxLength(200),

                            FileUpload::make('cover_image_path')
                                ->label('封面')
                                ->disk('public')
                                ->directory($uploadDirectory)
                                ->image()
                                ->saveUploadedFileUsing(static fn (BaseFileUpload $component, TemporaryUploadedFile $file): ?string =>
                                    \App\Support\Uploads\ImageVariants::store($component, $file)
                                )
                                ->deleteUploadedFileUsing(static function (BaseFileUpload $component, string $file): void {
                                    \App\Support\Uploads\ImageVariants::delete($component, $file);
                                })
                                ->maxSize(4096),

                            Textarea::make('summary')
                                ->label('摘要')
                                ->rows(3)
                                ->maxLength(500),

                            RichEditor::make('content')
                                ->label('內容')
                                ->columnSpanFull(),

                            DateTimePicker::make('published_at')
                                ->label('發布時間')
                                ->seconds(false),

                            Toggle::make('is_published')
                                ->label('發布中')
                                ->default(false),

                            Toggle::make('is_pinned')
                                ->label('首頁顯示?')
                                ->default(false),

                            TextInput::make('sort_order')
                                ->label('排序')
                                ->numeric()
                                ->default(0)
                                ->required(),
                        ])
                        ->columns([
                            'sm' => 2,
                            'lg' => 3,
                        ])
                        ->columnSpanFull(),
                ])
                ->columnSpanFull(),
        ];
    }

    /**
     * Standard table columns used by our "content-type" resources.
     *
     * @return array<int, mixed>
     */
    public static function defaultTableColumns(): array
    {
        return [
            ImageColumn::make('cover_image_path')
                ->label('封面')
                ->disk('public')
                ->getStateUsing(fn ($record): ?string =>
                    \App\Support\Uploads\ImageVariants::preferredPath($record?->cover_image_path ?? null, variant: 'medium', diskName: 'public')
                )
                ->square(),

            TextColumn::make('title')
                ->label('標題')
                ->searchable()
                ->sortable(),

            ToggleColumn::make('is_published')
                ->label('發布')
                ->sortable(),

            ToggleColumn::make('is_pinned')
                ->label('首頁顯示?')
                ->sortable(),

            TextColumn::make('published_at')
                ->label('發布時間')
                ->dateTime('Y-m-d H:i')
                ->sortable(),

            TextColumn::make('sort_order')
                ->label('排序')
                ->numeric()
                ->sortable(),

            TextColumn::make('updated_at')
                ->label('更新')
                ->dateTime('Y-m-d H:i')
                ->sortable(),
        ];
    }
}
