<?php

namespace App\Filament\Resources;

use App\Filament\Exports\InvoiceMasterExporter;
use App\Filament\Resources\InvoiceMasterResource\Pages;
use App\Filament\Resources\InvoiceMasterResource\RelationManagers;
use App\Models\InvoiceMaster;
use Filament\Facades\Filament;
use Filament\Forms;
use Filament\Forms\Form;
use Filament\Notifications\Notification;
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\SoftDeletingScope;
use Filament\Tables\Actions\ExportAction;
use Storage;
use Maatwebsite\Excel\Facades\Excel;
use Filament\Forms\Components\FileUpload;

class InvoiceMasterResource extends Resource
{
    protected static ?string $model = InvoiceMaster::class;

    protected static ?string $navigationIcon = 'heroicon-o-rectangle-stack';

    protected static ?string $navigationGroup = 'Manufacturing SD';

    public static function form(Form $form): Form
    {
        return $form
            ->schema([
                Forms\Components\TextInput::make('receiving_plant')
                    ->label('Receiving Plant'),
                Forms\Components\TextInput::make('receiving_plant_name')
                    ->label('Receiving Plant Name'),
                Forms\Components\TextInput::make('transit_days')
                    ->label('Transit Days'),
                Forms\Components\TextInput::make('transport_name')
                    ->label('Transport Name'),
                Forms\Components\Hidden::make('created_by')
                    ->label('Created By')
                    ->default(Filament::auth()->user()?->name),
                Forms\Components\Hidden::make('updated_by')
                    ->label('Updated By')
                    ->default(Filament::auth()->user()?->name),
            ]);
    }

    public static function table(Table $table): Table
    {
        return $table
            ->columns([
                Tables\Columns\TextColumn::make('No.')
                    ->label('No.')
                    ->alignCenter()
                    ->getStateUsing(function ($record, $livewire, $column, $rowLoop) {
                        $paginator = $livewire->getTableRecords();
                        $perPage = method_exists($paginator, 'perPage') ? $paginator->perPage() : 10;
                        $currentPage = method_exists($paginator, 'currentPage') ? $paginator->currentPage() : 1;

                        return ($currentPage - 1) * $perPage + $rowLoop->iteration;
                    }),
                Tables\Columns\TextColumn::make('receiving_plant')
                    ->label('Receiving Plant')
                    ->alignCenter()
                    ->searchable()
                    ->sortable(),
                Tables\Columns\TextColumn::make('receiving_plant_name')
                    ->label('Receiving Plant Name')
                    ->alignCenter()
                    ->searchable()
                    ->sortable(),
                Tables\Columns\TextColumn::make('transit_days')
                    ->label('Transit Days')
                    ->alignCenter()
                    ->searchable()
                    ->sortable(),
                Tables\Columns\TextColumn::make('transport_name')
                    ->label('Transport Name')
                    ->alignCenter()
                    ->searchable()
                    ->sortable(),
                Tables\Columns\TextColumn::make('created_at')
                    ->label('Created At')
                    ->alignCenter()
                    ->dateTime()
                    ->sortable()
                    ->toggleable(isToggledHiddenByDefault: true),
                Tables\Columns\TextColumn::make('updated_at')
                    ->label('Updated At')
                    ->alignCenter()
                    ->dateTime()
                    ->sortable()
                    ->toggleable(isToggledHiddenByDefault: true),
                Tables\Columns\TextColumn::make('deleted_at')
                    ->label('Deleted At')
                    ->alignCenter()
                    ->dateTime()
                    ->sortable()
                    ->toggleable(isToggledHiddenByDefault: true),
            ])
            ->filters([
                Tables\Filters\TrashedFilter::make(),
            ])
            ->actions([
                Tables\Actions\ViewAction::make(),
                Tables\Actions\EditAction::make(),
            ])
            ->bulkActions([
                Tables\Actions\BulkActionGroup::make([
                    Tables\Actions\DeleteBulkAction::make(),
                    Tables\Actions\ForceDeleteBulkAction::make(),
                    Tables\Actions\RestoreBulkAction::make(),
                ]),
            ])
            ->headerActions([
                Tables\Actions\Action::make('Import Invoice Master')
                    ->label('Import Invoice Master')
                    ->form([
                        FileUpload::make('invoice_master_file')
                            ->label('Import Invoice Master')
                            ->preserveFilenames()
                            ->storeFiles(false)
                            ->reactive()
                            ->required()
                            ->disk('local')
                            ->directory('uploads/temp'),
                    ])
                    ->action(function (array $data) {
                        $uploadedFile = $data['invoice_master_file'];

                        $disk = Storage::disk('local');

                        $user = Filament::auth()->user();

                        $operatorName = $user->name;

                        // Get original filename
                        $originalName = $uploadedFile->getClientOriginalName();

                        $path = $uploadedFile->storeAs('uploads/temp', $originalName, 'local'); // returns relative path

                        $fullPath = Storage::disk('local')->path($path);

                        if ($fullPath && file_exists($fullPath))
                        {
                            $rows = Excel::toArray(null, $fullPath)[0];

                            if ((count($rows) - 1) <= 0) {
                                Notification::make()
                                    ->title('Records Not Found')
                                    ->body("Import the valid 'Invoice Master Data' file to proceed..!")
                                    ->danger()
                                    ->send();

                                if ($disk->exists($path)) {
                                    $disk->delete($path);
                                }

                                return;
                            }

                            $invalidReceivePlant = [];
                            $invalidReceivePlantName = [];
                            $invalidTransitDays = [];
                            $invalidTransportName = [];

                            foreach ($rows as $index => $row) {
                                if ($index == 0) {
                                    continue;
                                }

                                $receivingPlant = trim($row[0]);
                                $receivingPlantName = trim($row[1]);
                                $transitDays = trim($row[2]);
                                $transportName = trim($row[3]);

                                if (empty($receivingPlant)) {
                                    $invalidReceivePlant[] = "Row {$index}";
                                }
                                if (empty($receivingPlantName)) {
                                    $invalidReceivePlantName[] = "Row {$index}";
                                }
                                if (empty($transitDays)) {
                                    $invalidTransitDays[] = "Row {$index}";
                                }
                                if (empty($transportName)) {
                                    $invalidTransportName[] = "Row {$index}";
                                }

                            }

                            if (! empty($invalidReceivePlant) || ! empty($invalidReceivePlantName) || ! empty($invalidTransitDays) || ! empty($invalidTransportName)) {
                                $errorMsg = '';

                                if (! empty($invalidReceivePlant)) {
                                    $errorMsg .= 'Missing Receiving Plant in rows: '.implode(', ', $invalidReceivePlant).'<br>';
                                }
                                if (! empty($invalidReceivePlantName)) {
                                    $errorMsg .= 'Missing Receiving Plant Name in rows: '.implode(', ', $invalidReceivePlantName).'<br>';
                                }
                                if (! empty($invalidTransitDays)) {
                                    $errorMsg .= 'Missing Transit Days in rows: '.implode(', ', $invalidTransitDays).'<br>';
                                }
                                if (! empty($invalidTransportName)) {
                                    $errorMsg .= 'Missing Transport Name in rows: '.implode(', ', $invalidTransportName).'<br>';
                                }

                                Notification::make()
                                    ->title('Missing Mandatory Fields')
                                    ->body($errorMsg)
                                    ->danger()
                                    ->send();

                                if ($disk->exists($path)) {
                                    $disk->delete($path);
                                }

                                return;
                            }

                            foreach ($rows as $index => $row)
                            {
                                if ($index == 0) {
                                    continue;
                                }

                                $receivingPlant = trim($row[0]);
                                $receivingPlantName = trim($row[1]);
                                $transitDays = trim($row[2]);
                                $transportName = trim($row[3]);

                                $inserted = InvoiceMaster::create([
                                    'receiving_plant' => $receivingPlant,
                                    'receiving_plant_name' => $receivingPlantName,
                                    'transit_days' => $transitDays,
                                    'transport_name' => $transportName,
                                    'created_at' => now(),
                                    'created_by' => $operatorName,
                                ]);
                            }
                            if ($inserted) {
                                Notification::make()
                                    ->title('Upload Success')
                                    ->body('Invoice master uploaded successfully!')
                                    ->success()
                                    ->send();
                                return;
                            }
                            else
                            {
                                Notification::make()
                                    ->title('Insertion Failed')
                                    ->body('Invoice master upload failed!')
                                    ->success()
                                    ->send();
                                return;
                            }
                        }
                    })
                    ->visible(function () {
                        return Filament::auth()->user()->can('view import invoice master');
                    }),
                ExportAction::make()
                    ->label('Export Invoice Master')
                    ->color('warning')
                    ->exporter(InvoiceMasterExporter::class)
                    ->visible(function () {
                        return Filament::auth()->user()->can('view export invoice master');
                    }),
            ]);
    }

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

    public static function getPages(): array
    {
        return [
            'index' => Pages\ListInvoiceMasters::route('/'),
            'create' => Pages\CreateInvoiceMaster::route('/create'),
            'view' => Pages\ViewInvoiceMaster::route('/{record}'),
            'edit' => Pages\EditInvoiceMaster::route('/{record}/edit'),
        ];
    }

    public static function getEloquentQuery(): Builder
    {
        return parent::getEloquentQuery()
            ->withoutGlobalScopes([
                SoftDeletingScope::class,
            ]);
    }
}
