<?php

namespace App\Filament\Exports;

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

class MfmParameterExporter extends Exporter
{
    protected static ?string $model = MfmParameter::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('deviceName.name')
                ->label('Device Name'),
            ExportColumn::make('name')
                ->label('PARAMETER NAME'),
            ExportColumn::make('mfmMeter.name')
                ->label('MFM METER'),
            ExportColumn::make('register_id')
                ->label('REGISTER ID'),
            ExportColumn::make('identifier')
                ->label('IDENTIFIER'),
            ExportColumn::make('byte_to_convert')
                ->label('BYTE TO CONVERT'),
            ExportColumn::make('type_to_convert')
                ->label('TYPE TO CONVERT'),
            ExportColumn::make('decimal_to_display')
                ->label('DECIMAL TO DISPLAY'),
            ExportColumn::make('created_at')
                ->label('CREATED AT'),
            ExportColumn::make('updated_at')
                ->label('UPDATED AT'),
            ExportColumn::make('deleted_at')
                ->label('DELETED AT')
                ->enabledByDefault(false),

        ];
    }

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