<?php

namespace App\Filament\Exports;

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

class LineExporter extends Exporter
{
    protected static ?string $model = Line::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('name')
                ->label('LINE NAME'),
            ExportColumn::make('type')
                ->label('LINE TYPE'),
            ExportColumn::make('no_of_operation')
                ->label('NO OF OPERATION'),
            ExportColumn::make('workGroup1.name')
                ->label('WORK GROUP CENTER 1'),
            ExportColumn::make('workGroup1.operation_number')
                ->label('OPERATION NUMBER 1'),
            ExportColumn::make('workGroup2.name')
                ->label('WORK GROUP CENTER 2'),
            ExportColumn::make('workGroup2.operation_number')
                ->label('OPERATION NUMBER 2'),
            ExportColumn::make('workGroup3.name')
                ->label('WORK GROUP CENTER 3'),
            ExportColumn::make('workGroup3.operation_number')
                ->label('OPERATION NUMBER 3'),
            ExportColumn::make('workGroup4.name')
                ->label('WORK GROUP CENTER 4'),
            ExportColumn::make('workGroup4.operation_number')
                ->label('OPERATION NUMBER 4'),
            ExportColumn::make('workGroup5.name')
                ->label('WORK GROUP CENTER 5'),
            ExportColumn::make('workGroup5.operation_number')
                ->label('OPERATION NUMBER 5'),
            ExportColumn::make('workGroup6.name')
                ->label('WORK GROUP CENTER 6'),
            ExportColumn::make('workGroup6.operation_number')
                ->label('OPERATION NUMBER 6'),
            ExportColumn::make('workGroup7.name')
                ->label('WORK GROUP CENTER 7'),
            ExportColumn::make('workGroup7.operation_number')
                ->label('OPERATION NUMBER 7'),
            ExportColumn::make('workGroup8.name')
                ->label('WORK GROUP CENTER 8'),
            ExportColumn::make('workGroup8.operation_number')
                ->label('OPERATION NUMBER 8'),
            ExportColumn::make('workGroup9.name')
                ->label('WORK GROUP CENTER 9'),
            ExportColumn::make('workGroup9.operation_number')
                ->label('OPERATION NUMBER 9'),
            ExportColumn::make('workGroup10.name')
                ->label('WORK GROUP CENTER 10'),
            ExportColumn::make('workGroup10.operation_number')
                ->label('OPERATION NUMBER 10'),
            ExportColumn::make('created_at')
                ->label('CREATED AT'),
            ExportColumn::make('updated_at')
                ->label('UPDATED AT'),
            ExportColumn::make('deleted_at')
                ->enabledByDefault(false)
                ->label('DELETED AT'),
        ];
    }

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