<?php

namespace App\Filament\Exports;

use App\Models\StockDataMaster;
use Filament\Actions\Exports\ExportColumn;
use Filament\Actions\Exports\Exporter;
use Filament\Actions\Exports\Models\Export;

class StockDataMasterExporter extends Exporter
{
    protected static ?string $model = StockDataMaster::class;

    public static function getColumns(): array
    {
        static $rowNumber = 0;

        return [
            ExportColumn::make('no')
                ->label('NO')
                ->state(function ($record) use (&$rowNumber) {
                    // Increment and return the row number
                    return ++$rowNumber;
                }),
            ExportColumn::make('plant.code')
                ->label('PLANT CODE'),
            ExportColumn::make('stickerMaster.item.code')
                ->label('ITEM CODE'),
            ExportColumn::make('type')
                ->label('TYPE')
                ->formatStateUsing(fn ($state) => match ($state) {
                    '0' => 'FG',
                    '1' => 'NON-FG',
                    default => '-',
                }),
            ExportColumn::make('location')
                ->label('LOCATION'),
            ExportColumn::make('bin')
                ->label('BIN'),
            ExportColumn::make('serial_number')
                ->label('SERIAL NUMBER'),
            ExportColumn::make('batch')
                ->label('BATCH'),
            ExportColumn::make('quantity')
                ->label('QUANTITY'),
            ExportColumn::make('doc_no')
                ->label('DOCUMENT NUMBER'),
            ExportColumn::make('motor_scanned_status')
                ->label('MOTOR SCANNED STATUS'),
            ExportColumn::make('pump_scanned_status')
                ->label('PUMP SCANNED STATUS'),
            ExportColumn::make('capacitor_scanned_status')
                ->label('CAPACITOR SCANNED STATUS'),
            ExportColumn::make('scanned_status_set')
                ->label('SCANNED STATUS SET'),
            ExportColumn::make('panel_box_item_code')
                ->label('PANEL BOX ITEM CODE'),
            ExportColumn::make('panel_box_supplier')
                ->label('PANEL BOX SUPPLIER'),
            ExportColumn::make('panel_box_sno')
                ->label('PANEL BOX SNO'),
            ExportColumn::make('scanned_status')
                ->label('SCANNED STATUS'),
            ExportColumn::make('scanned_quantity')
                ->label('SCANNED QUANTITY'),
            ExportColumn::make('system_stock')
                ->label('SYSTEM STOCK')
                ->state(fn ($record) => $record->quantity),
            ExportColumn::make('scanned_stock')
                ->label('SCANNED STOCK')
                ->state(fn ($record) => $record->scanned_quantity),
            ExportColumn::make('duplicate_stock')
                ->label('DUPLICATE STOCK')
                ->state(function ($record) {
                    return \App\Models\DuplicateStock::where('stock_data_master_id', $record->id)->count();
                }),
            ExportColumn::make('not_in_stock')
                ->label('NOT IN STOCK')
                ->state(function ($record) {
                    return \App\Models\NotInStock::where('serial_number', $record->serial_number)
                        ->where('plant_id', $record->plant_id)
                        ->count();
                }),
            ExportColumn::make('physical_stock')
                ->label('PHYSICAL STOCK')
                ->state(function ($record) {

                    $duplicate = \App\Models\DuplicateStock::where('stock_data_master_id', $record->id)->count();

                    $notInStock = \App\Models\NotInStock::where('serial_number', $record->serial_number)
                        ->where('plant_id', $record->plant_id)
                        ->count();

                    $scanned = $record->scanned_quantity ?? 0;

                    return $scanned + $duplicate + $notInStock;
                }),

            ExportColumn::make('stock_difference')
                ->label('STOCK DIFFERENCE COUNT')
                ->state(function ($record) {

                    $duplicate = \App\Models\DuplicateStock::where('stock_data_master_id', $record->id)->count();

                    $notInStock = \App\Models\NotInStock::where('serial_number', $record->serial_number)
                        ->where('plant_id', $record->plant_id)
                        ->count();

                    $scanned = (int) $record->scanned_quantity;

                    $physicalStock = $scanned + $duplicate + $notInStock;

                    $systemStock = (int) $record->quantity;

                    $difference = $physicalStock - $systemStock;

                    return max($difference, 0);
                }),
            ExportColumn::make('created_at')
                ->label('CREATED AT'),
            ExportColumn::make('updated_at')
                ->label('UPDATED AT'),
            ExportColumn::make('created_by')
                ->label('CREATED BY'),
            ExportColumn::make('updated_by')
                ->label('UPDATED BY'),
            ExportColumn::make('deleted_at')
                ->label('DELETED AT')
                ->enabledByDefault(false),
        ];
    }

    public static function getCompletedNotificationBody(Export $export): string
    {
        $body = 'Your stock data master export has completed and '.number_format($export->successful_rows).' '.str('row')->plural($export->successful_rows).' exported.';

        if ($failedRowsCount = $export->getFailedRowsCount()) {
            $body .= ' '.number_format($failedRowsCount).' '.str('row')->plural($failedRowsCount).' failed to export.';
        }

        return $body;
    }
}
