<?php

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

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

        Schema::create('pc_product_barcodes', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->unsignedInteger('business_id');
            $table->unsignedInteger('product_id');
            $table->string('barcode', 64);
            $table->string('type', 16)->default('gtin'); // ean/upc/gtin

            $table->string('source', 32)->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', 'barcode']);
            $table->unique(['business_id', 'product_id', 'barcode'], 'pc_product_barcodes_unique');
        });
    }

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