<?php

namespace App\Filament\Resources;

use App\Filament\Resources\ProfilePartResource\Pages;
use App\Models\ProfilePart;
use App\Service\DeepLService;
use Filament\Forms\Components\FileUpload;
use Filament\Forms\Components\Group;
use Filament\Forms\Components\Section;
use Filament\Forms\Components\Textarea;
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 ProfilePartResource extends Resource
{
    protected static ?string $model = ProfilePart::class;

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

    protected static ?string $navigationLabel = '宏匯簡介管理';

    protected static ?string $navigationGroup = '關於宏匯';

    protected static ?string $modelLabel = '宏匯簡介管理';

    protected static ?int $navigationSort = 2;

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

    public static function form(Form $form): Form
    {
        return $form
            ->schema([
                Section::make('')->schema([
                    Translate::make()->schema(fn (string $locale) => [
                        Textarea::make('title')->label('標題')->columnSpanFull(),
                        Textarea::make('content')->label('內文')->columnSpanFull(),
                    ])
                        ->locales(['zh_TW', 'en'])
                        ->actions([
                            app(DeepLService::class)->createTranslationAction('Main', ['title', 'content']),
                        ])
                        ->id('Main')->columnSpanFull(),
                    Group::make()->schema([
                        FileUpload::make('img_url')->label('圖片')
                            ->image()
                            ->optimize('webp')
                            ->maxImageWidth(1920)
                            ->directory('profile-parts')
                            ->acceptedFileTypes(['image/jpeg', 'image/jpg', 'image/png', 'image/webp'])->required()->imageEditor()
                            ->columnSpanFull(),
                        Translate::make()->schema(fn (string $locale) => [
                            TextInput::make('img_alt')->label('圖片註釋')->columnSpanFull(),
                        ])
                            ->locales(['zh_TW', 'en'])
                            ->actions([
                                app(DeepLService::class)->createTranslationAction('Img', ['img_alt']),
                            ])
                            ->id('Img')
                            ->columnSpanFull(),
                    ])->columnSpanFull(),
                ])->columns(3),
            ]);
    }

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

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

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