<?php

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

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('b2b_customer_requisition_list_products', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('requisition_list_id')->unsigned();
            $table->integer('product_id')->unsigned();
            $table->integer('variant_id')->unsigned()->nullable();
            $table->string('type');
            $table->string('sku');
            $table->string('name');
            $table->integer('qty')->default(1);
            $table->decimal('price', 18, 4)->default(0);
            $table->decimal('base_price', 18, 4)->default(0);
            $table->decimal('total', 18, 4)->default(0);
            $table->decimal('base_total', 18, 4)->default(0);
            $table->json('additional')->nullable();

            $table->foreign('requisition_list_id', 'b2b_req_list_prod_list_fk')->references('id')->on('b2b_customer_requisition_lists')->onDelete('cascade');
            $table->foreign('product_id', 'b2b_req_list_prod_product_fk')->references('id')->on('products')->onDelete('cascade');
            $table->foreign('variant_id', 'b2b_req_list_prod_variant_fk')->references('id')->on('products')->onDelete('set null');
        });
    }

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