<?php

declare(strict_types=1);

namespace Database\Factories;

use App\Models\OperationRun;
use App\Models\SupportRequest;
use App\Models\ManagedEnvironment;
use App\Models\User;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Str;

/**
 * @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\SupportRequest>
 */
class SupportRequestFactory extends Factory
{
    protected $model = SupportRequest::class;

    /**
     * Define the model's default state.
     *
     * @return array<string, mixed>
     */
    public function definition(): array
    {
        return [
            'managed_environment_id' => ManagedEnvironment::factory(),
            'initiated_by_user_id' => User::factory(),
            'internal_reference' => 'SR-'.strtoupper((string) Str::ulid()),
            'primary_context_type' => SupportRequest::PRIMARY_CONTEXT_TENANT,
            'attachment_mode' => SupportRequest::ATTACHMENT_MODE_DIAGNOSTIC_SNAPSHOT_ATTACHED,
            'severity' => SupportRequest::SEVERITY_NORMAL,
            'summary' => fake()->sentence(),
            'reproduction_notes' => fake()->optional()->paragraph(),
            'contact_name' => fake()->name(),
            'contact_email' => fake()->safeEmail(),
            'context_envelope' => [
                'schema_version' => 1,
                'generated_from' => 'factory',
                'attachment_mode' => SupportRequest::ATTACHMENT_MODE_DIAGNOSTIC_SNAPSHOT_ATTACHED,
                'primary_context' => [
                    'type' => SupportRequest::PRIMARY_CONTEXT_TENANT,
                ],
                'canonical_context' => [
                    'sections' => [],
                ],
                'diagnostic_snapshot' => [
                    'sections' => [],
                ],
                'omissions' => [],
            ],
            'external_handoff_mode' => SupportRequest::EXTERNAL_HANDOFF_MODE_INTERNAL_ONLY,
            'external_ticket_reference' => null,
            'external_ticket_url' => null,
            'external_handoff_failure_summary' => null,
        ];
    }

    public function canonicalContextOnly(): static
    {
        return $this->state(fn (array $attributes): array => [
            'attachment_mode' => SupportRequest::ATTACHMENT_MODE_CANONICAL_CONTEXT_ONLY,
            'context_envelope' => array_replace_recursive($attributes['context_envelope'] ?? [], [
                'attachment_mode' => SupportRequest::ATTACHMENT_MODE_CANONICAL_CONTEXT_ONLY,
                'diagnostic_snapshot' => null,
                'omissions' => [[
                    'type' => 'diagnostic_snapshot',
                    'reason' => 'omitted_without_support_diagnostics_view',
                ]],
            ]),
        ]);
    }

    public function forOperationRun(OperationRun $operationRun): static
    {
        return $this->state(fn (): array => [
            'managed_environment_id' => (int) $operationRun->managed_environment_id,
            'workspace_id' => (int) $operationRun->workspace_id,
            'operation_run_id' => (int) $operationRun->getKey(),
            'primary_context_type' => SupportRequest::PRIMARY_CONTEXT_OPERATION_RUN,
            'context_envelope' => [
                'schema_version' => 1,
                'generated_from' => 'factory',
                'attachment_mode' => SupportRequest::ATTACHMENT_MODE_DIAGNOSTIC_SNAPSHOT_ATTACHED,
                'primary_context' => [
                    'type' => SupportRequest::PRIMARY_CONTEXT_OPERATION_RUN,
                    'managed_environment_id' => (int) $operationRun->managed_environment_id,
                    'operation_run_id' => (int) $operationRun->getKey(),
                ],
                'canonical_context' => [
                    'sections' => [],
                ],
                'diagnostic_snapshot' => [
                    'sections' => [],
                ],
                'omissions' => [],
            ],
        ]);
    }
}
