<?php

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

return new class extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('order_items', function (Blueprint $table) {
            $table->id();
            $table->string('shopify_gid', 100)->unique();
            $table->unsignedBigInteger('order_id');
            $table->unsignedBigInteger('product_id')->nullable();
            $table->unsignedBigInteger('variant_id')->nullable();

            $table->string('nav_no', 20);
            $table->smallInteger('pos');
            $table->decimal('quantity', 15, 2);
            $table->decimal('total', 15, 2);
            $table->decimal('discount', 15, 2)->default(0);
            $table->decimal('quantity_to_fulfill', 15, 2)->default(0);
            $table->decimal('fulfilled_quantity', 15, 2)->default(0);
            $table->decimal('quantity_to_refund', 15, 2)->default(0);
            $table->decimal('refunded_quantity', 15, 2)->default(0);
            $table->decimal('refunded_total', 15, 2)->default(0);
            $table->boolean('no_transaction')->default(0);

            $table->timestamps();

            $table->foreign('order_id')
                ->references('id')->on('orders')
                ->onDelete('CASCADE')->onUpdate('CASCADE');
            $table->foreign('product_id')
                ->references('id')->on('products')
                ->onDelete('SET NULL')->onUpdate('CASCADE');
            $table->foreign('variant_id')
                ->references('id')->on('variants')
                ->onDelete('SET NULL')->onUpdate('CASCADE');
        });
    }

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