<?php

namespace Database\Factories;

use App\Enums\CampaignStatus;
use App\Models\Ad;
use App\Models\Kategori;
use App\Models\User;
use Illuminate\Database\Eloquent\Factories\Factory;

/**
 * @extends Factory<Ad>
 */
class AdFactory extends Factory
{
    public function definition(): array
    {
        return [
            'judul' => fake()->sentence(3),
            'url' => fake()->url(),
            'jam_tayang_mulai' => '00:00:00',
            'jam_tayang_selesai' => '23:59:59',
            'credit' => 0,
            'budget_harian' => fake()->numberBetween(10000, 1000000),
            'klik_hari_ini' => 0,
            'klik_tanggal' => null,
            'rollover_klik' => 0,
            'pause_reason' => null,
            'klik' => 0,
            'muncul' => 0,
            'user_id' => User::factory(),
            'status' => CampaignStatus::Pending,
            'tgl_aktif' => null,
        ];
    }

    /**
     * Attach satu kategori (default) setelah ad dibuat bila belum ada —
     * agar rotator/approve test jalan tanpa attach manual.
     */
    public function configure(): static
    {
        return $this->afterCreating(function (Ad $ad): void {
            if ($ad->kategoris()->exists()) {
                return;
            }
            $ad->kategoris()->sync(Kategori::factory()->create());
        });
    }

    public function active(): static
    {
        return $this->state(fn (array $attributes) => [
            'status' => CampaignStatus::Aktif,
            'tgl_aktif' => now(),
        ]);
    }

    public function draft(): static
    {
        return $this->state(fn (array $attributes) => [
            'status' => CampaignStatus::Draft,
        ]);
    }
}
