<?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('invoice_items', function (Blueprint $table) {
            $table->id();
            $table->nullableMorphs('itemable');
            $table->foreignId('parent_id')->nullable();
            $table->unsignedBigInteger('invoice_id')->nullable();
            $table->unsignedBigInteger('price_group_id')->nullable();
            $table->unsignedBigInteger('organization_id')->nullable();
            $table->string('type')->nullable();
            $table->string('price_gross')->nullable();
            $table->string('discount_type')->nullable()->default('percentage');
            $table->string('discount_percentage')->nullable();
            $table->string('discount_value')->nullable();
            $table->string('tax_type')->nullable()->default('percentage');
            $table->string('tax_percentage')->nullable();
            $table->string('tax_value')->nullable();
            $table->string('price_net')->nullable()->comment("price_net = price_gross - discount_value + tax_value");
            $table->string('total_net')->nullable()->comment("total_net = price_net * quantity");
            $table->string('notes')->nullable();
            $table->timestamps();
            $table->softDeletes();
            $table->unsignedBigInteger('created_by')->nullable();
            $table->unsignedBigInteger('updated_by')->nullable();
            $table->unsignedBigInteger('deleted_by')->nullable();


            $table->index(['itemable_id', 'itemable_type']);
            $table->index('organization_id');
            $table->index('invoice_id');
            $table->index('price_group_id');
            $table->index('created_by');
            $table->index('updated_by');
            $table->index('deleted_by');
        });
    }

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