<?php

namespace App\Filament\Resources\Transmittals;

use Filament\Tables\Columns\Layout\Split;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Columns\Layout\Stack;
use Filament\Tables\Columns\Layout\View;
use Filament\Actions\BulkActionGroup;
use Filament\Actions\DeleteBulkAction;
use Filament\Actions\Action;
use Filament\Actions\ViewAction;
use Filament\Actions\EditAction;
use Filament\Actions\DeleteAction;
use Filament\Schemas\Schema;
use App\Filament\Resources\Transmittals\Pages\ListTransmittals;
use App\Filament\Resources\Transmittals\Pages\CreateTransmittal;
use App\Filament\Resources\Transmittals\Pages\ViewTransmittal;
use App\Filament\Resources\Transmittals\Pages\EditTransmittal;
use App\Commands\Transmittal\GenerateTransmittalSeries;
use App\Commands\Transmittal\StoreTransmittalCommand;
use App\Filament\Resources\TransmittalResource\Pages;
use App\Filament\Exports\TransmittalExcelExport;
use App\Models\Branch;
use App\Models\Client;
use App\Models\Transmittal;
use App\Models\User;
use Filament\Forms;
use Filament\Forms\Components\DatePicker;
use Filament\Forms\Components\Repeater;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\Textarea;
use Filament\Forms\Components\TextInput;
use Filament\Resources\Resource;
use Filament\Support\Enums\FontWeight;
use Filament\Tables;
use Filament\Tables\Filters\SelectFilter;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Facades\Auth;
use Malzariey\FilamentDaterangepickerFilter\Filters\DateRangeFilter;

class TransmittalResource extends Resource
{
    protected static ?string $model = Transmittal::class;

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

    public static function getEloquentQuery(): Builder
    {
        return parent::getEloquentQuery()->orderBy('id', 'desc');
    }

    public static function table(Table $table): Table
    {
        return $table
            ->columns([
                Split::make([
                    TextColumn::make('series')
                        ->searchable(query: function (Builder $query, string $search): Builder {
                            $wildcardSearch = '%' . str_replace(' ', '%', $search) . '%';

                            return $query->where(function (Builder $query) use ($wildcardSearch) {
                                $query->where('series', 'like', $wildcardSearch)
                                    ->orWhereHas('notes', function (Builder $query) use ($wildcardSearch) {
                                        $query->where('comment', 'like', $wildcardSearch);
                                    })
                                    ->orWhereHas('files', function (Builder $query) use ($wildcardSearch) {
                                        $query->where('description', 'like', $wildcardSearch);
                                    });
                            });
                        })
                        ->label('Series')
                        ->weight(FontWeight::Bold)
                        ->columnSpan(2),
                    Stack::make([
                        TextColumn::make('client.company')
                            ->searchable(query: function (Builder $query, string $search): Builder {
                                return $query->whereHas('client', function (Builder $query) use ($search) {
                                    $query->where('company', 'like', '%' . str_replace(' ', '%', $search) . '%');
                                });
                            })
                            ->weight(FontWeight::SemiBold)->label('Client'),
                        TextColumn::make('branch.code')
                            ->searchable(query: function (Builder $query, string $search): Builder {
                                return $query->whereHas('branch', function (Builder $query) use ($search) {
                                    $query->where('code', 'like', '%' . str_replace(' ', '%', $search) . '%');
                                });
                            })
                            ->label('Branch'),
                    ]),
                ]),
                View::make('transmittal.tables.collapsible-files-component')->collapsible(),
            ])
            ->filters([
                SelectFilter::make('client_id')->label('Client filter')->options(function () {
                    return Client::query()->get()->pluck('company', 'id');
                }),
                SelectFilter::make('branch_id')->label('Branch Filter')->options(function () {
                    return Branch::query()->get()->pluck('code', 'id');
                }),
                SelectFilter::make('user_id')->label('Assigned To Filter')->options(function () {
                    return User::query()->get()->pluck('name', 'id');
                }),
                DateRangeFilter::make('date_created'),
                DateRangeFilter::make('date_dispatch'),
                DateRangeFilter::make('date_received'),

            ])
            ->heading('Transmittals')
            ->description('Click on toggle button at the end of table row to show additional details.')
            ->recordActions(static::getTableActions())
            ->toolbarActions([
                BulkActionGroup::make([
                    DeleteBulkAction::make(),
                ]),
            ]);
    }

    public static function getTableActions(): array
    {
        return [
            Action::make('Export')
                ->label('Export as Excel')
                ->icon('heroicon-o-arrow-down-tray')
                ->action(function (Transmittal $record, $livewire) {
                    $export = TransmittalExcelExport::make('transmittal');

                    return app()->call([$export, 'hydrate'], [
                        'livewire' => $livewire,
                        'records' => collect([$record]),
                        'formData' => null,
                    ])->export();
                }),
            ViewAction::make(),
            Action::make('Update Status')
                ->fillForm(function ($record) {
                    return [
                        'user_id' => $record->user_id,
                        'date_dispatch' => $record->date_dispatch,
                        'date_received' => $record->date_received,
                        'received_by' => $record->received_by,
                    ];
                })
                ->schema([
                    Select::make('user_id')->label('Dispatch By')
                        ->relationship('user', 'name')
                        ->searchable()
                        ->preload(),
                    Datepicker::make('date_dispatch')->label('Dispatch Date')
                        ->native(false)->default(now()),
                    TextInput::make('received_by')->label('Received By'),
                    Datepicker::make('date_received')->label('Date Received')->native(false),
                ])
                ->action(function ($data, $record) {
                    $data['id'] = $record->id;
                    (new StoreTransmittalCommand)->execute($data);
                })
                ->icon('heroicon-o-pencil-square')
                ->slideOver()
                ->hidden(! Auth::user()->can('update_transmittal')),
            EditAction::make(),
            DeleteAction::make(),
        ];
    }

    public static function form(Schema $schema): Schema
    {
        return $schema
            ->components(static::getFormSchema());
    }

    public static function getFormSchema(): array
    {
        return [
            Select::make('client_id')
                ->options(function () {
                    return Client::query()->get()->pluck('company', 'id');
                })
                ->label('Client')
                ->reactive()
                ->required()
                ->required()->columnSpan(3),
            Select::make('branch_id')->label('Branch')->relationship('branch')->options(function (callable $get) {
                return Branch::query()->where('client_id', $get('client_id'))->get()->pluck('code', 'id');
            })->required(),
            TextInput::make('series')->readOnly()->default((new GenerateTransmittalSeries)->execute([]))->unique('transmittals', ignoreRecord: true),
            DatePicker::make('date_created')
                ->native(false)
                ->required()->default(now()),

            Repeater::make('files')
                ->relationship('files')
                ->label('Item')
                ->schema([
                    Textarea::make('description')->required(),
                    Repeater::make('comments')
                        ->relationship('notes')
                        ->label('Note')
                        ->schema([
                            TextInput::make('comment')->label('Note')->required(),
                        ]),
                    Repeater::make('remarks')
                        ->relationship('remarks')
                        ->label('Remarks')
                        ->schema([
                            TextInput::make('remark')->label('Remark')->required(),
                        ]),
                ])
                ->columns(3)
                ->columnSpan(3),
        ];
    }

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

    public static function getPages(): array
    {
        return [
            'index' => ListTransmittals::route('/'),
            'create' => CreateTransmittal::route('/create'),
            'view' => ViewTransmittal::route('/{record}'),
            'edit' => EditTransmittal::route('/{record}/edit'),
        ];
    }
}
