<?php

namespace App\Filament\Admin\Resources;

use App\Enums\IconEnum;
use App\Models\Customer;
use App\Models\Quote;
use Filament\Forms\Components\DatePicker;
use Filament\Forms\Components\Section;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\Textarea;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Form;
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Table;
use Icetalker\FilamentTableRepeater\Forms\Components\TableRepeater;

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

    protected static ?string $navigationIcon = IconEnum::QUOTE->value;

    protected static ?string $navigationGroup = 'Production';

    protected static ?int $navigationSort = 1;

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

                    DatePicker::make('date')
                        ->required(),

                    TextArea::make('notes')
                        ->columnSpan(2),
                ])
                    ->columns(2),

                TableRepeater::make('embroideryEntries')
                    ->relationship('embroideryEntries')
                    ->schema([
                        TextInput::make('logo')
                            ->label('Logo name'),
                        TextInput::make('placement'),
                        TextInput::make('quantity')
                            ->prefix('#'),
                        TextInput::make('width')
                            ->suffix('"'),
                        TextInput::make('height')
                            ->suffix('"'),
                        TextInput::make('stitch_count'),
                        TextInput::make('digitizing_cost')
                            ->prefix('$'),
                        TextInput::make('run_charge')
                            ->prefix('$'),
                    ]),

                TableRepeater::make('screenPrintEntries')
                    ->relationship('screenPrintEntries')
                    ->schema([
                        TextInput::make('logo')
                            ->label('Logo name'),
                        TextInput::make('quantity')
                            ->prefix('#'),
                        TextInput::make('width')
                            ->suffix('"'),
                        TextInput::make('height')
                            ->suffix('"'),
                        TextInput::make('color_amount'),
                        TextInput::make('color_match')
                            ->prefix('$'),
                        TextInput::make('flash')
                            ->prefix('$'),
                        TextInput::make('fleece')
                            ->prefix('$'),
                        TextInput::make('poly_ink')
                            ->prefix('$'),
                        TextInput::make('other_charges'),
                    ]),

                TableRepeater::make('heatTransferEntries')
                    ->relationship('heatTransferEntries')
                    ->schema([
                        TextInput::make('logo')
                            ->label('Logo name'),
                        TextInput::make('quantity')
                            ->prefix('#'),
                        TextInput::make('Width')
                            ->suffix('"'),
                        TextInput::make('Height')
                            ->suffix('"'),
                        TextInput::make('price')
                            ->prefix('$'),
                    ]),
            ])->columns(1);
    }

    public static function table(Table $table): Table
    {
        return $table
            ->columns([
                TextColumn::make('date')
                    ->date('Y-m-d')
                    ->sortable()
                    ->searchable(),

                TextColumn::make('customer.company_name')
                    ->sortable()
                    ->searchable(),

                TextColumn::make('notes')
                    ->searchable()
                    ->extraHeaderAttributes(['class' => 'w-full']),
            ])
            ->defaultSort('created_at', 'desc')
            ->groups([
                'order.customer.company_name',
            ])
            ->filters([
            ])
            ->actions([
                Tables\Actions\EditAction::make(),
            ])
            ->bulkActions([
            ]);
    }

    public static function canAccess(): bool
    {
        return auth()->user()->is_admin ?? false;
    }

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

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