<?php

declare(strict_types=1);

namespace Database\Factories;

use App\Models\ReportJob;
use App\Models\Tenant;
use App\Models\User;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Carbon;

/**
 * @extends Factory<ReportJob>
 */
class ReportJobFactory extends Factory
{
    public function definition(): array
    {
        return [
            'tenant_id' => Tenant::factory(),
            'user_id' => User::factory()->state(fn (array $attrs, ReportJob $r) => ['tenant_id' => $r->tenant_id]),
            'type' => 'deals_export',
            'parameters' => [
                'format' => 'csv',
                'date_from' => Carbon::now()->subMonth()->toDateString(),
                'date_to' => Carbon::now()->toDateString(),
            ],
            'status' => ReportJob::STATUS_PENDING,
        ];
    }

    public function processing(): static
    {
        return $this->state(['status' => ReportJob::STATUS_PROCESSING]);
    }

    public function done(): static
    {
        return $this->state([
            'status' => ReportJob::STATUS_DONE,
            'file_path' => 'reports/1/123.csv',
            'file_size' => 1024,
            'generation_seconds' => 2,
            'finished_at' => Carbon::now(),
            'expires_at' => Carbon::now()->addDays(30),
        ]);
    }

    public function failed(): static
    {
        return $this->state([
            'status' => ReportJob::STATUS_FAILED,
            'error_message' => 'S3 timeout',
            'finished_at' => Carbon::now(),
        ]);
    }
}
