<?php

namespace App\Filament\Resources;

use App\Filament\Resources\DealerVisitPlanResource\Pages;
use App\Filament\Resources\DealerVisitPlanResource\RelationManagers;
use App\Models\DealerVisitPlan;
use Filament\Facades\Filament;
use Filament\Forms;
use Filament\Forms\Form;
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\SoftDeletingScope;
use Filament\Tables\Actions\ExportAction;
use Filament\Tables\Actions\ImportAction;
use App\Filament\Exports\DealerVisitPlanExporter;
use App\Filament\Imports\DealerVisitPlanImporter;

class DealerVisitPlanResource extends Resource
{
    protected static ?string $model = DealerVisitPlan::class;

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

    protected static ?string $navigationGroup = 'Gate Entry';

    public static function form(Form $form): Form
    {
        return $form
            ->schema([
                Forms\Components\TextInput::make('organizer')
                    ->label('Organizer'),
                Forms\Components\TextInput::make('mobile_number')
                    ->label('Mobile Number'),
                Forms\Components\TextInput::make('name')
                    ->label('Dealer Name'),
                Forms\Components\TextInput::make('company')
                    ->label('Dealer Company'),
                Forms\Components\DatePicker::make('visit_plan_date')
                    ->label('Visit Plan Date'),
                Forms\Components\Select::make('employee_master_id')
                    ->label('Recipient Employee')
                    ->relationship('employeeMaster', 'name')
                    ->required(),
                Forms\Components\TextInput::make('number_of_person')
                    ->label('Number Of Person'),
                Forms\Components\TextInput::make('purpose_of_visit')
                    ->label('Purpose Of Visit'),
                Forms\Components\Select::make('mode_of_travel')
                    ->label('Mode Of Travel')
                    ->options([
                        'Rental' => 'Rental',
                        'Car' => 'Car',
                        'Bike' => 'Bike',
                    ])
                    ->reactive()
                    ->placeholder('Select Mode of Travel'),
                Forms\Components\Select::make('status')
                    ->label('Status')
                    ->options([
                        'Planned' => 'Planned',
                        'Completed' => 'Completed',
                    ])
                    ->required(),
                Forms\Components\Hidden::make('created_by')
                    ->label('Created By')
                    ->default(Filament::auth()->user()?->name),
                Forms\Components\Hidden::make('updated_by')
                    ->label('Updated By')
                    ->default(Filament::auth()->user()?->name),
            ]);
    }

    public static function table(Table $table): Table
    {
        return $table
            ->columns([
                Tables\Columns\TextColumn::make('No.')
                    ->label('No.')
                    ->getStateUsing(function ($record, $livewire, $column, $rowLoop) {
                        $paginator = $livewire->getTableRecords();
                        $perPage = method_exists($paginator, 'perPage') ? $paginator->perPage() : 10;
                        $currentPage = method_exists($paginator, 'currentPage') ? $paginator->currentPage() : 1;

                        return ($currentPage - 1) * $perPage + $rowLoop->iteration;
                    }),
                Tables\Columns\TextColumn::make('name')
                    ->label('Dealer Name')
                    ->alignCenter()
                    ->searchable()
                    ->sortable(),
                Tables\Columns\TextColumn::make('company')
                    ->label('Dealer Company')
                    ->alignCenter()
                    ->searchable()
                    ->sortable(),
                Tables\Columns\TextColumn::make('visit_plan_date')
                    ->label('Visit Plan Date')
                    ->alignCenter()
                    ->searchable()
                    ->date()
                    ->sortable(),
                Tables\Columns\TextColumn::make('organizer')
                    ->label('Organizer')
                    ->alignCenter()
                    ->searchable()
                    ->sortable(),
                Tables\Columns\TextColumn::make('mobile_number')
                    ->label('Mobile Number')
                    ->alignCenter()
                    ->searchable()
                    ->sortable(),
                Tables\Columns\TextColumn::make('employeeMaster.name')
                    ->label('Recipient Employee')
                    ->alignCenter()
                    ->searchable()
                    ->sortable(),
                Tables\Columns\TextColumn::make('number_of_person')
                    ->label('No of Person')
                    ->alignCenter()
                    ->searchable()
                    ->sortable(),
                Tables\Columns\TextColumn::make('purpose_of_visit')
                    ->label('Purpose of Visit')
                    ->alignCenter()
                    ->searchable()
                    ->sortable(),
                Tables\Columns\TextColumn::make('mode_of_travel')
                    ->label('Mode of travel')
                    ->alignCenter()
                    ->searchable()
                    ->sortable(),
                Tables\Columns\TextColumn::make('status')
                    ->label('Status')
                    ->alignCenter()
                    ->searchable()
                    ->sortable(),
                Tables\Columns\TextColumn::make('created_at')
                    ->dateTime()
                    ->sortable()
                    ->toggleable(isToggledHiddenByDefault: true),
                Tables\Columns\TextColumn::make('updated_at')
                    ->dateTime()
                    ->sortable()
                    ->toggleable(isToggledHiddenByDefault: true),
                Tables\Columns\TextColumn::make('deleted_at')
                    ->dateTime()
                    ->sortable()
                    ->toggleable(isToggledHiddenByDefault: true),
            ])
            ->filters([
                Tables\Filters\TrashedFilter::make(),
            ])
            ->actions([
                Tables\Actions\ViewAction::make(),
                Tables\Actions\EditAction::make(),
            ])
            ->bulkActions([
                Tables\Actions\BulkActionGroup::make([
                    Tables\Actions\DeleteBulkAction::make(),
                    Tables\Actions\ForceDeleteBulkAction::make(),
                    Tables\Actions\RestoreBulkAction::make(),
                ]),
            ])
            ->headerActions([
                ImportAction::make()
                    ->label('Import Dealer Visit Plan')
                    ->color('warning')
                    ->importer(DealerVisitPlanImporter::class)
                    ->visible(function () {
                        return Filament::auth()->user()->can('view import dealer visit plan');
                    }),
                ExportAction::make()
                    ->label('Export Dealer Visit Plan')
                    ->color('warning')
                    ->exporter(DealerVisitPlanExporter::class)
                    ->visible(function () {
                        return Filament::auth()->user()->can('view export dealer visit plan');
                    }),
            ]);
    }

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

    public static function getPages(): array
    {
        return [
            'index' => Pages\ListDealerVisitPlans::route('/'),
            'create' => Pages\CreateDealerVisitPlan::route('/create'),
            'view' => Pages\ViewDealerVisitPlan::route('/{record}'),
            'edit' => Pages\EditDealerVisitPlan::route('/{record}/edit'),
        ];
    }

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