<?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('orders', function (Blueprint $table) {
            $table->id();
            $table->string('invoice_number')->unique();
            $table->string('customer_name');
            $table->string('customer_phone')->nullable();

            // Data untuk SPK
            $table->string('file_name');
            $table->string('file_path')->nullable();
            $table->string('size');
            $table->string('material')->nullable();
            $table->integer('qty');
            $table->text('finishing_note')->nullable();

            // Keuangan
            $table->decimal('unit_price', 15, 2)->default(0);
            $table->decimal('total_price', 15, 2);
            $table->decimal('paid_amount', 15, 2)->default(0);
            $table->decimal('change_amount', 15, 2)->default(0);
            $table->enum('payment_status', ['unpaid', 'paid'])->default('unpaid');
            $table->timestamp('paid_at')->nullable();

            // Status Produksi
            $table->enum('production_status', ['pending', 'proses_cetak', 'finishing', 'selesai'])->default('pending');
            $table->timestamp('started_at')->nullable();
            $table->timestamp('finished_at')->nullable();

            // Relasi User
            $table->foreignId('designer_id')->constrained('users')->onDelete('cascade');
            $table->foreignId('cashier_id')->nullable()->constrained('users')->onDelete('set null');
            $table->foreignId('operator_id')->nullable()->constrained('users')->onDelete('set null');

            // SPK Print tracking
            $table->boolean('spk_printed')->default(false);
            $table->timestamp('spk_printed_at')->nullable();

            $table->timestamps();

            // Indexes
            $table->index('payment_status');
            $table->index('production_status');
            $table->index('created_at');
        });
    }

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