import pymysql
import os

def get_db():
    return pymysql.connect(
        host='localhost', user='root', password='45278434',
        database=os.getenv('DB_NAME', 'PM_proto'), charset='utf8mb4',
        cursorclass=pymysql.cursors.DictCursor
    )

def migrate_to_normalized_tables():
    conn = get_db()
    try:
        with conn.cursor() as cursor:
            # 1. 마스터 테이블 생성 (고유 정보)
            cursor.execute("""
                CREATE TABLE IF NOT EXISTS projects_master (
                    project_id VARCHAR(100) PRIMARY KEY,
                    project_nm VARCHAR(255) NOT NULL,
                    short_nm VARCHAR(255),
                    department VARCHAR(255),
                    continent VARCHAR(100),
                    country VARCHAR(100),
                    master VARCHAR(100),
                    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
                ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
            """)

            # 2. 히스토리 테이블 생성 (일일 변동 정보)
            cursor.execute("""
                CREATE TABLE IF NOT EXISTS projects_history (
                    id INT AUTO_INCREMENT PRIMARY KEY,
                    project_id VARCHAR(100) NOT NULL,
                    crawl_date DATE NOT NULL,
                    recent_log VARCHAR(255),
                    file_count INT DEFAULT 0,
                    recorded_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
                    UNIQUE KEY idx_proj_date (project_id, crawl_date),
                    FOREIGN KEY (project_id) REFERENCES projects_master(project_id) ON DELETE CASCADE
                ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
            """)

            # 3. 기존 데이터 이전
            # 3-1. 마스터 정보 이전
            cursor.execute("""
                INSERT IGNORE INTO projects_master (project_id, project_nm, short_nm, department, continent, country, master)
                SELECT project_id, project_nm, short_nm, department, continent, country, master
                FROM overseas_projects
            """)
            
            # 3-2. 히스토리 정보 이전
            cursor.execute("""
                INSERT IGNORE INTO projects_history (project_id, crawl_date, recent_log, file_count)
                SELECT project_id, crawl_date, recent_log, file_count
                FROM overseas_projects
            """)

            # 4. 기존 단일 테이블 삭제 (성공 후 삭제)
            # cursor.execute("DROP TABLE IF EXISTS overseas_projects") 
            
            conn.commit()
            print(">>> DB 정규화 마이그레이션 완료 (Master / History 분리)")
    finally:
        conn.close()

if __name__ == "__main__":
    migrate_to_normalized_tables()
