<?php

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

return new class extends Migration
{
    protected $connection = 'sam_stat';

    public function up(): void
    {
        Schema::connection($this->connection)->create('stat_sales_daily', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('tenant_id');
            $table->date('stat_date');

            // 수주
            $table->unsignedInteger('order_count')->default(0)->comment('신규 수주 건수');
            $table->decimal('order_amount', 18, 2)->default(0)->comment('수주 금액');
            $table->unsignedInteger('order_item_count')->default(0)->comment('수주 품목 수');

            // 매출
            $table->unsignedInteger('sales_count')->default(0)->comment('매출 건수');
            $table->decimal('sales_amount', 18, 2)->default(0)->comment('매출 금액');
            $table->decimal('sales_tax_amount', 18, 2)->default(0)->comment('세액');

            // 고객
            $table->unsignedInteger('new_client_count')->default(0)->comment('신규 고객 수');
            $table->unsignedInteger('active_client_count')->default(0)->comment('활성 고객 수');

            // 수주 상태별 건수
            $table->unsignedInteger('order_draft_count')->default(0);
            $table->unsignedInteger('order_confirmed_count')->default(0);
            $table->unsignedInteger('order_in_progress_count')->default(0);
            $table->unsignedInteger('order_completed_count')->default(0);
            $table->unsignedInteger('order_cancelled_count')->default(0);

            // 출하
            $table->unsignedInteger('shipment_count')->default(0);
            $table->decimal('shipment_amount', 18, 2)->default(0);

            $table->timestamps();

            $table->unique(['tenant_id', 'stat_date'], 'uk_tenant_date');
            $table->index('stat_date');
            $table->index('tenant_id');
        });
    }

    public function down(): void
    {
        Schema::connection($this->connection)->dropIfExists('stat_sales_daily');
    }
};
