<?php

namespace App\Filament\Resources;

use App\Filament\Resources\GalleryResource\Pages;
use App\Models\Gallery;
use Filament\Forms;
use Filament\Forms\Form;
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Tables\Table;
use Illuminate\Support\Facades\Gate;

class GalleryResource extends Resource
{
    protected static ?string $model = Gallery::class;

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

    protected static ?int $navigationSort = 4;

    protected static ?string $navigationGroup = 'Website Content';

    public static function form(Form $form): Form
    {
        return $form
            ->schema([
                Forms\Components\TextInput::make('title')
                    ->required()
                    ->maxLength(255)
                    ->label('Gallery Title'),
                Forms\Components\Textarea::make('text')
                    ->required()
                    ->maxLength(65535)
                    ->columnSpanFull()
                    ->label('Description'),
                Forms\Components\Repeater::make('images')
                    ->schema([
                        Forms\Components\FileUpload::make('image')
                            ->image()
                            ->required()
                            ->label('Image')
                            ->columnSpanFull(),
                    ])
                    ->defaultItems(1)
                    ->minItems(1)
                    ->maxItems(100)
                    ->addActionLabel('Add Image')
                    ->reorderable()
                    ->columnSpanFull()
                    ->label('Gallery Images'),
                Forms\Components\TextInput::make('display_count')
                    ->required()
                    ->numeric()
                    ->minValue(1)
                    ->maxValue(100)
                    ->default(6)
                    ->label('Number of Images to Display'),
            ]);
    }

    public static function table(Table $table): Table
    {
        return $table
            ->columns([
                Tables\Columns\TextColumn::make('title')
                    ->sortable()
                    ->searchable()
                    ->label('Gallery Title'),
                Tables\Columns\TextColumn::make('images')
                    ->formatStateUsing(function ($state) {
                        if (is_string($state)) {
                            $state = json_decode($state, true);
                        }
                        return is_array($state) ? count($state) : 0;
                    })
                    ->label('Number of Images'),
                Tables\Columns\TextColumn::make('display_count')
                    ->sortable()
                    ->searchable()
                    ->label('Display Count'),
                Tables\Columns\TextColumn::make('created_at')
                    ->dateTime()
                    ->sortable()
                    ->toggleable(isToggledHiddenByDefault: true),
                Tables\Columns\TextColumn::make('updated_at')
                    ->dateTime()
                    ->sortable()
                    ->toggleable(isToggledHiddenByDefault: true),
            ])
            ->filters([
                //
            ])
            ->actions([
                Tables\Actions\EditAction::make()->visible(fn () => Gate::allows('edit gallery')),
                Tables\Actions\DeleteAction::make()->visible(fn () => Gate::allows('delete gallery')),
            ])
            ->bulkActions([
                Tables\Actions\BulkActionGroup::make([
                    Tables\Actions\DeleteBulkAction::make()->visible(fn () => Gate::allows('delete gallery')),
                ]),
            ]);
    }

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

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

    public static function canViewAny(): bool
    {
        return Gate::allows('view gallery');
    }

    public static function canView($record): bool
    {
        return Gate::allows('view gallery');
    }

    public static function canCreate(): bool
    {
        return Gate::allows('create gallery');
    }

    public static function canEdit($record): bool
    {
        return Gate::allows('edit gallery');
    }

    public static function canDelete($record): bool
    {
        return Gate::allows('delete gallery');
    }

    public static function canRestore($record): bool
    {
        return Gate::allows('restore gallery');
    }

    public static function canForceDelete($record): bool
    {
        return Gate::allows('force delete gallery');
    }
}
