<?php

namespace App\Filament\Exports;

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

class StickerMasterExporter extends Exporter
{
    protected static ?string $model = StickerMaster::class;

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

        return [
            // ExportColumn::make('id')
            //     ->label('ID'),
            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('item.code')
                ->label('ITEM CODE'),
            ExportColumn::make('serial_number_motor')
                ->label('SERIAL NUMBER MOTOR'),
            ExportColumn::make('serial_number_pump')
                ->label('SERIAL NUMBER PUMP'),
            ExportColumn::make('serial_number_pumpset')
                ->label('SERIAL NUMBER PUMPSET'),
            ExportColumn::make('pack_slip_motor')
                ->label('PACK SLIP MOTOR'),
            ExportColumn::make('pack_slip_pump')
                ->label('PACK SLIP PUMP'),
            ExportColumn::make('pack_slip_pumpset')
                ->label('PACK SLIP PUMPSET'),
            ExportColumn::make('name_plate_motor')
                ->label('NAME PLATE MOTOR'),
            ExportColumn::make('name_plate_pump')
                ->label('NAME PLATE PUMP'),
            ExportColumn::make('name_plate_pumpset')
                ->label('NAME PLATE PUMPSET'),
            ExportColumn::make('tube_sticker_motor')
                ->label('TUBE STICKER MOTOR'),
            ExportColumn::make('tube_sticker_pump')
                ->label('TUBE STICKER PUMP'),
            ExportColumn::make('tube_sticker_pumpset')
                ->label('TUBE STICKER PUMPSET'),
            ExportColumn::make('warranty_card')
                ->label('WARRANTY CARD'),
            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('laser_part_validation1')
                ->label('LASER PART VALIDATION 1'),
            ExportColumn::make('laser_part_validation2')
                ->label('LASER PART VALIDATION 2'),
            ExportColumn::make('panel_box_code')
                ->label('PANEL BOX CODE'),
            ExportColumn::make('load_rate')
                ->label('LOAD RATE'),
            ExportColumn::make('material_type')
                ->label('MATERIAL TYPE'),
            ExportColumn::make('bundle_quantity')
                ->label('BUNDLE QUANTITY'),
            ExportColumn::make('created_at')
                ->label('CREATED AT'),
            ExportColumn::make('updated_at')
                ->label('UPDATED AT'),
            ExportColumn::make('deleted_at')
                ->enabledByDefault(false)
                ->label('DELETED AT'),
        ];
    }

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