<?php

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

class CreatePcChangeReportsTable extends Migration
{
    public function up()
    {
        if (Schema::hasTable('pc_change_reports')) {
            return;
        }

        Schema::create('pc_change_reports', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->unsignedInteger('business_id');
            $table->unsignedInteger('product_id');
            // Groups all rows produced by a single parse/enrich run.
            $table->string('run_id')->index();
            $table->enum('action', ['parse', 'enrich'])->default('parse');

            // Detailed before/after snapshot per product (JSON).
            $table->json('old_data')->nullable();      // raw blobs read from custom fields
            $table->json('new_data')->nullable();      // structured values written
            $table->json('removed_data')->nullable();  // tokens flagged wrong / dropped
            $table->json('missing_data')->nullable();  // detected gaps (e.g. no part number)

            $table->timestamps();

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

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