<?php

namespace App\Filament\Resources;

use App\Filament\Resources\QuoteResource\Pages;
use App\Models\Customer;
use App\Models\Order;
use App\Models\Quote;
use Filament\Forms\Components\Section;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\Split;
use Filament\Forms\Components\Textarea;
use Filament\Forms\Form;
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Tables\Table;

class QuoteResource extends Resource
{
    protected static ?string $model = Quote::class;

    protected static ?string $navigationIcon = 'lucide-quote';

    protected static ?string $navigationGroup = 'Production';

    protected static ?int $navigationSort = 1;

    public static function form(Form $form): Form
    {
        return $form
            ->schema([
                Section::make([
                    Split::make([
                        Select::make('customer_id')
                            ->required()
                            ->label('Customer')
                            ->options(Customer::all()->pluck('company_name', 'id'))
                            ->reactive()
                            ->searchable(),

                        Select::make('order_id')
                            ->label('Order')
                            ->options(fn ($get): array => Order::where('customer_id', $get('customer_id') ?? null)
                                ->get()
                                ->pluck('customer_po', 'id')
                                ->toArray())
                            ->searchable(),

                    ])->columnSpan(2),
                    Textarea::make('body')
                        ->columnSpan(2)
                        ->rows(8),
                ]),
            ])->columns(3);
    }

    public static function table(Table $table): Table
    {
        return $table
            ->columns([
                Tables\Columns\TextColumn::make('order.customer.company_name')
                    ->searchable(),
                Tables\Columns\TextColumn::make('order.customer_po')
                    ->searchable()
                    ->weight('bold')
                    ->color('code'),
                Tables\Columns\TextColumn::make('body')
                    ->searchable()
                    ->limit(100),
                Tables\Columns\TextColumn::make('created_at')
                    ->date('Y-m-d')
                    ->sortable(),
            ])
            ->defaultSort('created_at', 'desc')
            ->groups([
                'order.customer.company_name',
            ])
            ->filters([
            ])
            ->actions([
                Tables\Actions\EditAction::make(),
            ])
            ->bulkActions([
            ]);
    }

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

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