<?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('invoice_line_items', function (Blueprint $table) {
            $table->id();
            $table->foreignId('invoice_id')->constrained()->onDelete('cascade');

            // Line Item Details
            $table->string('type')->default('labour'); // labour, parts, miscellaneous
            $table->string('description');
            $table->decimal('quantity', 8, 2)->default(1);
            $table->decimal('unit_price', 10, 2);
            $table->decimal('total_amount', 10, 2);

            // Optional References
            $table->foreignId('part_id')->nullable()->constrained()->onDelete('set null');
            $table->foreignId('service_item_id')->nullable()->constrained()->onDelete('set null');
            $table->string('part_number')->nullable();
            $table->text('technical_notes')->nullable();

            $table->timestamps();

            // Indexes
            $table->index(['invoice_id', 'type']);
        });
    }

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