<?php

namespace App\Filament\Exports;

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

class BeforeTestReadingExporter extends Exporter
{
    protected static ?string $model = BeforeTestReading::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.code')
                ->label('PLANT CODE'),
            ExportColumn::make('line.name')
                ->label('LINE NAME'),
            ExportColumn::make('machine.name')
                ->label('MACHINE NAME'),
            ExportColumn::make('motorTestingMaster.item.code')
                ->label('ITEM CODE'),
            ExportColumn::make('motorTestingMaster.item.description')
                ->label('MODEL DESCRIPTION'),
            ExportColumn::make('serial_number')
                ->label('SERIAL NUMBER'),
            ExportColumn::make('motorTestingMaster.kw')
                ->label('KW'),
            ExportColumn::make('motorTestingMaster.hp')
                ->label('HP'),
            ExportColumn::make('motorTestingMaster.phase')
                ->label('PHASE'),
            ExportColumn::make('motorTestingMaster.connection')
                ->label('CONNECTION'),
            ExportColumn::make('motorTestingMaster.isi_model')
                ->label('ISI MODEL'),
            ExportColumn::make('before_fr_res_ry')
                ->label('BEFORE FR RESISTANCE RY'),
            ExportColumn::make('before_fr_res_yb')
                ->label('BEFORE FR RESISTANCE YB'),
            ExportColumn::make('before_fr_res_br')
                ->label('BEFORE FR RESISTANCE BR'),
            ExportColumn::make('before_fr_ir')
                ->label('BEFORE FR IR'),
            ExportColumn::make('tested_by')
                ->label('TESTED BY'),
            ExportColumn::make('updated_by')
                ->label('UPDATED BY'),
            ExportColumn::make('created_at')
                ->label('CREATED AT'),
            ExportColumn::make('updated_at')
                ->label('UPDATED AT'),
            ExportColumn::make('scanned_at')
                ->label('SCANNED AT'),
            ExportColumn::make('deleted_at')
                ->enabledByDefault(false)
                ->label('DELETED AT'),
        ];
    }

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