<?php
namespace App\Filament\Resources\Styles;

use BackedEnum;
use App\Filament\Resources\Styles\Pages;
use App\Models\Style;
use Filament\Schemas;
use Filament\Schemas\Schema;
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Tables\Table;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Components\Textarea;
use Filament\Forms\Components\FileUpload;
use Filament\Forms\Components\Select;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Columns\ImageColumn;
use Filament\Forms\Components\Toggle;
use Filament\Tables\Columns\IconColumn;
use Filament\Tables\Filters\SelectFilter;
use Filament\Schemas\Components\Grid;
use Filament\Schemas\Components\Tabs;
use Filament\Schemas\Components\Tabs\Tab;
use Filament\Actions\Action;
use Filament\Actions\BulkAction;
use Filament\Actions\BulkActionGroup;
use Filament\Actions\CreateAction;
use Filament\Actions\DeleteBulkAction;
use Filament\Actions\EditAction;

class StyleResource extends Resource
{
    protected static ?string $model = Style::class;

    protected static  BackedEnum|string|null $navigationIcon = 'heroicon-o-rectangle-stack';

    public static function form(Schema $schema): Schema
    {
        return $schema
            ->columns(1)
            ->components([
                Tabs::make('Style Details')
                    ->tabs([
                        Tab::make('General')
                            ->components([
                                Grid::make(2)
                                    ->columnSpanFull()
                                    ->components([
                                        TextInput::make('title')
                                            ->required()
                                            ->maxLength(255),
                                        Toggle::make('enabled')
                                            ->default(true),
                                    ]),
                                Grid::make(2)
                                    ->columnSpanFull()
                                    ->components([
                                        Textarea::make('prompt')
                                            ->required()
                                            ->rows(5),
                                        Textarea::make('description')
                                            ->required()
                                            ->rows(5),
                                    ]),
                                Select::make('ai_model_id')
                                    ->relationship('aiModel', 'name')
                                    ->required(),
                                FileUpload::make('preview_image')
                                    ->disk('public')
                                    ->directory('style_previews')
                                    ->image()
                                    ->imageEditor()
                                    ->required(),
                            ]),
                        Tab::make('Details')
                            ->components([
                                TextInput::make('sort_order')
                                    ->numeric()
                                    ->default(0),
                                Textarea::make('parameters')
                                    ->nullable()
                                    ->rows(15),
                            ]),
                    ]),
            ]);
    }

    public static function table(Table $table): Table
    {
        return $table
            ->defaultSort('sort_order')
            ->columns([
                TextColumn::make('title')->searchable()->sortable(),
                IconColumn::make('enabled')
                    ->boolean(),
                TextColumn::make('aiModel.name')->searchable()->sortable(),
                ImageColumn::make('preview_image')->disk('public'),
                TextColumn::make('sort_order')->sortable(),
            ])
            ->filters([
                SelectFilter::make('ai_model')
                    ->relationship('aiModel', 'name'),
            ])
            ->deferFilters(false)
            ->actions([
                EditAction::make(),
                Action::make('duplicate')
                    ->label('Duplicate')
                    ->icon('heroicon-o-document-duplicate')
                    ->action(function (\App\Models\Style $record) {
                        $newStyle = $record->replicate();
                        $newStyle->title = $record->title . ' (Kopie)';
                        $newStyle->save();
                        return redirect()->to(\App\Filament\Resources\Styles\StyleResource::getUrl('edit', ['record' => $newStyle->id]));
                    }),
            ])
            ->bulkActions([
                BulkActionGroup::make([
                    DeleteBulkAction::make(),
                    BulkAction::make('enable')
                        ->label('Enable Selected')
                        ->icon('heroicon-o-check-circle')
                        ->action(function (\Illuminate\Support\Collection $records) {
                            $records->each->update(['enabled' => true]);
                        }),
                    BulkAction::make('disable')
                        ->label('Disable Selected')
                        ->icon('heroicon-o-x-circle')
                        ->action(function (\Illuminate\Support\Collection $records) {
                            $records->each->update(['enabled' => false]);
                        }),
                ]),
            ])
            ->emptyStateActions([
                CreateAction::make(),
            ]);
    }
    
    public static function getRelations(): array
    {
        return [
            //
        ];
    }
    
    public static function getPages(): array
    {
        return [
            'index' => Pages\ListStyles::route('/'),
            'create' => Pages\CreateStyle::route('/create'),
            'edit' => Pages\EditStyle::route('/{record}/edit'),
        ];
    }    
}
