<?php

namespace App\Filament\Resources;

use App\Models\HeaderLogo;
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\FileUpload;
use Filament\Forms\Components\TextInput;
use Filament\Tables\Columns\ImageColumn;
use Filament\Tables\Columns\TextColumn;
use App\Filament\Resources\HeaderLogoResource\Pages;
use Illuminate\Support\Facades\Gate;

class HeaderLogoResource extends Resource
{
    protected static ?string $model = HeaderLogo::class;

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

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

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

    protected static ?int $navigationSort = 3;

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

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

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

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

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

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

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

    public static function form(Form $form): Form
    {
        return $form
            ->schema([
                Section::make('Logo Information')
                    ->schema([
                        FileUpload::make('logo')
                            ->label('Logo Image')
                            ->image()
                            ->directory('header-logos')
                            ->required(),
                        TextInput::make('url')
                            ->label('Logo URL')
                            ->url()
                            ->required(),
                        TextInput::make('slogan')
                            ->label('Slogan')
                            ->required(),
                        TextInput::make('title')
                            ->label('Title')
                            ->required(),
                    ])->columns(1),
            ]);
    }

    public static function table(Table $table): Table
    {
        return $table
            ->columns([
                ImageColumn::make('logo')
                    ->label('Logo')
                    ->circular(),
                TextColumn::make('url')
                    ->label('URL')
                    ->searchable(),
                TextColumn::make('slogan')
                    ->label('Slogan')
                    ->searchable(),
                TextColumn::make('title')
                    ->label('Title')
                    ->searchable(),
                TextColumn::make('created_at')
                    ->dateTime()
                    ->sortable()
                    ->toggleable(isToggledHiddenByDefault: true),
                TextColumn::make('updated_at')
                    ->dateTime()
                    ->sortable()
                    ->toggleable(isToggledHiddenByDefault: true),
            ])
            ->filters([
                //
            ])
            ->actions([
                Tables\Actions\EditAction::make()
                    ->visible(fn () => Gate::allows('edit headerlogo')),
                Tables\Actions\DeleteAction::make()
                    ->visible(fn () => Gate::allows('delete headerlogo')),
            ])
            ->bulkActions([
                Tables\Actions\BulkActionGroup::make([
                    Tables\Actions\DeleteBulkAction::make()
                        ->visible(fn () => Gate::allows('delete headerlogo')),
                ]),
            ]);
    }

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

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