<?php

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

return new class extends Migration
{
    /**
     * 계정과목 마스터 확장
     *
     * - sub_category: 중분류 (유동자산, 판관비, 매출원가 등)
     * - parent_code: 상위 계정과목 코드 (계층 구조)
     * - depth: 계층 깊이 (1=대, 2=중, 3=소)
     * - department_type: 부문 (common=공통, manufacturing=제조, admin=관리)
     * - description: 계정과목 설명
     */
    public function up(): void
    {
        Schema::table('account_codes', function (Blueprint $table) {
            $table->string('sub_category', 50)->nullable()->after('category')
                ->comment('중분류 (current_asset, fixed_asset, selling_admin, cogs 등)');
            $table->string('parent_code', 10)->nullable()->after('sub_category')
                ->comment('상위 계정과목 코드 (계층 구조)');
            $table->tinyInteger('depth')->default(3)->after('parent_code')
                ->comment('계층 깊이 (1=대분류, 2=중분류, 3=소분류)');
            $table->string('department_type', 20)->default('common')->after('depth')
                ->comment('부문 (common=공통, manufacturing=제조, admin=관리)');
            $table->string('description', 500)->nullable()->after('department_type')
                ->comment('계정과목 설명');

            $table->index(['tenant_id', 'category'], 'account_codes_tenant_category_idx');
            $table->index(['tenant_id', 'parent_code'], 'account_codes_tenant_parent_idx');
            $table->index(['tenant_id', 'depth'], 'account_codes_tenant_depth_idx');
        });
    }

    public function down(): void
    {
        Schema::table('account_codes', function (Blueprint $table) {
            $table->dropIndex('account_codes_tenant_category_idx');
            $table->dropIndex('account_codes_tenant_parent_idx');
            $table->dropIndex('account_codes_tenant_depth_idx');

            $table->dropColumn([
                'sub_category',
                'parent_code',
                'depth',
                'department_type',
                'description',
            ]);
        });
    }
};
