<?php

namespace App\Filament\Resources;

use App\Filament\Resources\IndustryResource\Pages;
use App\Filament\Resources\IndustryCategoryResource\RelationManagers;
use App\Models\Industry;
use App\Models\industryCategory;
use Filament\Forms;
use Filament\Forms\Components\FileUpload;
use Filament\Forms\Components\Repeater;
use Filament\Forms\Components\RichEditor;
use Filament\Forms\Components\Select;
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 IndustryResource extends Resource
{
    protected static ?string $model = Industry::class;


    protected static ?string $navigationIcon = 'heroicon-o-building-office-2';

    protected static ?string $navigationGroup = 'Industries';
    protected static ?int $navigationSort = 4;
    public static function form(Form $form): Form
    {
        return $form
            ->schema([
                Tabs::make('')->tabs([
                    Tab::make('Make')->schema([
                        Forms\Components\TextInput::make('name')
                            ->required(),
                        Forms\Components\Textarea::make('mini_description'),
                        Forms\Components\FileUpload::make('icon')
                            ->image()
                            ->disk('images'),
                        Select::make('industry_category')
                            ->relationship('industryCategory', 'name')
                            ->multiple()
                            ->searchable()
                            ->preload(),
                    ]),
                    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('Key Benefits')->schema([
                        TextInput::make('key_benefits_title'),
                        Repeater::make('key_benefits')->schema([
                            FileUpload::make('key_benefits_icon')
                                ->image()
                                ->disk('images'),
                            TextInput::make('key_benefits_title'),
                        ]),
                    ]),
                    Tab::make('CTA')->schema([
                        TextInput::make('cta_title'),
                        TextInput::make('cta_mini_description'),
                    ]),
                    Tab::make('Testimonials')
                        ->schema([
                            TextInput::make('title_testimonials'),
                            TextInput::make('description_testimonials'),
                        ]),
                    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('icon')
                    ->label('Icon')
                    ->disk('images')
                    ->toggleable(),

                TextColumn::make('name')
                    ->label('Category Name')
                    ->searchable()
                    ->sortable(),

                TextColumn::make('mini_description')
                    ->label('Description')
                    ->limit(50)
                    ->toggleable(isToggledHiddenByDefault: true),

                TextColumn::make('updated_at')
                    ->label('Last Updated')
                    ->toggleable(isToggledHiddenByDefault: true)
                    ->dateTime()
                    ->sortable(),
            ])
            ->filters([])
            ->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('name', 'asc')
            ->groups([
                Group::make('name')
                    ->label('First Letter')
                    ->getTitleFromRecordUsing(fn(industryCategory $record) => strtoupper(substr($record->name, 0, 1)))
                    ->collapsible(),
            ])
            ->paginated([10, 25, 50, 100]);
    }


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

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