<?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('sales', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('tenant_id')->comment('테넌트 ID');
            $table->string('sale_number', 30)->comment('매출번호');
            $table->date('sale_date')->comment('매출일자');
            $table->unsignedBigInteger('client_id')->comment('거래처 ID');
            $table->decimal('supply_amount', 15, 2)->comment('공급가액');
            $table->decimal('tax_amount', 15, 2)->comment('세액');
            $table->decimal('total_amount', 15, 2)->comment('합계');
            $table->text('description')->nullable()->comment('적요');
            $table->string('status', 20)->default('draft')->comment('상태: draft/confirmed/invoiced');
            $table->unsignedBigInteger('tax_invoice_id')->nullable()->comment('세금계산서 ID');
            $table->unsignedBigInteger('deposit_id')->nullable()->comment('입금 연결 ID');
            $table->unsignedBigInteger('created_by')->nullable()->comment('생성자');
            $table->unsignedBigInteger('updated_by')->nullable()->comment('수정자');
            $table->unsignedBigInteger('deleted_by')->nullable()->comment('삭제자');
            $table->softDeletes();
            $table->timestamps();

            // 인덱스
            $table->unique(['tenant_id', 'sale_number'], 'uk_tenant_sale_number');
            $table->index(['tenant_id', 'sale_date'], 'idx_tenant_sale_date');
            $table->index('client_id', 'idx_client');
            $table->index('status', 'idx_status');
        });
    }

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