<?php

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

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

        Schema::create('lc_expense_distributions', function (Blueprint $table) {
            $table->increments('id');
            $table->unsignedInteger('lc_expense_id');
            $table->unsignedInteger('purchase_line_id');
            // product reference is informational only (pr_product or core product) — no FK
            $table->unsignedBigInteger('product_id')->nullable();
            $table->decimal('distribution_ratio', 10, 6)->default(0);
            $table->decimal('allocated_amount', 15, 4)->default(0);
            $table->decimal('allocated_base_amount', 15, 4)->default(0);
            $table->boolean('is_custom')->default(false);
            $table->timestamps();

            $table->foreign('lc_expense_id')
                  ->references('id')->on('lc_expenses')
                  ->onDelete('cascade');

            $table->foreign('purchase_line_id')
                  ->references('id')->on('purchase_lines')
                  ->onDelete('cascade');
        });
    }

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