<?php

namespace App\Filament\Resources;

use App\Filament\Resources\BiddersResource\Pages;
use App\Filament\Resources\BiddersResource\RelationManagers;
use App\Models\Bidders;
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\TextColumn;
use Filament\Forms\Components\TextInput;
use UnitEnum;
use BackedEnum;
use Filament\Schemas\Schema;
use Filament\Actions\EditAction;
use Filament\Actions\BulkActionGroup;
use Filament\Actions\DeleteBulkAction;

class BiddersResource extends Resource
{
    protected static ?string $model = Bidders::class;

    protected static ?string $recordTitleAttribute = 'idbidders';

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

    protected static string | UnitEnum | null $navigationGroup = 'Silent Auction';

    public static function form(Schema $schema): Schema
    {
        return $schema
            ->components([
                TextInput::make('bidder_fname')->label('First Name'),
                TextInput::make('bidder_lname')->label('Last Name'),
                TextInput::make('bidder_addr')->label('Address'),
                TextInput::make('bidder_city')->label('City'),
                TextInput::make('bidder_state')->label('State'),
                TextInput::make('bidder_zip')->label('Zip'),
                TextInput::make('bidder_phone')->label('Phone Number')   
                  ->mask('(999) 999-9999'),
                TextInput::make('bidder_email')->label('Email'),
                TextInput::make('bidder_assigned_number')->label('Assigned Number'),
            ]);
    }

    public static function table(Table $table): Table
    {
        return $table
            ->columns([
                TextColumn::make('bidder_assigned_number')->sortable()->label('Assigned Number')->sortable(),
                TextColumn::make('bidder_fname')->sortable()->label('First Name')->sortable(),
                TextColumn::make('bidder_lname')->sortable()->label('Last Name')->sortable(),
                TextColumn::make('bidder_addr')->label('Address')->sortable(),
                TextColumn::make('bidder_city')->label('City')->sortable(),
                TextColumn::make('bidder_state')->label('State')->sortable(),
                TextColumn::make('bidder_zip')->label('Zip')->sortable(),
                TextColumn::make('bidder_phone')->label('Phone Number')->sortable()
                    ->formatStateUsing(fn ($state) => \App\Helpers\PhoneHelper::format($state)),
                TextColumn::make('bidder_email')->label('Email')->sortable(),
            ])
            ->filters([
                //
            ])
            ->recordActions([
                EditAction::make(),
            ])
            ->toolbarActions([
                BulkActionGroup::make([
                    DeleteBulkAction::make(),
                ]),
            ])
            ->defaultSort('bidder_assigned_number');
    }
    
    public static function getRelations(): array
    {
        return [
            RelationManagers\VehiclesRelationManager::class,
            RelationManagers\CheckoutRelationManager::class,
            RelationManagers\WinningBidsRelationManager::class,
        ];
    }
    
    public static function getPages(): array
    {
        return [
            'index' => Pages\ListBidders::route('/'),
            'create' => Pages\CreateBidders::route('/create'),
            'edit' => Pages\EditBidders::route('/{record}/edit'),
        ];
    }    
}
