<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use Modules\Core\Enums\StatusEnum;

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('product_unit_conversions', function (Blueprint $table) {
            $table->id();
            $table->foreignId('product_id')->constrained()->cascadeOnDelete();
            $table->foreignId('from_unit_id')->comment('Example:Box, Strip, Carton')->constrained('units');
            $table->decimal('from_qty', 20, 6)->default(1);
            $table->foreignId('to_unit_id')->comment('SHOULD always be base_unit_id of products table')->constrained('units');
            $table->decimal('to_qty', 20, 6);
            $table->boolean('is_purchase_unit')->default(false);
            $table->boolean('is_sale_unit')->default(false);
            $table->boolean('is_default_purchase')->default(false);
            $table->boolean('is_default_sale')->default(false);
            $table->integer('priority')->default(0);
            $table->enum('status', StatusEnum::casesAsArray())->default(StatusEnum::ACTIVE);

            $table->unique(['product_id', 'from_unit_id']);
            $table->index(['product_id', 'priority']);
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('product_unit_conversions');
    }
};
