<?php

namespace App\Filament\Resources;

use App\Filament\Resources\AboutUsResource\Pages;
use App\Filament\Resources\AboutUsResource\RelationManagers;
use App\Models\AboutUs;
use Filament\Forms;
use Filament\Forms\Form;
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\SoftDeletingScope;
use Illuminate\Support\Facades\Gate;

class AboutUsResource extends Resource
{
    protected static ?string $model = AboutUs::class;

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

    protected static ?string $navigationLabel = 'About Us';
    protected static ?string $pluralLabel = 'About Us';
    protected static ?string $modelLabel = 'About Us';
    protected static ?string $navigationGroup = 'Website Content';
    protected static ?int $navigationSort = 1;

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

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

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

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

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

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

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

    public static function form(Forms\Form $form): Forms\Form
    {
        return $form
            ->schema([
                Forms\Components\Section::make('Basic Information')
                    ->schema([
                        Forms\Components\TextInput::make('main_title')
                            ->label('Main Title')
                            ->required()
                            ->maxLength(255)
                            ->placeholder('Enter the main title'),
                        Forms\Components\Textarea::make('short_description')
                            ->label('Short Description')
                            ->maxLength(500)
                            ->rows(3)
                            ->placeholder('Enter a brief description')
                            ->helperText('A concise summary of the about us section'),
                        Forms\Components\Textarea::make('about_description')
                            ->label('About Description')
                            ->required()
                            ->rows(6)
                            ->placeholder('Enter the detailed about description')
                            ->columnSpanFull(),
                    ])
                    ->columns(2),

                Forms\Components\Section::make('Additional Sections')
                    ->schema([
                        Forms\Components\Repeater::make('additional_sections')
                            ->schema([
                                Forms\Components\TextInput::make('title')
                                    ->label('Section Title')
                                    ->required()
                                    ->maxLength(255)
                                    ->placeholder('Enter section title'),
                                Forms\Components\Textarea::make('description')
                                    ->label('Section Description')
                                    ->required()
                                    ->rows(3)
                                    ->placeholder('Enter section description'),
                            ])
                            ->columns(2)
                            ->defaultItems(1)
                            ->minItems(1)
                            ->maxItems(10)
                            ->addActionLabel('Add Another Section')
                            ->reorderable()
                            ->collapsible()
                            ->collapsed()
                            ->columnSpanFull(),
                    ]),

                Forms\Components\Section::make('History Section')
                    ->schema([
                        Forms\Components\TextInput::make('history_title')
                            ->label('History Title')
                            ->maxLength(255)
                            ->placeholder('Enter history section title'),
                        Forms\Components\Textarea::make('history_description')
                            ->label('History Description')
                            ->rows(4)
                            ->placeholder('Enter history description')
                            ->columnSpanFull(),
                    ])
                    ->columns(2),

                Forms\Components\Section::make('Media Content')
                    ->schema([
                        Forms\Components\FileUpload::make('background_image')
                            ->label('Background Image')
                            ->image()
                            ->disk('public')
                            ->directory('about/backgrounds')
                            ->visibility('public')
                            ->imageEditor()
                            ->placeholder('Upload background image')
                            ->helperText('Recommended size: 1920x1080px'),
                        Forms\Components\FileUpload::make('history_photos')
                            ->label('History Photos')
                            ->multiple()
                            ->image()
                            ->disk('public')
                            ->directory('about/history')
                            ->visibility('public')
                            ->imageEditor()
                            ->reorderable()
                            ->placeholder('Upload history photos')
                            ->helperText('You can upload multiple photos for the history section')
                            ->columnSpanFull(),
                    ])
                    ->columns(2),
            ]);
    }

    public static function table(Tables\Table $table): Tables\Table
    {
        return $table
            ->columns([
                Tables\Columns\TextColumn::make('main_title')
                    ->label('Main Title')
                    ->limit(50)
                    ->sortable()
                    ->searchable(),
                Tables\Columns\TextColumn::make('short_description')
                    ->label('Short Description')
                    ->limit(80)
                    ->searchable()
                    ->toggleable(),
                Tables\Columns\TextColumn::make('additional_sections')
                    ->label('Additional Sections')
                    ->formatStateUsing(fn ($state) => is_array($state) ? count($state) : 0)
                    ->badge()
                    ->color('info'),
                Tables\Columns\TextColumn::make('history_photos')
                    ->label('History Photos')
                    ->formatStateUsing(fn ($state) => is_array($state) ? count($state) : 0)
                    ->badge()
                    ->color('success'),
                Tables\Columns\TextColumn::make('created_at')
                    ->dateTime()
                    ->sortable()
                    ->toggleable(isToggledHiddenByDefault: true),
                Tables\Columns\TextColumn::make('updated_at')
                    ->dateTime()
                    ->sortable()
                    ->toggleable(isToggledHiddenByDefault: true),
            ])
            ->filters([
                Tables\Filters\Filter::make('has_additional_sections')
                    ->label('Has Additional Sections')
                    ->query(fn ($query) => $query->whereJsonLength('additional_sections', '>', 0)),
                Tables\Filters\Filter::make('has_history_photos')
                    ->label('Has History Photos')
                    ->query(fn ($query) => $query->whereJsonLength('history_photos', '>', 0)),
            ])
            ->actions([
                Tables\Actions\EditAction::make()
                    ->visible(fn () => Gate::allows('edit aboutus')),
                Tables\Actions\DeleteAction::make()
                    ->visible(fn () => Gate::allows('delete aboutus')),
            ])
            ->bulkActions([
                Tables\Actions\BulkActionGroup::make([
                    Tables\Actions\DeleteBulkAction::make()
                        ->visible(fn () => Gate::allows('delete aboutus')),
                ]),
            ])
            ->defaultSort('created_at', 'desc');
    }

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

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