"""
SALE_MAIN 테이블 컬럼 확인 스크립트
"""

import sys
import os
sys.path.insert(0, os.path.dirname(__file__))

from db.dbsetup import DatabaseManager
from sqlalchemy import text

def check_sale_table_columns(table_name):
    """테이블의 모든 컬럼 확인"""
    db_manager = DatabaseManager()

    try:
        session = db_manager.get_session('PM_PRES')

        # SQL Server에서 테이블 컬럼 정보 조회
        query = text(f"""
            SELECT
                COLUMN_NAME,
                DATA_TYPE,
                CHARACTER_MAXIMUM_LENGTH,
                IS_NULLABLE
            FROM INFORMATION_SCHEMA.COLUMNS
            WHERE TABLE_NAME = '{table_name}'
            ORDER BY ORDINAL_POSITION
        """)

        columns = session.execute(query).fetchall()

        print("=" * 80)
        print(f"{table_name} 테이블 컬럼 목록")
        print("=" * 80)

        for col in columns:
            nullable = "NULL" if col.IS_NULLABLE == 'YES' else "NOT NULL"
            max_len = f"({col.CHARACTER_MAXIMUM_LENGTH})" if col.CHARACTER_MAXIMUM_LENGTH else ""
            print(f"{col.COLUMN_NAME:30} {col.DATA_TYPE}{max_len:20} {nullable}")

        print("=" * 80)
        print(f"총 {len(columns)}개 컬럼")
        print("=" * 80)

    except Exception as e:
        print(f"오류 발생: {e}")
    finally:
        db_manager.close_all()

if __name__ == '__main__':
    check_sale_table_columns('SALE_MAIN')
    print("\n\n")
    check_sale_table_columns('SALE_SUB')
