<?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('sam_stat')->create('stat_project_monthly', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('tenant_id');
            $table->smallInteger('stat_year');
            $table->tinyInteger('stat_month');

            // 프로젝트 현황
            $table->unsignedInteger('active_site_count')->default(0);
            $table->unsignedInteger('completed_site_count')->default(0);
            $table->unsignedInteger('new_contract_count')->default(0);
            $table->decimal('contract_total_amount', 18, 2)->default(0);

            // 원가
            $table->decimal('expected_expense_total', 18, 2)->default(0);
            $table->decimal('actual_expense_total', 18, 2)->default(0);
            $table->decimal('labor_cost_total', 18, 2)->default(0);
            $table->decimal('material_cost_total', 18, 2)->default(0);

            // 수익률
            $table->decimal('gross_profit', 18, 2)->default(0);
            $table->decimal('gross_profit_rate', 5, 2)->default(0);

            // 이슈
            $table->unsignedInteger('handover_report_count')->default(0);
            $table->unsignedInteger('structure_review_count')->default(0);

            $table->timestamps();

            $table->unique(['tenant_id', 'stat_year', 'stat_month'], 'uk_tenant_year_month');
            $table->index(['stat_year', 'stat_month'], 'idx_year_month');
        });
    }

    public function down(): void
    {
        Schema::connection('sam_stat')->dropIfExists('stat_project_monthly');
    }
};
