<?php

namespace App\Filament\Resources;

use App\Filament\Resources\HomePageResource\Pages;
use App\Filament\Resources\HomePageResource\RelationManagers;
use App\Models\HomePage;
use Filament\Forms;
use Filament\Forms\Components\FileUpload;
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\Columns\ImageColumn;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Builder;
use Filament\Tables;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Filters\Filter;
use Filament\Tables\Grouping\Group;
use Illuminate\Database\Eloquent\SoftDeletingScope;

class HomePageResource extends Resource
{
    protected static ?string $model = HomePage::class;

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

    protected static ?string $navigationGroup = 'Home Page';

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

    public static function form(Form $form): Form
    {
        return $form
            ->schema([
                Tabs::make()
                    ->tabs([
                        Tab::make('Top')
                            ->schema([
                                FileUpload::make('first_header_image')
                                    ->image()
                                    ->disk('images'),
                                FileUpload::make('seconed_header_image')
                                    ->image()
                                    ->disk('images'),
                                TextInput::make('header_title'),
                                TextInput::make('header_sub_title'),
                            ])->columns(2),
                        Tab::make('banner')
                            ->schema([
                                TextInput::make('title'),
                                TextInput::make('description'),
                                FileUpload::make('banner')
                                    ->image()
                                    ->columnSpanFull()
                                    ->imageEditor()
                                    ->imageEditorAspectRatios([
                                        '21:5' => 'Portrait (21:5)',
                                    ])
                                    ->imageCropAspectRatio('21:5')
                                    ->disk('images')
                                    ->columnSpanFull(),
                                TextInput::make('link')
                                    ->columnSpanFull(),
                                TextInput::make('title_under_banner'),
                                TextInput::make('description_under_banner'),
                            ])->columns(2),
                        Tab::make('Solutions')
                            ->schema([
                                TextInput::make('title_solutions_section'),
                                TextInput::make('description_solutions_section'),
                            ])->columns(1),
                        Tab::make('statistics')
                            ->schema([
                                TextInput::make('reduce_costs'),
                                TextInput::make('value_reduce_costs'),
                                TextInput::make('enhance_security'),
                                TextInput::make('value_enhance_security'),
                                TextInput::make('community_users'),
                                TextInput::make('value_community_users'),
                                TextInput::make('accelerate_growth'),
                                TextInput::make('value_accelerate_growth'),
                            ])->columns(2),
                        Tab::make('Portfolio')
                            ->schema([
                                TextInput::make('title_portfolio'),
                                TextInput::make('description_portfolio'),
                            ])->columns(1),
                        Tab::make('Questions')
                            ->schema([
                                TextInput::make('title_question'),
                                TextInput::make('description_question'),
                            ])->columns(1),
                        Tab::make('Testimonials')
                            ->schema([
                                TextInput::make('title_testimonials'),
                                TextInput::make('description_testimonials'),
                            ])->columns(1),
                        Tab::make('Insights')
                            ->schema([
                                TextInput::make('title_insights'),
                                TextInput::make('description_insights'),
                            ])->columns(1),
                        Tab::make('Schedule')
                            ->schema([
                                TextInput::make('title_schedule'),
                                TextInput::make('description_schedule'),
                            ])->columns(1),
                        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('first_header_image')
                    ->label('Header Image')
                    ->disk('images')
                    ->toggleable(),

                TextColumn::make('header_title')
                    ->label('Title')
                    ->searchable()
                    ->sortable()
                    ->limit(50)
                    ->wrap(),

                TextColumn::make('title_solutions_section')
                    ->label('Solutions Title')
                    ->toggleable(isToggledHiddenByDefault: true),

                TextColumn::make('title_portfolio')
                    ->label('Portfolio Title')
                    ->toggleable(isToggledHiddenByDefault: true),

                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')),

                Filter::make('recently_updated')
                    ->label('Updated Recently')
                    ->query(fn(Builder $query): Builder => $query->where('updated_at', '>=', now()->subWeek()))
                    ->toggle(),
            ])
            ->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 getPages(): array
    {
        return [
            'index' => Pages\ListHomePages::route('/'),
            'create' => Pages\CreateHomePage::route('/create'),
            'edit' => Pages\EditHomePage::route('/{record}/edit'),
        ];
    }
}
