<?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_system_daily', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('tenant_id');
            $table->date('stat_date');

            // API 사용량
            $table->unsignedInteger('api_request_count')->default(0);
            $table->unsignedInteger('api_error_count')->default(0);
            $table->unsignedInteger('api_avg_response_ms')->default(0);

            // 사용자 활동
            $table->unsignedInteger('active_user_count')->default(0);
            $table->unsignedInteger('login_count')->default(0);

            // 감사
            $table->unsignedInteger('audit_create_count')->default(0);
            $table->unsignedInteger('audit_update_count')->default(0);
            $table->unsignedInteger('audit_delete_count')->default(0);

            // 알림
            $table->unsignedInteger('fcm_sent_count')->default(0);
            $table->unsignedInteger('fcm_failed_count')->default(0);

            // 파일
            $table->unsignedInteger('file_upload_count')->default(0);
            $table->decimal('file_upload_size_mb', 10, 2)->default(0);

            // 결재
            $table->unsignedInteger('approval_submitted_count')->default(0);
            $table->unsignedInteger('approval_completed_count')->default(0);
            $table->decimal('approval_avg_hours', 8, 2)->default(0);

            $table->timestamps();

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

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