<?php

namespace App\Filament\Resources;

use App\Filament\Resources\HomeResource\Pages;
use App\Models\Home;
use Filament\Forms;
use Filament\Forms\Form;
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Tables\Table;
use Filament\Forms\Components\Section;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Components\Textarea;
use Filament\Forms\Components\FileUpload;
use Filament\Forms\Components\Repeater;
use Illuminate\Support\Facades\Gate;

class HomeResource extends Resource
{
    protected static ?string $model = Home::class;

    protected static ?string $navigationIcon = 'heroicon-o-home';
    protected static ?string $navigationLabel = 'Home Page';
    protected static ?string $pluralLabel = 'Home Pages';
    protected static ?string $modelLabel = 'Home Page';
    protected static ?string $navigationGroup = 'Website Content';
    protected static ?int $navigationSort = 1;

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

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

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

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

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

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

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

    public static function form(Form $form): Form
    {
        return $form->schema([
            Section::make('Main Content')
                ->schema([
                    TextInput::make('main_title')
                        ->label('Main Title')
                        ->required()
                        ->maxLength(255)
                        ->placeholder('Enter the main title for the home page'),
                    Textarea::make('main_description')
                        ->label('Main Description')
                        ->required()
                        ->rows(4)
                        ->placeholder('Enter the main description')
                        ->columnSpanFull(),
                    FileUpload::make('top_image')
                        ->label('Top Image')
                        ->image()
                        ->disk('public')
                        ->directory('home/top')
                        ->visibility('public')
                        ->imageEditor()
                        ->placeholder('Upload top image')
                        ->helperText('Recommended size: 1920x1080px'),
                ])
                ->columns(2),

            Section::make('Statistics Section')
                ->schema([
                    Repeater::make('count_items')
                        ->label('Count Items')
                        ->schema([
                            TextInput::make('name')
                                ->label('Name')
                                ->required()
                                ->maxLength(255)
                                ->placeholder('Enter statistic name'),
                            TextInput::make('count')
                                ->label('Count')
                                ->numeric()
                                ->required()
                                ->minValue(0)
                                ->placeholder('Enter count number'),
                            FileUpload::make('icon_image')
                                ->label('Icon Image')
                                ->image()
                                ->disk('public')
                                ->directory('home/count_icons')
                                ->visibility('public')
                                ->imageEditor()
                                ->required()
                                ->placeholder('Upload icon image')
                                ->helperText('Recommended size: 64x64px'),
                        ])
                        ->addActionLabel('Add Count Item')
                        ->minItems(1)
                        ->maxItems(10)
                        ->columns(3)
                        ->collapsible()
                        ->collapsed(),
                ]),

            Section::make('Display Counts')
                ->schema([
                    TextInput::make('gallery_count')
                        ->label('Gallery Count')
                        ->numeric()
                        ->minValue(0)
                        ->placeholder('Enter gallery count')
                        ->helperText('Number of gallery items to display'),
                    TextInput::make('news_count')
                        ->label('News Count')
                        ->numeric()
                        ->minValue(0)
                        ->placeholder('Enter news count')
                        ->helperText('Number of news items to display'),
                    TextInput::make('program_count')
                        ->label('Program Count')
                        ->numeric()
                        ->minValue(0)
                        ->placeholder('Enter program count')
                        ->helperText('Number of programs to display'),
                ])
                ->columns(3),

            Section::make('Bottom Section')
                ->schema([
                    FileUpload::make('bottom_image')
                        ->label('Bottom Image')
                        ->image()
                        ->disk('public')
                        ->directory('home/bottom')
                        ->visibility('public')
                        ->imageEditor()
                        ->placeholder('Upload bottom image')
                        ->helperText('Recommended size: 1920x1080px'),
                    Textarea::make('bottom_text')
                        ->label('Bottom Text')
                        ->rows(3)
                        ->placeholder('Enter bottom text')
                        ->columnSpanFull(),
                ])
                ->columns(2),
        ]);
    }

    public static function table(Table $table): Table
    {
        return $table
            ->columns([
                Tables\Columns\TextColumn::make('main_title')
                    ->label('Main Title')
                    ->sortable()
                    ->searchable()
                    ->limit(50),
                Tables\Columns\TextColumn::make('main_description')
                    ->label('Description')
                    ->limit(80)
                    ->searchable()
                    ->toggleable(),
                Tables\Columns\TextColumn::make('count_items')
                    ->label('Count Items')
                    ->formatStateUsing(fn ($state) => is_array($state) ? count($state) : 0)
                    ->badge()
                    ->color('info'),
                Tables\Columns\TextColumn::make('gallery_count')
                    ->label('Gallery Count')
                    ->sortable()
                    ->badge()
                    ->color('success'),
                Tables\Columns\TextColumn::make('program_count')
                    ->label('Program Count')
                    ->sortable()
                    ->badge()
                    ->color('warning'),
                Tables\Columns\TextColumn::make('news_count')
                    ->label('News Count')
                    ->sortable()
                    ->badge()
                    ->color('danger'),
                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_count_items')
                    ->label('Has Count Items')
                    ->query(fn ($query) => $query->whereJsonLength('count_items', '>', 0)),
            ])
            ->actions([
                Tables\Actions\EditAction::make()
                    ->visible(fn () => Gate::allows('edit home')),
                Tables\Actions\DeleteAction::make()
                    ->visible(fn () => Gate::allows('delete home')),
            ])
            ->bulkActions([
                Tables\Actions\BulkActionGroup::make([
                    Tables\Actions\DeleteBulkAction::make()
                        ->visible(fn () => Gate::allows('delete home')),
                ]),
            ])
            ->defaultSort('created_at', 'desc');
    }

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

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