<?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('order_items', function (Blueprint $table) {
            $table->id();
            $table->foreignId('order_id')->constrained()->onDelete('cascade');
            
            // Product Info
            $table->foreignId('product_id')->nullable()->constrained()->onDelete('set null');
            $table->string('product_name');            // Snapshot nama produk
            
            // Material/Bahan Info
            $table->foreignId('material_id')->nullable()->constrained()->onDelete('set null');
            $table->string('material_name')->nullable(); // Snapshot nama bahan
            
            // File Info (untuk SPK)
            $table->string('file_name');
            $table->string('file_path')->nullable();
            
            // Size & Quantity
            $table->string('size_description');        // "1.5 x 2 Meter", "A3", dll (display)
            $table->decimal('size_value', 10, 4)->default(1); // Nilai ukuran untuk kalkulasi (misal: 3.0 untuk 1.5x2m)
            $table->string('size_unit')->default('m2'); // m2, pcs, lembar
            $table->integer('qty')->default(1);
            
            // Pricing
            $table->decimal('base_price', 15, 2)->default(0);      // Harga dasar per unit
            $table->decimal('calculated_price', 15, 2)->default(0); // base_price × size_value
            $table->decimal('subtotal', 15, 2)->default(0);        // calculated_price × qty
            
            // Finishing
            $table->text('finishing_note')->nullable();
            
            $table->timestamps();
            
            $table->index('order_id');
        });
    }

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