<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Support\Facades\DB;

/**
 * clients 테이블 is_active 컬럼을 CHAR(1) 'Y'/'N'에서 TINYINT(1) 0/1로 변환
 *
 * 프로젝트 표준:
 * - DB: tinyint(1) with 0/1
 * - Model: 'is_active' => 'boolean' cast
 * - API: true/false
 */
return new class extends Migration
{
    public function up(): void
    {
        // 1. 'Y' → 1, 'N' → 0 으로 데이터 변환 (임시로 숫자 문자열)
        DB::statement("UPDATE clients SET is_active = '1' WHERE is_active = 'Y'");
        DB::statement("UPDATE clients SET is_active = '0' WHERE is_active = 'N' OR is_active = '' OR is_active IS NULL");

        // 2. 컬럼 타입 변경: CHAR(1) → TINYINT(1)
        DB::statement("
            ALTER TABLE clients
            MODIFY COLUMN is_active TINYINT(1) NOT NULL DEFAULT 1 COMMENT '활성여부(1=활성,0=비활성)'
        ");
    }

    public function down(): void
    {
        // 1. 컬럼 타입 복구: TINYINT(1) → CHAR(1)
        DB::statement("
            ALTER TABLE clients
            MODIFY COLUMN is_active CHAR(1) NOT NULL DEFAULT 'Y' COMMENT '활성여부(Y/N)'
        ");

        // 2. 1 → 'Y', 0 → 'N' 으로 데이터 복구
        DB::statement("UPDATE clients SET is_active = 'Y' WHERE is_active = '1' OR is_active = 1");
        DB::statement("UPDATE clients SET is_active = 'N' WHERE is_active = '0' OR is_active = 0");
    }
};
