<?php

namespace App\Filament\Exports;

use App\Models\Axn;
use App\Models\InvoiceValidation;
use Filament\Actions\Exports\ExportColumn;
use Filament\Actions\Exports\Exporter;
use Filament\Actions\Exports\Models\Export;

class AxnExporter extends Exporter
{
    protected static ?string $model = InvoiceValidation::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('invoice_number')
            //     ->label('INVOICE NUMBER'),
            ExportColumn::make('year')
                ->label('YEAR')
                ->state(function ($record) {
                return substr($record->serial_number, 0, 2);
            }),
            ExportColumn::make('month')
                ->label('MONTH')
                ->state(function ($record) {
                    return substr($record->serial_number, 2, 2);
                }),
            ExportColumn::make('vendor')
                ->label('VENDOR NUMBER')
                ->state(function ($record) {
                    return substr($record->serial_number, 4, 4);
                }),
            ExportColumn::make('serial_number')
                ->label('SERIAL NUMBER')
                ->state(function ($record) {
                    $sn = $record->serial_number;

                    $offset = 2 + 2 + 4;

                    return substr($sn, $offset);
                }),
            ExportColumn::make('stickerMaster.item.code')
                ->label('ITEM CODE'),
        ];
    }

    public static function getCompletedNotificationBody(Export $export): string
    {
        $body = 'Your axn 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;
    }
}
