<?php

namespace App\Filament\Resources;

use App\Filament\Resources\PrivacyResource\Pages;
use App\Filament\Resources\PrivacyResource\RelationManagers;
use App\Models\Privacy;
use Filament\Forms;
use Filament\Forms\Components\FileUpload;
use Filament\Forms\Components\RichEditor;
use Filament\Forms\Components\Tabs;
use Filament\Forms\Components\Tabs\Tab;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Form;
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Tables\Table;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Columns\ImageColumn;
use Filament\Tables\Filters\Filter;
use Filament\Tables\Grouping\Group;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\SoftDeletingScope;

class PrivacyResource extends Resource
{
    protected static ?string $model = Privacy::class;

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

    protected static ?string $navigationGroup = 'Legal';
    public static function canCreate(): bool
    {
        return Privacy::count() === 0;
    }

    public static function form(Form $form): Form
    {
        return $form
            ->schema([
                Tabs::make('')->tabs([
                    Tab::make('Title')->schema([
                        FileUpload::make('banner')
                            ->image()
                            ->disk('images'),
                        TextInput::make('title'),
                        TextInput::make('title_description'),
                    ])->columnSpanFull(),
                    Tab::make('First Paragraph')->schema([
                        RichEditor::make('first_paragraph')
                    ])->columnSpanFull(),
                    Tab::make('images')->schema([
                        FileUpload::make('first_image')
                            ->image()
                            ->disk('images'),
                        FileUpload::make('second_image')
                            ->image()
                            ->disk('images'),
                    ])->columnSpanFull(),
                    Tab::make('Second Paragraph')->schema([
                        RichEditor::make('second_paragraph')
                    ])->columnSpanFull(),
                    Tab::make('Booking')->schema([
                        TextInput::make('booking_title'),
                        TextInput::make('booking_description'),
                    ])->columnSpanFull(),
                    Tab::make('Meta')
                        ->schema([
                            TextInput::make('meta_title'),
                            TextInput::make('meta_description'),
                            TextInput::make('meta_keyword'),
                        ])->columnSpanFull(),
                ])
            ])->columns(1);
    }

    public static function table(Table $table): Table
    {
        return $table
            ->columns([
                ImageColumn::make('banner')
                    ->label('Banner Image')
                    ->disk('images')
                    ->toggleable(),

                TextColumn::make('title')
                    ->label('Title')
                    ->searchable()
                    ->sortable(),

                TextColumn::make('updated_at')
                    ->label('Last Updated')
                    ->dateTime()
                    ->sortable(),
            ])
            ->filters([
                Filter::make('has_banner')
                    ->label('Has Banner Image')
                    ->query(fn(Builder $query): Builder => $query->whereNotNull('banner')),
            ])
            ->actions([
                Tables\Actions\ActionGroup::make([
                    Tables\Actions\ViewAction::make(),
                    Tables\Actions\EditAction::make(),
                    Tables\Actions\DeleteAction::make(),
                ]),
            ])
            ->bulkActions([
                Tables\Actions\BulkActionGroup::make([
                    Tables\Actions\DeleteBulkAction::make(),
                ]),
            ])
            ->defaultSort('updated_at', 'desc')
            ->paginated([10, 25, 50]);
    }

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

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