<?php

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

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

        Schema::create('pc_compatibles', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->unsignedInteger('business_id');
            $table->unsignedInteger('product_id');
            $table->unsignedInteger('compatible_product_id');
            $table->enum('relation_type', ['compatible', 'replacement', 'accessory'])->default('compatible');

            $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', 'product_id']);
            $table->unique(['product_id', 'compatible_product_id', 'relation_type'], 'pc_compatibles_unique');
        });
    }

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