<?php

namespace App\Filament\Resources;

use App\Models\Footer;
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\Tables\Columns\TextColumn;
use Filament\Tables\Columns\ToggleColumn;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\SoftDeletingScope;
use App\Filament\Resources\FooterResource\Pages;
use Filament\Forms\Components\Repeater;
use Illuminate\Support\Facades\Gate;

class FooterResource extends Resource
{
    protected static ?string $model = Footer::class;

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

    protected static ?string $navigationLabel = 'Footer';

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

    protected static ?int $navigationSort = 1;

    public static function form(Form $form): Form
    {
        return $form
            ->schema([
                Section::make('Contact Information')
                    ->schema([
                        TextInput::make('footer_tiittle')
                            ->label('Title')
                            ->required(),
                        TextInput::make('footer_address')
                            ->label('Address')
                            ->required(),
                        TextInput::make('footer_mail_id')
                            ->label('Email')
                            ->email()
                            ->required(),
                        TextInput::make('footer_phone_num')
                            ->label('Phone Number')
                            ->tel()
                            ->required(),
                        TextInput::make('footer_tele_num')
                            ->label('Telephone Number')
                            ->tel(),
                    ])->columns(2),

                Section::make('Pages')
                    ->schema([
                        Repeater::make('pages')
                            ->label('Pages')
                            ->schema([
                                TextInput::make('name')->label('Page Name')->required(),
                                TextInput::make('link')->label('Page Link')->required(),
                            ])
                            ->addActionLabel('Add Page')
                            ->columns(2)
                            ->required(),
                    ]),

                Section::make('Social Media')
                    ->schema([
                        Toggle::make('social_status')
                            ->label('Enable Social Media')
                            ->default(true),
                        TextInput::make('social_heading_1')
                            ->label('Social Media 1 Title'),
                        TextInput::make('social_link_1')
                            ->label('Social Media 1 Link')
                            ->url(),
                        TextInput::make('social_heading_2')
                            ->label('Social Media 2 Title'),
                        TextInput::make('social_link_2')
                            ->label('Social Media 2 Link')
                            ->url(),
                        TextInput::make('social_heading_3')
                            ->label('Social Media 3 Title'),
                        TextInput::make('social_link_3')
                            ->label('Social Media 3 Link')
                            ->url(),
                        TextInput::make('social_heading_4')
                            ->label('Social Media 4 Title'),
                        TextInput::make('social_link_4')
                            ->label('Social Media 4 Link')
                            ->url(),
                    ])->columns(2),

                Section::make('Legal Information')
                    ->schema([
                        Toggle::make('term_of_service_status')
                            ->label('Enable Terms of Service')
                            ->default(true),
                        TextInput::make('term_of_service')
                            ->label('Terms of Service Text'),
                        TextInput::make('term_of_service_link')
                            ->label('Terms of Service Link')
                            ->url(),

                        Toggle::make('privacy_policy_status')
                            ->label('Enable Privacy Policy')
                            ->default(true),
                        TextInput::make('privacy_policy')
                            ->label('Privacy Policy Text'),
                        TextInput::make('privacy_policy_link')
                            ->label('Privacy Policy Link')
                            ->url(),

                        Toggle::make('copyright_status')
                            ->label('Enable Copyright')
                            ->default(true),
                        TextInput::make('copyright')
                            ->label('Copyright Text'),
                    ])->columns(2),
            ]);
    }

    public static function table(Table $table): Table
    {
        return $table
            ->columns([
                TextColumn::make('footer_tiittle')
                    ->label('Title')
                    ->searchable()
                    ->sortable(),
                TextColumn::make('footer_mail_id')
                    ->label('Email')
                    ->searchable(),
                TextColumn::make('footer_phone_num')
                    ->label('Phone')
                    ->searchable(),
                ToggleColumn::make('social_status')
                    ->label('Social Media'),
                ToggleColumn::make('term_of_service_status')
                    ->label('Terms of Service'),
                ToggleColumn::make('privacy_policy_status')
                    ->label('Privacy Policy'),
                ToggleColumn::make('copyright_status')
                    ->label('Copyright'),
                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 footer')),
                Tables\Actions\DeleteAction::make()->visible(fn () => Gate::allows('delete footer')),
                Tables\Actions\ForceDeleteAction::make()->visible(fn () => Gate::allows('force delete footer')),
                Tables\Actions\RestoreAction::make()->visible(fn () => Gate::allows('restore footer')),
            ])
            ->bulkActions([
                Tables\Actions\BulkActionGroup::make([
                    Tables\Actions\DeleteBulkAction::make()->visible(fn () => Gate::allows('delete footer')),
                    Tables\Actions\ForceDeleteBulkAction::make()->visible(fn () => Gate::allows('force delete footer')),
                    Tables\Actions\RestoreBulkAction::make()->visible(fn () => Gate::allows('restore footer')),
                ]),
            ]);
    }

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

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

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

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

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

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

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

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

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

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