<?php

namespace App\Filament\Exports;

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

class PumpTestingMasterExporter extends Exporter
{
    protected static ?string $model = PumpTestingMaster::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('item.code')
                ->label('PUMP CODE'),
            ExportColumn::make('item.category')
                ->label('PUMP CATEGORY')
                ->enabledByDefault(false),
            ExportColumn::make('item.description')
                ->label('PUMP TYPE'),
            ExportColumn::make('item.uom')
                ->label('UNIT OF MEASURE')
                ->enabledByDefault(false),
            ExportColumn::make('head')
                ->label('HEAD'),
            ExportColumn::make('head_type')
                ->label('HEAD TYPE'),
            ExportColumn::make('discharge')
                ->label('DISCHARGE'),
            ExportColumn::make('discharge_type')
                ->label('DISCHARGE TYPE'),
            ExportColumn::make('motor_efficiency')
                ->label('MOTOR EFFICIENCY'),
            ExportColumn::make('pump_efficiency')
                ->label('PUMP EFFICIENCY'),
            ExportColumn::make('speed')
                ->label('SPEED'),
            ExportColumn::make('frequency')
                ->label('FREQUENCY'),
            ExportColumn::make('i_max')
                ->label('I-MAX'),
            ExportColumn::make('p_input')
                ->label('P-INPUT'),
            ExportColumn::make('delivery_size')
                ->label('DELIVERY SIZE'),
            ExportColumn::make('no_of_stages')
                ->label('NO OF STAGES'),
            ExportColumn::make('size')
                ->label('SIZE'),
            ExportColumn::make('maximum_head')
                ->label('MAXIMUM HEAD'),
            ExportColumn::make('motor_type')
                ->label('MOTOR TYPE'),
            ExportColumn::make('motor_code')
                ->label('MOTOR CODE'),
            ExportColumn::make('kw_hp')
                ->label('KW / HP'),
            ExportColumn::make('connection_type')
                ->label('CONNECTION TYPE'),
            ExportColumn::make('voltage')
                ->label('VOLTAGE'),
            ExportColumn::make('phase')
                ->label('PHASE'),
            ExportColumn::make('oh_minimum')
                ->label('OH MINIMUM'),
            ExportColumn::make('oh_maximum')
                ->label('OH MAXIMUM'),
            ExportColumn::make('current_minimum')
                ->label('CURRENT MINIMUM'),
            ExportColumn::make('current_maximum')
                ->label('CURRENT MAXIMUM'),
            ExportColumn::make('class_of_insulation')
                ->label('CLASS OF INSULATION'),
            ExportColumn::make('testing_code')
                ->label('TESTING CODE'),
            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 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;
    }
}
