<?php

namespace App\Filament\Resources;

use App\Filament\Resources\UploadFileResource\Pages;
use App\Models\UploadFile;
use App\Service\DeepLService;
use Filament\Forms\Components\DatePicker;
use Filament\Forms\Components\FileUpload;
use Filament\Forms\Components\Group;
use Filament\Forms\Components\Radio;
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\TextColumn;
use Filament\Tables\Table;
use SolutionForest\FilamentTranslateField\Forms\Component\Translate;

class UploadFileResource extends Resource
{
    protected static ?string $model = UploadFile::class;

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

    protected static ?string $navigationGroup = '企業永續';

    protected static ?string $navigationLabel = '附檔上傳管理';

    protected static ?string $modelLabel = '附檔上傳管理';

    public static function form(Form $form): Form
    {
        return $form
            ->schema([
                //
                Section::make('')->schema([
                    Group::make()->schema([
                        Translate::make()->schema(fn(string $locale) => [
                            TextInput::make('title')->required($locale == 'zh_TW')->label("標題"),
                        ])
                            ->locales(["zh_TW", "en"])
                            ->actions([
                                app(DeepLService::class)->createTranslationAction("UploadFile", ["title"])
                            ])->columnSpanFull(),
                        Radio::make('type')->label('類型')->options([1 => '社會公益'])->default(1),
                        Group::make()->schema([
                            FileUpload::make('upload_link')->label('上傳檔案')->required()->directory('uploadFiles')->columnSpan(1),
                        ])->columnSpanFull(),
                        DatePicker::make('award_date')->label('獲得日期'),
                    ])->columns(4)->columnSpanFull(),
                ]),
            ]);
    }

    public static function table(Table $table): Table
    {
        return $table
            ->columns([
                // TextColumn::make("type_name")->label("類型"),
                TextColumn::make('title')->label('標題'),
                TextColumn::make('award_date')->label('獲得日期'),
            ])
            ->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\ListUploadFiles::route('/'),
            'create' => Pages\CreateUploadFile::route('/create'),
            'edit' => Pages\EditUploadFile::route('/{record}/edit'),
        ];
    }
}
