<?php

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

return new class extends Migration
{
    public function up(): void
    {
        if (! Schema::hasTable('meta_campaign_safety_audits')) {
            Schema::create('meta_campaign_safety_audits', function (Blueprint $table) {
                $table->id();
                $table->string('status', 30)->default('processing')->index();
                $table->string('risk_audience_id', 100)->nullable();
                $table->string('risk_audience_name')->nullable();
                $table->string('purchaser_audience_id', 100)->nullable();
                $table->string('purchaser_audience_name')->nullable();
                $table->unsignedInteger('total_adsets')->default(0);
                $table->unsignedInteger('risk_compliant_count')->default(0);
                $table->unsignedInteger('risk_missing_count')->default(0);
                $table->unsignedInteger('purchaser_excluded_count')->default(0);
                $table->json('rows')->nullable();
                $table->unsignedSmallInteger('meta_http_status')->nullable();
                $table->string('meta_error_code', 50)->nullable();
                $table->string('meta_error_subcode', 50)->nullable();
                $table->string('meta_trace_id')->nullable();
                $table->text('error_message')->nullable();
                $table->timestamp('started_at')->nullable();
                $table->timestamp('finished_at')->nullable();
                $table->timestamps();
            });
        }
    }

    public function down(): void
    {
        Schema::dropIfExists('meta_campaign_safety_audits');
    }
};
