<?php

namespace App\Filament\Resources;

use App\Models\TopHeader;
use Filament\Forms;
use Filament\Tables;
use Filament\Forms\Form;
use Filament\Tables\Table;
use Filament\Resources\Resource;
use Filament\Forms\Components\Section;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Components\Toggle;
use Filament\Forms\Components\FileUpload;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Columns\ImageColumn;
use App\Filament\Resources\TopHeaderResource\Pages;
use Illuminate\Support\Facades\Gate;

class TopHeaderResource extends Resource
{
    protected static ?string $model = TopHeader::class;
    protected static ?string $navigationIcon = 'heroicon-o-arrow-up-on-square-stack';
    protected static ?string $navigationGroup = 'Website Settings';

    public static function form(Form $form): Form
    {
        return $form->schema([
            Section::make('Top Header Information')
                ->schema([
                    FileUpload::make('icon_facility')
                        ->label('Facility Icon')
                        ->image()
                        ->imageResizeMode('cover')
                        ->imageCropAspectRatio('1:1')
                        ->imageResizeTargetWidth('100')
                        ->imageResizeTargetHeight('100')
                        ->directory('top-header-icons')
                        ->required(),
                    TextInput::make('facilities_tittle')
                        ->label('Facilities Title')
                        ->required(),
                    FileUpload::make('icon_studentlife')
                        ->label('Student Life Icon')
                        ->image()
                        ->imageResizeMode('cover')
                        ->imageCropAspectRatio('1:1')
                        ->imageResizeTargetWidth('100')
                        ->imageResizeTargetHeight('100')
                        ->directory('top-header-icons')
                        ->required(),
                    TextInput::make('studentlife_tittle')
                        ->label('Student Life Title')
                        ->required(),
                    Toggle::make('search_status')
                        ->label('Show Search?')
                        ->onColor('success')
                        ->offColor('danger'),
                ])
                ->columns(2),
            Section::make('Header Buttons')
                ->schema([
                    Toggle::make('applynow_status')
                        ->label('Show Apply Now Button')
                        ->onColor('success')
                        ->offColor('danger'),
                    TextInput::make('apply_now')
                        ->label('Apply Now Label'),
                    TextInput::make('apply_now_link')
                        ->label('Apply Now Link'),
                    Toggle::make('requestinfo_status')
                        ->label('Show Request Info Button')
                        ->onColor('success')
                        ->offColor('danger'),
                    TextInput::make('requestinfo')
                        ->label('Request Info Label'),
                    TextInput::make('requestinfo_link')
                        ->label('Request Info Link'),
                ])
                ->columns(2),
        ]);
    }

    public static function table(Table $table): Table
    {
        return $table
            ->columns([
                ImageColumn::make('icon_facility')
                    ->label('Facility Icon')
                    ->circular(),
                TextColumn::make('facilities_tittle')
                    ->label('Facilities Title')
                    ->searchable(),
                ImageColumn::make('icon_studentlife')
                    ->label('Student Life Icon')
                    ->circular(),
                TextColumn::make('studentlife_tittle')
                    ->label('Student Life Title')
                    ->searchable(),
                TextColumn::make('search_status')
                    ->label('Search Enabled')
                    ->formatStateUsing(fn ($state) => $state ? 'Yes' : 'No'),
                TextColumn::make('applynow_status')
                    ->label('Apply Now')
                    ->formatStateUsing(fn ($state) => $state ? 'Yes' : 'No'),
                TextColumn::make('apply_now')
                    ->label('Apply Now Label'),
                TextColumn::make('apply_now_link')
                    ->label('Apply Now Link'),
                TextColumn::make('requestinfo_status')
                    ->label('Request Info')
                    ->formatStateUsing(fn ($state) => $state ? 'Yes' : 'No'),
                TextColumn::make('requestinfo')
                    ->label('Request Info Label'),
                TextColumn::make('requestinfo_link')
                    ->label('Request Info Link'),
                TextColumn::make('created_at')
                    ->dateTime()
                    ->sortable()
                    ->toggleable(isToggledHiddenByDefault: true),
                TextColumn::make('updated_at')
                    ->dateTime()
                    ->sortable()
                    ->toggleable(isToggledHiddenByDefault: true),
            ])
            ->defaultSort('created_at', 'desc')
            ->actions([
                Tables\Actions\EditAction::make()->visible(fn () => Gate::allows('edit topheader')),
                Tables\Actions\DeleteAction::make()->visible(fn () => Gate::allows('delete topheader')),
            ])
            ->bulkActions([
                Tables\Actions\BulkActionGroup::make([
                    Tables\Actions\DeleteBulkAction::make()->visible(fn () => Gate::allows('delete topheader')),
                ]),
            ]);
    }

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

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

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

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

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

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

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

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