<?php

namespace App\Filament\Exports;

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

class StickerMappingMasterExporter extends Exporter
{
    protected static ?string $model = StickerMappingMaster::class;

    public static function getColumns(): array
    {
        static $rowNumber = 0;
        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('itemCharacteristic.item.code')
            ->label('ITEM CODE'),
            ExportColumn::make('sticker_structure1_id')
            ->label('STICKER 1')
            ->getStateUsing(function ($record) {
                return $record->sticker1Structure?->sticker_id ?? '-';
            }),
            ExportColumn::make('sticker1_machine_id')
            ->label('WC STICKER 1')
            ->getStateUsing(function ($record) {
                return $record->sticker1Machine?->work_center ?? '-';
            }),
            ExportColumn::make('sticker1_print_ip')
            ->label('STICKER 1 PRINT IP'),
            ExportColumn::make('sticker_structure2_id')
            ->label('STICKER 2')
            ->getStateUsing(function ($record) {
                return $record->sticker2Structure?->sticker_id ?? '-';
            }),
            ExportColumn::make('sticker2_machine_id')
            ->label('WC STICKER 2')
            ->getStateUsing(function ($record) {
                return $record->sticker2Machine?->work_center ?? '-';
            }),
            ExportColumn::make('sticker2_print_ip')
            ->label('STICKER 2 PRINT IP'),
            ExportColumn::make('sticker_structure3_id')
            ->label('STICKER 3')
            ->getStateUsing(function ($record) {
                return $record->sticker3Structure?->sticker_id ?? '-';
            }),
            ExportColumn::make('sticker3_machine_id')
            ->label('WC STICKER 3')
            ->getStateUsing(function ($record) {
                return $record->sticker3Machine?->work_center ?? '-';
            }),
            ExportColumn::make('sticker3_print_ip')
            ->label('STICKER 3 PRINT IP'),
            ExportColumn::make('sticker_structure4_id')
            ->label('STICKER 4')
            ->getStateUsing(function ($record) {
                return $record->sticker4Structure?->sticker_id ?? '-';
            }),
            ExportColumn::make('sticker4_machine_id')
            ->label('WC STICKER 4')
            ->getStateUsing(function ($record) {
                return $record->sticker4Machine?->work_center ?? '-';
            }),
            ExportColumn::make('sticker4_print_ip')
            ->label('STICKER 4 PRINT IP'),
            ExportColumn::make('sticker_structure5_id')
            ->label('STICKER 5')
            ->getStateUsing(function ($record) {
                return $record->sticker5Structure?->sticker_id ?? '-';
            }),
            ExportColumn::make('sticker5_machine_id')
            ->label('WC STICKER 5')
            ->getStateUsing(function ($record) {
                return $record->sticker5Machine?->work_center ?? '-';
            }),
            ExportColumn::make('sticker5_print_ip')
            ->label('STICKER 5 PRINT IP'),
            ExportColumn::make('sticker_structure6_id')
            ->label('STICKER 6')
            ->getStateUsing(function ($record) {
                return $record->sticker6Structure?->sticker_id ?? '-';
            }),
            ExportColumn::make('sticker6_machine_id')
            ->label('WC STICKER 6')
            ->getStateUsing(function ($record) {
                return $record->sticker6Machine?->work_center ?? '-';
            }),
            ExportColumn::make('sticker6_print_ip')
            ->label('STICKER 6 PRINT IP'),
            ExportColumn::make('sticker_structure7_id')
            ->label('STICKER 7')
            ->getStateUsing(function ($record) {
                return $record->sticker7Structure?->sticker_id ?? '-';
            }),
            ExportColumn::make('sticker7_machine_id')
            ->label('WC STICKER 7')
            ->getStateUsing(function ($record) {
                return $record->sticker2Machine?->work_center ?? '-';
            }),
            ExportColumn::make('sticker7_print_ip')
            ->label('STICKER 7 PRINT IP'),
            ExportColumn::make('sticker_structure8_id')
            ->label('STICKER 8')
            ->getStateUsing(function ($record) {
                return $record->sticker8Structure?->sticker_id ?? '-';
            }),
            ExportColumn::make('sticker8_machine_id')
            ->label('WC STICKER 8')
            ->getStateUsing(function ($record) {
                return $record->sticker8Machine?->work_center ?? '-';
            }),
            ExportColumn::make('sticker8_print_ip')
            ->label('STICKER 8 PRINT IP'),
            ExportColumn::make('created_at')
            ->label('CREATED AT'),
            ExportColumn::make('updated_at')
            ->label('UPDATED AT'),
            ExportColumn::make('created_by')
            ->label('CREATED BY'),
            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 sticker mapping 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;
    }
}
