<?php

namespace App\Filament\Resources;

use App\Filament\Resources\ContactUsPageResource\Pages;
use App\Filament\Resources\ContactUsPageResource\RelationManagers;
use App\Models\ContactUsPage;
use DeepCopy\Filter\Filter;
use Filament\Forms;
use Filament\Forms\Components\FileUpload;
use Filament\Forms\Components\Group;
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\Columns\ImageColumn;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Builder;


use Illuminate\Database\Eloquent\SoftDeletingScope;

class ContactUsPageResource extends Resource
{
    protected static ?string $model = ContactUsPage::class;

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

    protected static ?string $navigationGroup = 'Contact Us';

    protected static ?string $navigationLabel = 'Contact Us Page';
    public static function canCreate(): bool
    {
        return ContactUsPage::count() === 0;
    }

    public static function form(Form $form): Form
    {
        return $form
            ->schema([
                Tabs::make()
                    ->tabs([
                        Tab::make('Header')
                            ->schema([
                                FileUpload::make('banner')
                                    ->image()
                                    ->disk('images'),
                                TextInput::make('title'),
                                TextInput::make('description'),

                            ]),
                        Tab::make('Map section')
                            ->schema([
                                TextInput::make('map')
                                    ->columnSpanFull(),
                                TextInput::make('first_icon_title'),
                                TextInput::make('second_icon_title'),
                                TextInput::make('third_icon_title'),
                                TextInput::make('first_icon_time'),
                                TextInput::make('second_icon_time'),
                                TextInput::make('third_icon_time'),
                                FileUpload::make('first_icon')
                                    ->image()
                                    ->disk('images'),
                                FileUpload::make('second_icon')
                                    ->image()
                                    ->disk('images'),
                                FileUpload::make('third_icon')
                                    ->image()
                                    ->disk('images'),
                            ])->columns(3),
                        Tab::make('Meta')
                            ->schema([
                                TextInput::make('meta_title'),
                                TextInput::make('meta_description'),
                                TextInput::make('meta_keyword'),
                            ]),
                    ]),
            ])->columns(1);
    }

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

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

                TextColumn::make('first_icon_title')
                    ->label('Contact Method 1')
                    ->toggleable(isToggledHiddenByDefault: true),

                TextColumn::make('second_icon_title')
                    ->label('Contact Method 2')
                    ->toggleable(isToggledHiddenByDefault: true),

                TextColumn::make('updated_at')
                    ->label('Last Updated')
                    ->dateTime()
                    ->sortable(),
            ])
            ->filters([
                // Filter::make('has_contact_methods')
                //     ->label('Has Contact Methods')
                //     ->query(fn(Builder $query): Builder => $query->whereNotNull('first_icon_title')),
            ])
            ->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')
            // ->groups([
            //     Group::make('updated_at')
            //         ->label('Update Month')
            //         ->date()
            //         ->collapsible(),
            // ])
            ->paginated([10, 25, 50]);
    }

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

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