<?php

namespace App\Filament\Exports;

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

class RequestCharacteristicExporter extends Exporter
{
    protected static ?string $model = RequestCharacteristic::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('work_flow_id')
                ->label('WORK FLOW ID'),
            ExportColumn::make('characteristicApproverMaster.machine_name')
                ->label('MACHINE NAME'),
            ExportColumn::make('item.code')
                ->label('ITEM CODE'),
            ExportColumn::make('aufnr')
                ->label('JOB NUMBER'),
            ExportColumn::make('characteristicApproverMaster.approver_type')
                ->label('APPROVER TYPE'),
            ExportColumn::make('characteristicApproverMaster.characteristic_field')
                ->label('MASTER CHARACTERISTIC FIELD'),
            ExportColumn::make('characteristic_name')
                ->label('CHARACTERISTIC NAME'),
            ExportColumn::make('current_value')
                ->label('CURRENT VALUE'),
            ExportColumn::make('update_value')
                ->label('UPDATE VALUE'),
            ExportColumn::make('characteristicApproverMaster.name1')
                ->label('APPROVER NAME 1'),
            ExportColumn::make('approver_status1')
                ->label('APPROVER STATUS 1'),
            ExportColumn::make('approver_remark1')
                ->label('APPROVER REMARK 1'),
            ExportColumn::make('approved1_at')
                ->label('APPROVED AT 1'),
            ExportColumn::make('characteristicApproverMaster.name2')
                ->label('APPROVER NAME 2'),
            ExportColumn::make('approver_status2')
                ->label('APPROVER STATUS 2'),
            ExportColumn::make('approver_remark2')
                ->label('APPROVER REMARK 2'),
            ExportColumn::make('approved2_at')
                ->label('APPROVED AT 2'),
            ExportColumn::make('characteristicApproverMaster.name3')
                ->label('APPROVER NAME 3'),
            ExportColumn::make('approver_status3')
                ->label('APPROVER STATUS 3'),
            ExportColumn::make('approver_remark3')
                ->label('APPROVER REMARK 3'),
            ExportColumn::make('approved3_at')
                ->label('APPROVED AT 1'),
            ExportColumn::make('mail_status')
                ->label('MAIL STATUS')
                ->formatStateUsing(function ($state) {
                    if (! $state || $state == '' || $state == null) {
                        return '-';
                    }

                    return match ($state) {
                        'Sent' => 'Sent-M1',
                        'Sent-Mail2' => 'Sent-M2',
                        'Sent-Mail3' => 'Sent-M3',
                        default => '-',
                    };
                }),
            ExportColumn::make('trigger_at')
                ->label('TRIGGERED AT'),
            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 request characteristic 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;
    }
}
