<?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('customer_quality_metrics')
            && ! Schema::hasColumn('customer_quality_metrics', 'cancel_rate')) {
            Schema::table('customer_quality_metrics', function (Blueprint $table) {
                $table->decimal('cancel_rate', 8, 2)->default(0)->index()->after('return_rate');
            });
        }

        if (! Schema::hasTable('customer_quality_exclusions')) {
            Schema::create('customer_quality_exclusions', function (Blueprint $table) {
                $table->id();
                $table->string('metric_key', 100)->unique();
                $table->foreignId('user_id')->nullable()->constrained('users')->nullOnDelete();
                $table->boolean('is_excluded')->default(true)->index();
                $table->string('category', 40)->default('internal')->index();
                $table->string('reason')->nullable();
                $table->foreignId('created_by_admin_id')->nullable()->constrained('admins')->nullOnDelete();
                $table->foreignId('updated_by_admin_id')->nullable()->constrained('admins')->nullOnDelete();
                $table->timestamps();

                $table->index(['is_excluded', 'metric_key'], 'customer_quality_exclusions_active_metric_index');
            });
        }

        if (Schema::hasTable('customer_quality_sync_logs')) {
            Schema::table('customer_quality_sync_logs', function (Blueprint $table) {
                if (! Schema::hasColumn('customer_quality_sync_logs', 'meta_http_status')) {
                    $table->unsignedSmallInteger('meta_http_status')->nullable()->after('error_message');
                }
                if (! Schema::hasColumn('customer_quality_sync_logs', 'meta_error_code')) {
                    $table->string('meta_error_code', 40)->nullable()->after('meta_http_status');
                }
                if (! Schema::hasColumn('customer_quality_sync_logs', 'meta_error_subcode')) {
                    $table->string('meta_error_subcode', 40)->nullable()->after('meta_error_code');
                }
                if (! Schema::hasColumn('customer_quality_sync_logs', 'meta_trace_id')) {
                    $table->string('meta_trace_id', 191)->nullable()->index()->after('meta_error_subcode');
                }
            });
        }
    }

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

        if (Schema::hasTable('customer_quality_metrics')
            && Schema::hasColumn('customer_quality_metrics', 'cancel_rate')) {
            Schema::table('customer_quality_metrics', function (Blueprint $table) {
                $table->dropColumn('cancel_rate');
            });
        }

        if (Schema::hasTable('customer_quality_sync_logs')) {
            Schema::table('customer_quality_sync_logs', function (Blueprint $table) {
                foreach (['meta_http_status', 'meta_error_code', 'meta_error_subcode', 'meta_trace_id'] as $column) {
                    if (Schema::hasColumn('customer_quality_sync_logs', $column)) {
                        $table->dropColumn($column);
                    }
                }
            });
        }
    }
};
