<?php

namespace App\Filament\Resources;

use App\Models\Header;
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\Select;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Columns\ToggleColumn;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\SoftDeletingScope;
use App\Filament\Resources\HeaderResource\Pages;
use Illuminate\Support\Facades\Gate;

class HeaderResource extends Resource
{
    protected static ?string $model = Header::class;

    protected static ?string $navigationIcon = 'heroicon-o-bars-3';

    protected static ?string $navigationLabel = 'Header Menu';

    protected static ?string $navigationGroup = 'Website Settings';

    protected static ?int $navigationSort = 2;

    public static function form(Form $form): Form
    {
        return $form
            ->schema([
                Section::make('Menu Information')
                    ->schema([
                        TextInput::make('name')
                            ->label('Menu Name')
                            ->required(),
                        TextInput::make('link')
                            ->label('Menu Link')
                            ->required(),
                        TextInput::make('slug')
                            ->label('Slug')
                            ->required(),
                        Select::make('parent_id')
                            ->label('Parent Menu')
                            ->relationship('parent', 'name')
                            ->searchable()
                            ->preload(),
                        TextInput::make('position')
                            ->label('Position')
                            ->numeric()
                            ->default(0),
                        Toggle::make('status')
                            ->label('Active')
                            ->default(true),
                    ])->columns(2),

            ]);
    }

    public static function table(Table $table): Table
    {
        return $table
            ->columns([
                TextColumn::make('name')
                    ->label('Menu Name')
                    ->searchable()
                    ->sortable(),
                TextColumn::make('link')
                    ->label('Link')
                    ->searchable(),
                TextColumn::make('parent.name')
                    ->label('Parent Menu')
                    ->sortable(),
                TextColumn::make('position')
                    ->label('Position')
                    ->sortable(),
                ToggleColumn::make('status')
                    ->label('Active'),
                TextColumn::make('created_at')
                    ->dateTime()
                    ->sortable()
                    ->toggleable(isToggledHiddenByDefault: true),
                TextColumn::make('updated_at')
                    ->dateTime()
                    ->sortable()
                    ->toggleable(isToggledHiddenByDefault: true),
            ])
            ->filters([
                Tables\Filters\TrashedFilter::make(),
            ])
            ->actions([
                Tables\Actions\EditAction::make()->visible(fn () => Gate::allows('edit header')),
                Tables\Actions\DeleteAction::make()->visible(fn () => Gate::allows('delete header')),
                Tables\Actions\ForceDeleteAction::make()->visible(fn () => Gate::allows('force delete header')),
                Tables\Actions\RestoreAction::make()->visible(fn () => Gate::allows('restore header')),
            ])
            ->bulkActions([
                Tables\Actions\BulkActionGroup::make([
                    Tables\Actions\DeleteBulkAction::make()->visible(fn () => Gate::allows('delete header')),
                    Tables\Actions\ForceDeleteBulkAction::make()->visible(fn () => Gate::allows('force delete header')),
                    Tables\Actions\RestoreBulkAction::make()->visible(fn () => Gate::allows('restore header')),
                ]),
            ]);
    }

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

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

    public static function getEloquentQuery(): Builder
    {
        return parent::getEloquentQuery()
            ->withoutGlobalScopes([
                SoftDeletingScope::class,
            ]);
    }

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

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

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

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

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

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

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