<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    public function up()
    {
        Schema::create('sales', function (Blueprint $table) {
            $table->id();
            $table->string('invoice_no')->unique();
            $table->foreignId('customer_id')->constrained()->onDelete('restrict');
            $table->foreignId('user_id')->constrained()->onDelete('restrict');
            $table->enum('sale_type', ['cash', 'installment']);
            $table->decimal('total_amount', 12, 2)->default(0);
            $table->decimal('discount_amount', 12, 2)->default(0);
            $table->decimal('vat_amount', 12, 2)->default(0);
            $table->decimal('net_amount', 12, 2)->default(0);
            $table->decimal('paid_amount', 12, 2)->default(0);
            $table->decimal('due_amount', 12, 2)->default(0);
            $table->decimal('down_payment', 12, 2)->nullable();
            $table->integer('installment_months')->nullable();
            $table->decimal('monthly_installment', 12, 2)->nullable();
            $table->decimal('interest_rate', 5, 2)->default(0);
            $table->enum('status', ['pending', 'processing', 'completed', 'cancelled', 'delivered'])->default('pending');
            $table->enum('delivery_status', ['pending', 'processing', 'delivered', 'returned'])->default('pending');
            $table->enum('payment_status', ['paid', 'partial', 'due', 'overdue', 'cancelled'])->default('due');
            $table->date('sale_date');
            $table->date('delivery_date')->nullable();
            $table->date('warranty_start_date')->nullable();
            $table->date('warranty_end_date')->nullable();
            $table->text('notes')->nullable();
            $table->timestamps();
            $table->softDeletes();
            
            $table->index('invoice_no');
            $table->index('customer_id');
            $table->index('user_id');
            $table->index('sale_type');
            $table->index('status');
            $table->index('sale_date');
            $table->index('payment_status');
        });
    }

    public function down()
    {
        Schema::dropIfExists('sales');
    }
};