<?php

namespace App\Filament\Exports;

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

class ImportTransitExporter extends Exporter
{
    protected static ?string $model = ImportTransit::class;

    public static function getColumns(): array
    {
        static $rowNumber = 0;
        return [
            ExportColumn::make('no')
                ->label('NO')
                ->state(function ($record) use (&$rowNumber) {
                    return ++$rowNumber;
                }),
            ExportColumn::make('cri_rfq_number')
                ->label('CRI RFQ NUMBER'),
            ExportColumn::make('mail_received_date')
                ->label('MAIL RECEIVED DATE'),
            ExportColumn::make('pricol_ref_number')
                ->label('PRICOL REF NUMBER'),
            ExportColumn::make('requester')
                ->label('REQUESTER'),
            ExportColumn::make('shipper')
                ->label('SHIPPER'),
            ExportColumn::make('shipper_location')
                ->label('SHIPPER LOCATION'),
            ExportColumn::make('shipper_invoice')
                ->label('SHIPPER INVOICE'),
            ExportColumn::make('shipper_invoice_date')
                ->label('SHIPPER INVOICE DATE'),
            ExportColumn::make('inv_value')
                ->label('Inv Value'),
            ExportColumn::make('freight_charge')
                ->label('Freight Charge'),
            ExportColumn::make('customs_agent_name')
                ->label('CUSTOMS AGENT NAME'),
            ExportColumn::make('eta_date')
                ->label('ETA DATE'),
            ExportColumn::make('status')
                ->label('STATUS'),
            ExportColumn::make('insurance_status')
                ->label('Insurance Status'),
            ExportColumn::make('delivery_location')
                ->label('DELIVERY LOCATION'),
            ExportColumn::make('etd_date')
                ->label('ETD DATE'),
            ExportColumn::make('mode')
                ->label('MODE'),
            ExportColumn::make('inco_terms')
                ->label('INCO TERMS'),
            ExportColumn::make('port_of_loading')
                ->label('PORT OF LOADING'),
            ExportColumn::make('port_of_discharge')
                ->label('PORT OF DISCHARGE'),
            ExportColumn::make('delivery_city')
                ->label('DELIVERY CITY'),
            ExportColumn::make('packages')
                ->label('PACKAGES'),
            ExportColumn::make('type_of_package')
                ->label('TYPE OF PACKAGE'),
            ExportColumn::make('gross_weight')
                ->label('GROSS WEIGHT'),
            ExportColumn::make('volume')
                ->label('VOLUME'),
            ExportColumn::make('bill_number')
                ->label('BILL NUMBER'),
            ExportColumn::make('bill_received_date')
                ->label('BILL RECEIVED DATE'),
            ExportColumn::make('vessel_number')
                ->label('VESSEL 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 import 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;
    }
}
