<?php

namespace App\Filament\Resources;

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

class FacilityResource extends Resource
{
    protected static ?string $model = Facility::class;

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

    protected static ?int $navigationSort = 3;

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

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

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

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

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

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

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

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

    public static function form(Form $form): Form
    {
        return $form
            ->schema([
                Forms\Components\TextInput::make('main_title')
                    ->required()
                    ->maxLength(255)
                    ->label('Main Title'),
                Forms\Components\FileUpload::make('image')
                    ->image()
                    ->required()
                    ->label('Facility Image'),
                Forms\Components\Textarea::make('text')
                    ->required()
                    ->maxLength(65535)
                    ->columnSpanFull()
                    ->label('Description'),
                Forms\Components\TextInput::make('director_title')
                    ->required()
                    ->maxLength(255)
                    ->label('Director Title'),
                Forms\Components\TextInput::make('director_name')
                    ->required()
                    ->maxLength(255)
                    ->label('Director Name'),
                Forms\Components\Textarea::make('director_desc')
                    ->required()
                    ->maxLength(65535)
                    ->columnSpanFull()
                    ->label('Director Description'),
                Forms\Components\TextInput::make('director_position')
                    ->required()
                    ->maxLength(255)
                    ->label('Director Position'),
                Forms\Components\TextInput::make('director_college')
                    ->required()
                    ->maxLength(255)
                    ->label('Director College'),
                Forms\Components\FileUpload::make('director_image')
                    ->image()
                    ->required()
                    ->label('Director Image'),
                Forms\Components\TextInput::make('team_title')
                    ->required()
                    ->maxLength(255)
                    ->label('Team Title'),
            ]);
    }

    public static function table(Table $table): Table
    {
        return $table
            ->columns([
                Tables\Columns\TextColumn::make('main_title')
                    ->sortable()
                    ->searchable()
                    ->label('Main Title'),
                Tables\Columns\ImageColumn::make('image')
                    ->label('Facility Image'),
                Tables\Columns\TextColumn::make('director_name')
                    ->sortable()
                    ->searchable()
                    ->label('Director Name'),
                Tables\Columns\TextColumn::make('director_position')
                    ->sortable()
                    ->searchable()
                    ->label('Director Position'),
                Tables\Columns\ImageColumn::make('director_image')
                    ->label('Director Image'),
                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 facility')),
                Tables\Actions\DeleteAction::make()
                    ->visible(fn () => Gate::allows('delete facility')),
            ])
            ->bulkActions([
                Tables\Actions\BulkActionGroup::make([
                    Tables\Actions\DeleteBulkAction::make()
                        ->visible(fn () => Gate::allows('delete facility')),
                ]),
            ]);
    }

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

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