<?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('estimate_line_items', function (Blueprint $table) {
            $table->id();
            $table->foreignId('estimate_id')->constrained()->onDelete('cascade');
            $table->enum('type', ['labor', 'parts', 'miscellaneous']);
            $table->foreignId('part_id')->nullable()->constrained()->onDelete('set null');
            
            // Item details
            $table->text('description');
            $table->decimal('quantity', 8, 2)->default(1);
            $table->decimal('unit_price', 10, 2);
            $table->decimal('total_amount', 10, 2);
            
            // Labor specific fields
            $table->decimal('labor_hours', 8, 2)->nullable();
            $table->decimal('labor_rate', 10, 2)->nullable();
            
            // Pricing details
            $table->decimal('markup_percentage', 5, 2)->default(0);
            $table->text('notes')->nullable();
            $table->boolean('required')->default(true);
            $table->string('category', 100)->nullable();
            
            $table->timestamps();
            
            // Indexes
            $table->index(['estimate_id', 'type']);
        });
    }

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