<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        // Campaign/iklan (RPD §12.2). status: 0=Pending, 1=Aktif, 2=Batal, 3=Selesai, 4=Draft.
        Schema::create('ads', function (Blueprint $table) {
            $table->id();
            $table->string('judul');
            $table->text('url');
            $table->time('jam_tayang_mulai')->default('00:00:00');
            $table->time('jam_tayang_selesai')->default('23:59:59');
            $table->unsignedInteger('credit');              // alokasi credit (1 credit = 1 klik)
            $table->foreignId('kategori_id')->constrained('kategoris')->restrictOnDelete();
            $table->unsignedInteger('klik')->default(0);    // jumlah klik nyata tercatat
            $table->unsignedInteger('muncul')->default(0);  // counter tayang
            $table->foreignId('user_id')->constrained()->cascadeOnDelete(); // iduser
            $table->unsignedTinyInteger('status')->default(0); // BR-03
            $table->timestamp('tgl_aktif')->nullable();     // diisi saat approve
            $table->timestamps();

            // Algoritma rotator (FR-ROT-02): status + jam tayang + klik<credit + kategori, urut klik ASC.
            $table->index(['status', 'kategori_id', 'klik']);
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('ads');
    }
};
