<?php

namespace App\Filament\Resources;

use App\Filament\Resources\BadgeResource\Pages;
use App\Models\Badge;
use App\Service\DeepLService;
use Filament\Forms\Components\FileUpload;
use Filament\Forms\Components\Group;
use Filament\Forms\Components\Radio;
use Filament\Forms\Components\Section;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Form;
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Tables\Columns\ImageColumn;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Table;
use SolutionForest\FilamentTranslateField\Forms\Component\Translate;

class BadgeResource extends Resource
{
    protected static ?string $model = Badge::class;

    protected static ?string $navigationIcon = 'heroicon-o-check-badge';

    protected static ?string $navigationGroup = '其他設定';

    protected static ?string $navigationLabel = '認證標章管理';

    protected static ?string $modelLabel = '認證標章管理';

    // 預設排序
    protected static function booted()
    {
        static::addGlobalScope('ordered', function ($query) {
            $query->orderBy('badges.order')->orderBy('badges.id');
        });
    }

    public static function form(Form $form): Form
    {
        return $form
            ->schema([
                Section::make('')->schema([
                    Group::make()->schema([
                        Group::make()->schema([
                            Translate::make()->schema(fn (string $locale) => [
                                TextInput::make('title')->required($locale == 'zh_TW')
                                    ->unique(
                                        table: 'badges',
                                        column: 'title->' . $locale,
                                        ignoreRecord: true
                                    )
                                    ->maxLength(40)->label('徽章標題'),
                            ])
                                ->locales(['zh_TW', 'en'])
                                ->actions([
                                    app(DeepLService::class)->createTranslationAction('Main', ['title']),
                                ])->columnSpan(1),
                        ])->columns(2)->columnSpanFull(),
                        Group::make()->schema([
                            FileUpload::make('img_url')->label('圖片')->directory('badge')->columnSpan(1)
                                ->image()
                                ->optimize('webp')
                                ->maxImageWidth(1920)
                                ->acceptedFileTypes(['image/jpeg', 'image/jpg', 'image/png', 'image/webp'])->required()->imageEditor(),
                        ])->columnSpanFull(),
                    ])->columns(4)->columnSpanFull(),
                    Group::make()->schema([
                        Radio::make('visible')->label('顯示/不顯示')->options([1 => '顯示', 0 => '不顯示'])->inline()->default(1)
                            ->columnSpan(2),
                    ])->columns(4)->columnSpanFull(),
                ]),
            ]);
    }

    public static function table(Table $table): Table
    {
        return $table
            ->columns([
                //
                TextColumn::make('title')->label('徽章標題'),
                ImageColumn::make('img_url')->label('圖片'),
            ])
            ->filters([
                //
            ])
            ->actions([
                Tables\Actions\EditAction::make(),
            ])
            ->bulkActions([
                Tables\Actions\BulkActionGroup::make([
                    Tables\Actions\DeleteBulkAction::make(),
                ]),
            ]);
    }

    public static function getRelations(): array
    {
        return [
            //
        ];
    }

    public static function getPages(): array
    {
        return [
            'index' => Pages\ListBadges::route('/'),
            'create' => Pages\CreateBadge::route('/create'),
            'edit' => Pages\EditBadge::route('/{record}/edit'),
        ];
    }
}
