<?php

namespace App\Filament\Exports;

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

class CharacteristicApproverMasterExporter extends Exporter
{
    protected static ?string $model = CharacteristicApproverMaster::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('machine.work_center')
                ->label('WORK CENTER'),
            ExportColumn::make('approver_type')
                ->label('APPROVER TYPE'),
            ExportColumn::make('machine_name')
                ->label('MACHINE NAME'),
            ExportColumn::make('characteristic_field')
                ->label('MASTER CHARACTERISTIC FIELD'),
            ExportColumn::make('name1')
                ->label('APPROVER NAME 1'),
            ExportColumn::make('mail1')
                ->label('MAIL 1'),
            ExportColumn::make('duration1')
                ->label('DURATION 1'),
            ExportColumn::make('name2')
                ->label('APPROVER NAME 2'),
            ExportColumn::make('mail2')
                ->label('MAIL 2'),
            ExportColumn::make('duration2')
                ->label('DURATION 2'),
            ExportColumn::make('name3')
                ->label('APPROVER NAME 3'),
            ExportColumn::make('mail3')
                ->label('MAIL 3'),
            ExportColumn::make('duration3')
                ->label('DURATION 3'),
            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 characteristic approver 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;
    }
}
