<?php

namespace App\Filament\Resources;

use App\Filament\Resources\LibraryResource\Pages;
use App\Models\Library;
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\Tables\Columns\TextColumn;
use Filament\Tables\Columns\ImageColumn;
use Illuminate\Support\Facades\Gate;

class LibraryResource extends Resource
{
    protected static ?string $model = Library::class;

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

    protected static ?string $navigationLabel = 'Library';

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

    protected static ?int $navigationSort = 1;

    public static function form(Form $form): Form
    {
        return $form
            ->schema([
                Forms\Components\Section::make('Basic Information')
                    ->schema([
                        Forms\Components\TextInput::make('name')
                            ->required()
                            ->maxLength(255)
                            ->placeholder('Enter library name'),
                        Forms\Components\Textarea::make('description')
                            ->required()
                            ->rows(4)
                            ->placeholder('Enter library description')
                            ->columnSpanFull(),
                        Forms\Components\FileUpload::make('background_image')
                            ->image()
                            ->directory('library')
                            ->visibility('public')
                            ->imageEditor()
                            ->columnSpanFull(),
                    ]),

                Forms\Components\Section::make('Library Details')
                    ->schema([
                        Forms\Components\TextInput::make('library_title')
                            ->required()
                            ->maxLength(255)
                            ->placeholder('Enter library title'),
                        Forms\Components\TextInput::make('online_resources_title')
                            ->required()
                            ->maxLength(255)
                            ->placeholder('Enter online resources title'),
                        Forms\Components\TextInput::make('resources_title')
                            ->required()
                            ->maxLength(255)
                            ->placeholder('Enter resources title'),
                        Forms\Components\Textarea::make('resources_description')
                            ->required()
                            ->rows(3)
                            ->placeholder('Enter resources description')
                            ->columnSpanFull(),
                    ]),

                Forms\Components\Section::make('Opening Hours')
                    ->schema([
                        Forms\Components\TextInput::make('opening_hours_title')
                            ->required()
                            ->maxLength(255)
                            ->placeholder('Enter opening hours title'),
                        Forms\Components\TextInput::make('library_opening_time')
                            ->required()
                            ->maxLength(255)
                            ->placeholder('e.g., 8:00 AM'),
                        Forms\Components\TextInput::make('library_closing_time')
                            ->required()
                            ->maxLength(255)
                            ->placeholder('e.g., 6:00 PM'),
                        Forms\Components\Textarea::make('library_time_description')
                            ->required()
                            ->rows(3)
                            ->placeholder('Enter library time description')
                            ->columnSpanFull(),
                    ]),

                Forms\Components\Section::make('Contact Information')
                    ->schema([
                        Forms\Components\TextInput::make('contact_title')
                            ->required()
                            ->maxLength(255)
                            ->placeholder('Enter contact title'),
                        Forms\Components\TextInput::make('tittle_circulation')
                            ->required()
                            ->maxLength(255)
                            ->placeholder('Enter circulation title'),
                        Forms\Components\TextInput::make('desk_contact_number1')
                            ->required()
                            ->tel()
                            ->maxLength(255)
                            ->placeholder('Enter first contact number'),
                        Forms\Components\TextInput::make('desk_contact_number2')
                            ->tel()
                            ->maxLength(255)
                            ->placeholder('Enter second contact number (optional)'),
                        Forms\Components\TextInput::make('desk_email')
                            ->required()
                            ->email()
                            ->maxLength(255)
                            ->placeholder('Enter desk email'),
                        Forms\Components\TextInput::make('desk_title_2')
                            ->required()
                            ->maxLength(255)
                            ->placeholder('Enter second desk title'),
                    ]),

                Forms\Components\Section::make('Renewal Information')
                    ->schema([
                        Forms\Components\Textarea::make('renewal_text_1')
                            ->required()
                            ->rows(3)
                            ->placeholder('Enter first renewal text')
                            ->columnSpanFull(),
                        Forms\Components\Textarea::make('renewal_text_2')
                            ->required()
                            ->rows(3)
                            ->placeholder('Enter second renewal text')
                            ->columnSpanFull(),
                        Forms\Components\Textarea::make('renewal_text_3')
                            ->required()
                            ->rows(3)
                            ->placeholder('Enter third renewal text')
                            ->columnSpanFull(),
                    ]),

                Forms\Components\Section::make('Library Policy')
                    ->schema([
                        Forms\Components\TextInput::make('library_policy_title')
                            ->required()
                            ->maxLength(255)
                            ->placeholder('Enter library policy title'),
                        Forms\Components\Textarea::make('library_policy_description')
                            ->required()
                            ->rows(4)
                            ->placeholder('Enter library policy description')
                            ->columnSpanFull(),
                    ]),
            ]);
    }

    public static function table(Table $table): Table
    {
        return $table
            ->columns([
                Tables\Columns\TextColumn::make('name')
                    ->searchable()
                    ->sortable(),
                Tables\Columns\TextColumn::make('library_title')
                    ->searchable()
                    ->sortable(),
                Tables\Columns\ImageColumn::make('background_image')
                    ->circular(),
                Tables\Columns\TextColumn::make('library_opening_time')
                    ->label('Opening Time')
                    ->sortable(),
                Tables\Columns\TextColumn::make('library_closing_time')
                    ->label('Closing Time')
                    ->sortable(),
                Tables\Columns\TextColumn::make('desk_email')
                    ->searchable(),
                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 library')),
                Tables\Actions\ViewAction::make()->visible(fn () => Gate::allows('view library')),
                Tables\Actions\DeleteAction::make()->visible(fn () => Gate::allows('delete library')),
            ])
            ->bulkActions([
                Tables\Actions\BulkActionGroup::make([
                    Tables\Actions\DeleteBulkAction::make()->visible(fn () => Gate::allows('delete library')),
                ]),
            ]);
    }

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

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

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

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

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

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

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

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

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