<?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('credit_inquiries', function (Blueprint $table) {
            $table->id();
            $table->string('inquiry_key', 32)->unique()->comment('조회 고유 키');
            $table->string('company_key', 20)->index()->comment('사업자번호/법인번호');
            $table->string('company_name')->nullable()->comment('업체명');
            $table->unsignedBigInteger('user_id')->nullable()->comment('조회자 ID');
            $table->timestamp('inquired_at')->comment('조회 일시');

            // 요약 정보 (빠른 조회용)
            $table->unsignedInteger('short_term_overdue_cnt')->default(0)->comment('단기연체정보 건수');
            $table->unsignedInteger('negative_info_kci_cnt')->default(0)->comment('신용도판단정보(한국신용정보원) 건수');
            $table->unsignedInteger('negative_info_pb_cnt')->default(0)->comment('공공정보 건수');
            $table->unsignedInteger('negative_info_cb_cnt')->default(0)->comment('신용도판단정보(신용정보사) 건수');
            $table->unsignedInteger('suspension_info_cnt')->default(0)->comment('당좌거래정지정보 건수');
            $table->unsignedInteger('workout_cnt')->default(0)->comment('법정관리/워크아웃정보 건수');

            // API 응답 원본 데이터 (JSON)
            $table->json('raw_summary')->nullable()->comment('OA12 신용요약정보 원본');
            $table->json('raw_short_term_overdue')->nullable()->comment('OA13 단기연체정보 원본');
            $table->json('raw_negative_info_kci')->nullable()->comment('OA14 신용도판단정보(한국신용정보원) 원본');
            $table->json('raw_negative_info_cb')->nullable()->comment('OA15 신용도판단정보(신용정보사) 원본');
            $table->json('raw_suspension_info')->nullable()->comment('OA16 당좌거래정지정보 원본');
            $table->json('raw_workout_info')->nullable()->comment('OA17 법정관리/워크아웃정보 원본');

            // 상태
            $table->enum('status', ['success', 'partial', 'failed'])->default('success')->comment('조회 상태');
            $table->text('error_message')->nullable()->comment('에러 메시지');

            $table->timestamps();

            // 인덱스
            $table->index(['company_key', 'inquired_at']);
            $table->index('inquired_at');
        });
    }

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