<?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
    {
        Schema::table('serp_checks', function (Blueprint $t) {
            $t->string('execution_mode', 16)->default('live')->after('provider');
            $t->string('origin', 16)->default('manual')->after('execution_mode');
            $t->timestamp('provider_task_posted_at')->nullable()->after('provider_reference');
            $t->timestamp('provider_last_polled_at')->nullable()->after('provider_task_posted_at');
            $t->unsignedSmallInteger('provider_poll_attempts')->default(0)->after('provider_last_polled_at');
            $t->decimal('provider_cost', 12, 6)->nullable()->after('provider_poll_attempts');
            $t->char('provider_cost_currency', 3)->nullable()->after('provider_cost');
            $t->unsignedInteger('provider_status_code')->nullable()->after('provider_cost_currency');
            $t->unique(['provider', 'provider_reference'], 'serp_checks_provider_reference_unique');
        });
    }

    public function down(): void
    {
        Schema::table('serp_checks', function (Blueprint $t) {
            $t->dropUnique('serp_checks_provider_reference_unique');
            $t->dropColumn(['execution_mode', 'origin', 'provider_task_posted_at', 'provider_last_polled_at', 'provider_poll_attempts', 'provider_cost', 'provider_cost_currency', 'provider_status_code']);
        });
    }
};
