<?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_order_attributions')) {
            Schema::create('meta_order_attributions', function (Blueprint $table) {
                $table->id();
                $table->unsignedBigInteger('order_id')->unique();
                $table->string('campaign_id', 100)->nullable()->index();
                $table->string('adset_id', 100)->nullable()->index();
                $table->string('ad_id', 100)->nullable()->index();
                $table->string('product_id', 100)->nullable()->index();
                $table->string('utm_source', 100)->nullable();
                $table->string('utm_medium', 100)->nullable();
                $table->string('utm_campaign', 191)->nullable();
                $table->string('utm_content', 191)->nullable();
                $table->string('utm_term', 191)->nullable();
                $table->string('fbclid', 500)->nullable();
                $table->string('fbc', 255)->nullable();
                $table->string('fbp', 255)->nullable();
                $table->text('landing_url')->nullable();
                $table->unsignedTinyInteger('attribution_window_days')->default(7);
                $table->timestamp('attributed_at')->nullable()->index();
                $table->timestamps();

                $table->index(['campaign_id', 'attributed_at'], 'meta_attr_campaign_date_idx');
                $table->index(['ad_id', 'attributed_at'], 'meta_attr_ad_date_idx');
            });
        }

        if (! Schema::hasTable('meta_ads_insights')) {
            Schema::create('meta_ads_insights', function (Blueprint $table) {
                $table->id();
                $table->char('identity_hash', 64)->unique();
                $table->date('insight_date')->index();
                $table->string('level', 30)->default('ad');
                $table->string('campaign_id', 100)->nullable()->index();
                $table->string('campaign_name')->nullable();
                $table->string('adset_id', 100)->nullable()->index();
                $table->string('adset_name')->nullable();
                $table->string('ad_id', 100)->nullable()->index();
                $table->string('ad_name')->nullable();
                $table->string('product_id', 100)->nullable()->index();
                $table->unsignedBigInteger('impressions')->default(0);
                $table->unsignedBigInteger('clicks')->default(0);
                $table->decimal('add_to_cart', 24, 4)->default(0);
                $table->decimal('purchases', 24, 4)->default(0);
                $table->decimal('spend', 24, 8)->default(0);
                $table->decimal('purchase_value', 24, 8)->default(0);
                $table->string('currency', 10)->nullable();
                $table->string('attribution_setting', 100)->nullable();
                $table->json('raw_response')->nullable();
                $table->timestamp('synced_at')->nullable();
                $table->timestamps();

                $table->index(['insight_date', 'campaign_id'], 'meta_insight_date_campaign_idx');
                $table->index(['insight_date', 'product_id'], 'meta_insight_date_product_idx');
            });
        }

        if (! Schema::hasTable('meta_ads_sync_logs')) {
            Schema::create('meta_ads_sync_logs', function (Blueprint $table) {
                $table->id();
                $table->date('date_from');
                $table->date('date_to');
                $table->string('status', 30)->default('processing')->index();
                $table->unsignedInteger('base_rows')->default(0);
                $table->unsignedInteger('product_rows')->default(0);
                $table->json('warnings')->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_ads_sync_logs');
        Schema::dropIfExists('meta_ads_insights');
        Schema::dropIfExists('meta_order_attributions');
    }
};
