<?php

namespace App\Filament\Exports;

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

class PanelBoxValidationExporter extends Exporter
{
    protected static ?string $model = PanelBoxValidation::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.name')
            ->label('PLANT NAME'),
            ExportColumn::make('line.name')
            ->label('LINE NAME'),
            ExportColumn::make('stickerMaster.item.code')
            ->label('ITEM CODE'),
            ExportColumn::make('production_order')
            ->label('PRODUCTION ORDER'),
            ExportColumn::make('inspection_lot_number')
            ->label('INSPECTION LOT NUMBER'),
            ExportColumn::make('serial_number')
            ->label('SERIAL NUMBER'),
            ExportColumn::make('panel_box_supplier')
            ->label('PANEL BOX SUPPLIER'),
            ExportColumn::make('panel_box_serial_number')
            ->label('PANEL BOX SERIAL NUMBER'),
            ExportColumn::make('panel_box_code')
            ->label('PANEL BOX CODE'),
            ExportColumn::make('serial_number_panel')
            ->label('SERIAL NUMBER PANEL'),
            ExportColumn::make('pack_slip_panel')
            ->label('PACK SLIP PANEL'),
            ExportColumn::make('name_plate_panel')
            ->label('NAME PLATE PANEL'),
            ExportColumn::make('tube_sticker_panel')
            ->label('TUBE STICKER PANEL'),
            ExportColumn::make('warranty_card_panel')
            ->label('WARRANTY CARD PANEL'),
            ExportColumn::make('part_validation1')
            ->label('PART VALIDATION 1'),
            ExportColumn::make('part_validation2')
            ->label('PART VALIDATION 2'),
            ExportColumn::make('part_validation3')
            ->label('PART VALIDATION 3'),
            ExportColumn::make('part_validation4')
            ->label('PART VALIDATION 4'),
            ExportColumn::make('part_validation5')
            ->label('PART VALIDATION 5'),
            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 panel box validation 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;
    }
}
