<?php

namespace App\Filament\Exports;

use App\Models\AuditLog;
use Filament\Actions\Exports\ExportColumn;
use Filament\Actions\Exports\Exporter;
use Filament\Actions\Exports\Models\Export;
use Illuminate\Support\Number;

/**
 * Columns mirror AuditLogResource::table() exactly.
 */
class AuditLogExporter extends Exporter
{
    protected static ?string $model = AuditLog::class;

    public static function getColumns(): array
    {
        return [
            ExportColumn::make('created_at')->label('When'),
            ExportColumn::make('actor.name')->label('Actor'),
            ExportColumn::make('actor.email')->label('Email'),
            ExportColumn::make('action'),
            ExportColumn::make('description'),
            ExportColumn::make('ip')->label('IP'),
            ExportColumn::make('subject_type')->label('Subject'),
        ];
    }

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