<?php

namespace App\Filament\Exports;

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

class PumpTestingEntryExporter extends Exporter
{
    protected static ?string $model = PumpTestingEntry::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('tested_date')
                ->label('TESTED DATE'),
            ExportColumn::make('tested_type')
                ->label('TESTED TYPE'),
            ExportColumn::make('pumpTestingMaster.item.code')
                ->label('PUMP CODE'),
            ExportColumn::make('pumpTestingMaster.item.category')
                ->label('PUMP CATEGORY')
                ->enabledByDefault(false),
            ExportColumn::make('pumpTestingMaster.item.description')
                ->label('PUMP TYPE'),
            ExportColumn::make('pumpTestingMaster.item.uom')
                ->label('UNIT OF MEASURE')
                ->enabledByDefault(false),
            ExportColumn::make('pump_serial_number')
                ->label('PUMP SERIAL NUMBER'),
            ExportColumn::make('correction_head')
                ->label('CORRECTION HEAD'),
            ExportColumn::make('sl_no')
                ->label('SL. NO.'),
            ExportColumn::make('voltage')
                ->label('VOLTAGE'),
            ExportColumn::make('frequency')
                ->label('FREQUENCY'),
            ExportColumn::make('speed')
                ->label('SPEED'),
            ExportColumn::make('head')
                ->label('HEAD'),
            ExportColumn::make('flow_reading')
                ->label('FLOW READING'),
            ExportColumn::make('current')
                ->label('CURRENT'),
            ExportColumn::make('watt')
                ->label('WATT'),
            ExportColumn::make('created_at')
                ->label('CREATED AT'),
            ExportColumn::make('created_by')
                ->label('CREATED BY'),
            ExportColumn::make('updated_at')
                ->label('UPDATED AT'),
            ExportColumn::make('updated_by')
                ->label('UPDATED BY'),
            ExportColumn::make('deleted_at')
                ->enabledByDefault(false)
                ->label('DELETED AT'),
        ];
    }

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