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

            $table->unsignedInteger('wo_total_count')->default(0);
            $table->unsignedInteger('wo_completed_count')->default(0);
            $table->decimal('production_qty', 18, 2)->default(0);
            $table->decimal('defect_qty', 18, 2)->default(0);
            $table->decimal('avg_defect_rate', 5, 2)->default(0);
            $table->decimal('avg_efficiency_rate', 5, 2)->default(0);
            $table->decimal('avg_delivery_rate', 5, 2)->default(0);
            $table->decimal('total_planned_hours', 10, 2)->default(0);
            $table->decimal('total_actual_hours', 10, 2)->default(0);
            $table->unsignedInteger('issue_total_count')->default(0);

            $table->timestamps();

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

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