<?php

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

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

        Schema::create('pc_part_numbers', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->unsignedInteger('business_id');
            $table->unsignedInteger('product_id');

            $table->string('part_number');
            // Uppercased, separators stripped — used for de-dup and cross-product search.
            $table->string('normalized')->index();
            $table->enum('type', ['mpn', 'oem', 'model_code', 'internal', 'other'])->default('other');
            $table->boolean('is_primary')->default(false);

            // Lineage trio (+ verification) — see plan.
            $table->enum('source', ['manual', 'parsed', 'official', 'trusted', 'api'])->default('manual');
            $table->unsignedTinyInteger('confidence')->default(100);
            $table->boolean('needs_review')->default(false);
            $table->timestamp('verified_at')->nullable();
            $table->unsignedInteger('verified_by')->nullable();

            $table->timestamps();

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

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