<?php

namespace App\Filament\Exports;

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

class InvoiceInTransitExporter extends Exporter
{
    protected static ?string $model = InvoiceInTransit::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'),
            ExportColumn::make('receiving_plant')
            ->label('RECEIVING PLANT'),
            ExportColumn::make('receiving_plant_name')
            ->label('RECEIVING PLANT NAME'),
            ExportColumn::make('invoice_number')
            ->label('INVOICE NUMBER'),
            ExportColumn::make('invoice_date')
            ->label('INVOICE DATE'),
            ExportColumn::make('item_code')
            ->label('ITEM CODE'),
            ExportColumn::make('description')
            ->label('DESCRIPTION'),
            ExportColumn::make('quantity')
            ->label('QUANTITY'),
            ExportColumn::make('transport_name')
            ->label('TRANSPORT NAME'),
            ExportColumn::make('lr_bl_aw_number')
            ->label('LR_BL_AW NUMBER'),
            ExportColumn::make('lr_bl_aw_date')
            ->label('LR_BL_AW DATE'),
            ExportColumn::make('pending_days')
            ->label('PENDING DAYS'),
            ExportColumn::make('obd_number')
            ->label('OBD NUMBER'),
            ExportColumn::make('obd_date')
            ->label('OBD DATE'),
            ExportColumn::make('shipment_weight')
            ->label('SHIPMENT WEIGHT'),
            ExportColumn::make('unit_price')
            ->label('UNIT PRICE'),
            ExportColumn::make('net_value')
            ->label('NET VALUE'),
            ExportColumn::make('total_item_amount')
            ->label('TOTAL ITEM AMOUNT'),
            ExportColumn::make('tax_amount')
            ->label('TAX AMOUNT'),
            ExportColumn::make('transport_mode')
            ->label('TRANSPORT MODE'),
            ExportColumn::make('vehicle_number')
            ->label('VEHICLE NUMBER'),
            ExportColumn::make('e_waybill_number')
            ->label('E_WAYBILL NUMBER'),
            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 invoice in transit 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;
    }
}
