<?php

namespace Database\Factories;

use App\Enums\QrCodeType;
use App\Models\QrCode;
use Illuminate\Database\Eloquent\Factories\Factory;

/**
 * @extends Factory<QrCode>
 */
class QrCodeFactory extends Factory
{
    /**
     * Define the model's default state.
     *
     * @return array<string, mixed>
     */
    public function definition(): array
    {
        return [
            'short_code' => QrCode::generateShortCode(),
            'name' => fake()->words(3, true),
            'type' => QrCodeType::Url,
            'payload' => null,
            'target_url' => fake()->url(),
            'is_dynamic' => true,
            'is_active' => true,
            'scan_count' => 0,
            'last_scanned_at' => null,
        ];
    }

    /**
     * Indicate that the QR code is a dynamic URL redirect.
     */
    public function url(): static
    {
        return $this->state(fn (array $attributes) => [
            'type' => QrCodeType::Url,
            'payload' => null,
            'target_url' => fake()->url(),
            'is_dynamic' => true,
        ]);
    }

    /**
     * Indicate that the QR code is inactive and should stop redirecting.
     */
    public function inactive(): static
    {
        return $this->state(fn (array $attributes) => [
            'is_active' => false,
        ]);
    }

    /**
     * Indicate that the QR code is static, encoding its payload directly
     * with no redirect and no scan analytics.
     */
    public function static(): static
    {
        return $this->state(fn (array $attributes) => [
            'type' => QrCodeType::Text,
            'payload' => ['text' => fake()->sentence()],
            'target_url' => null,
            'is_dynamic' => false,
        ]);
    }
}
