<?php

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

return new class extends Migration
{
    /**
     * Run the migrations.
     *
     * Tabel ip_history (RPD §15.1): tidak ada di dump asli, wajib dibuat manual.
     * Menunjang anti-click-fraud: 1 IP = maks 1 klik per iklan per hari (BR-08).
     */
    public function up(): void
    {
        Schema::create('ip_histories', function (Blueprint $table) {
            $table->id();
            $table->string('ip', 45);
            $table->foreignId('ad_id')->constrained('ads')->cascadeOnDelete(); // idads
            $table->string('browser', 255)->nullable();
            $table->date('tanggal');
            $table->timestamps();

            // Cek keunikan IP+iklan+hari: dipakai rotator untuk menolak klik ganda.
            $table->index(['ad_id', 'ip', 'tanggal']);
        });
    }

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