<?php

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

/**
 * Phase 2 (enrichment) audit table — created now so lineage is in place. The
 * enrichment pipeline that writes to it is built in a later phase.
 */
class CreatePcEnrichmentLogTable extends Migration
{
    public function up()
    {
        if (Schema::hasTable('pc_enrichment_log')) {
            return;
        }

        Schema::create('pc_enrichment_log', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->unsignedInteger('business_id');
            $table->unsignedInteger('product_id');
            $table->string('provider')->nullable();   // official | trusted | api driver key
            $table->string('status')->default('pending');
            $table->json('raw_payload')->nullable();   // exact provider response for audit
            $table->json('candidates')->nullable();    // extracted candidate values
            $table->text('message')->nullable();
            $table->timestamps();

            $table->index(['business_id', 'product_id']);
        });
    }

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