<?php

namespace App\Filament\Resources;

use App\Filament\Resources\VehiclesResource\Pages;
use App\Filament\Resources\VehiclesResource\RelationManagers;
use App\Models\Vehicles;
use UnitEnum;
use BackedEnum;
use Filament\Schemas\Schema;
use Filament\Resources\Resource;
use Filament\Tables\Table;
use Filament\Tables;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\SoftDeletingScope;
use Filament\Tables\Columns\IconColumn;
use Filament\Tables\Columns\TextColumn;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\Toggle;
use App\Models\Bidders;
use App\Models\CarShowCategory;
use Filament\Actions\EditAction;
use Filament\Actions\BulkActionGroup;
use Filament\Actions\DeleteBulkAction;

class VehiclesResource extends Resource
{
    protected static ?string $model = Vehicles::class;

    protected static string | BackedEnum | null $navigationIcon = 'heroicon-o-rectangle-stack';

    protected static string | UnitEnum | null $navigationGroup = 'Car Show';

    public static function form(Schema $schema): Schema
    {
        return $schema
            ->components([
                Select::make('owner')
                    ->label('Owner')
                    ->options(Bidders::orderBy('bidder_assigned_number')->pluck('bidder_assigned_number', 'idbidders'))
                    ->searchable(),
                TextInput::make('year')
                    ->label('Year'),
                TextInput::make('make')
                    ->label('Make'),
                TextInput::make('model')
                    ->label('Model'),
                Select::make('type')
                    ->label('Type')
                    ->options(CarShowCategory::vehtype()->pluck('category_name', 'id'))
                    ->searchable(),
            ]);
    }

    public static function table(Table $table): Table
    {
        return $table
            ->columns([
                TextColumn::make('vehicleOwner.bidder_assigned_number')
                    ->label('Number')
                    ->sortable(),
                TextColumn::make('year')
                    ->label('Year')
                    ->sortable(),
                TextColumn::make('make')
                    ->label('Make')
                    ->sortable(),
                TextColumn::make('model')
                    ->label('Model')
                    ->sortable(),
                TextColumn::make('vehicleType.category_name')
                    ->label('Type')
                    ->sortable(),
                IconColumn::make('doNotJudge')
                    ->label('Judged?')
                    ->sortable()
                    ->boolean()
                    ->falseIcon('heroicon-o-check-badge')
                    ->trueIcon('heroicon-o-x-circle')
                    ->trueColor('danger')
                    ->falseColor('success'),
                TextColumn::make('vehicleOwner.bidder_fname')
                    ->label('First Name')
                    ->sortable(),
                TextColumn::make('vehicleOwner.bidder_lname')
                    ->label('Last Name')
                    ->sortable(),
            ])
            ->filters([
                //
            ])
            ->recordActions([
                EditAction::make(),
            ])
            ->toolbarActions([
                BulkActionGroup::make([
                    DeleteBulkAction::make(),
                ]),
            ])
            ->defaultSort('vehicleOwner.bidder_assigned_number');
    }
    
    public static function getRelations(): array
    {
        return [
            RelationManagers\VehicleOwnerRelationManager::class,
            RelationManagers\VehicleScoresRelationManager::class,
            RelationManagers\VehicleTypeRelationManager::class,
            RelationManagers\CarShowWinnerRelationManager::class,
        ];
    }
    
    public static function getPages(): array
    {
        return [
            'index' => Pages\ListVehicles::route('/'),
            'create' => Pages\CreateVehicles::route('/create'),
            'edit' => Pages\EditVehicles::route('/{record}/edit'),
        ];
    }    
}
