<?php

namespace App\Filament\Exports;

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

class WindedSerialValidationErrorExporter extends Exporter
{
    protected static ?string $model = WindedSerialValidationError::class;

    public static function getColumns(): array
    {
        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('ITEM CODE'),
            ExportColumn::make('item.description')
                ->label('DESCRIPTION')
                ->enabledByDefault(false),
            ExportColumn::make('item.category')
                ->label('CATEGORY')
                ->enabledByDefault(false),
            ExportColumn::make('item.uom')
                ->label('UNIT OF MEASURE')
                ->enabledByDefault(false),
            ExportColumn::make('machine.work_center')
                ->label('WORK CENTER'),
            ExportColumn::make('machine_name')
                ->label('MACHINE NAME'),
            ExportColumn::make('aufnr')
                ->label('AUFNR'),
            ExportColumn::make('gernr')
                ->label('GERNR'),
            ExportColumn::make('model_type')
                ->label('MODEL TYPE'),
            ExportColumn::make('winded_status')
                ->label('WINDED STATUS'),
            ExportColumn::make('winded_qr')
                ->label('WINDED QR'),
            ExportColumn::make('winded_code')
                ->label('WINDED CODE'),
            ExportColumn::make('winded_serial_number')
                ->label('WINDED SERIAL NUMBER'),
            ExportColumn::make('created_at')
                ->label('CREATED AT'),
            ExportColumn::make('created_by')
                ->label('CREATED BY'),
            ExportColumn::make('updated_at')
                ->label('UPDATED AT')
                ->enabledByDefault(true),
            ExportColumn::make('updated_by')
                ->label('UPDATED BY')
                ->enabledByDefault(true),
            ExportColumn::make('deleted_at')
                ->label('DELETED AT')
                ->enabledByDefault(false),
        ];
    }

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