#!/usr/bin/env python3
"""
누락된 테이블 수동 생성 스크립트
배포 시 자동으로 실행되도록 설계
"""

import os
import sys
import psycopg2
from psycopg2 import sql

# 현재 디렉토리를 Python 경로에 추가
sys.path.insert(0, '/app')

# 환경 변수 로드
DB_HOST = os.getenv("DB_HOST", "postgres")
DB_PORT = os.getenv("DB_PORT", "5432")
DB_NAME = os.getenv("DB_NAME", "tk_mp_bom")
DB_USER = os.getenv("DB_USER", "tkmp_user")
DB_PASSWORD = os.getenv("DB_PASSWORD", "tkmp_password_2025")

def create_missing_tables():
    """누락된 테이블들을 직접 생성"""
    print("🔧 누락된 테이블 생성 시작...")
    
    try:
        conn = psycopg2.connect(
            host=DB_HOST,
            port=DB_PORT,
            database=DB_NAME,
            user=DB_USER,
            password=DB_PASSWORD
        )
        
        with conn.cursor() as cursor:
            # purchase_requests 테이블 생성
            cursor.execute("""
                CREATE TABLE IF NOT EXISTS purchase_requests (
                    request_id VARCHAR(50) PRIMARY KEY,
                    request_no VARCHAR(100) NOT NULL,
                    file_id INTEGER REFERENCES files(id),
                    job_no VARCHAR(50) NOT NULL,
                    category VARCHAR(50),
                    material_count INTEGER,
                    excel_file_path VARCHAR(500),
                    requested_by INTEGER REFERENCES users(user_id),
                    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
                    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
                );
            """)
            print("✅ purchase_requests 테이블 생성 완료")
            
            # materials 테이블에 누락된 컬럼들 추가
            cursor.execute("ALTER TABLE materials ADD COLUMN IF NOT EXISTS brand VARCHAR(100);")
            cursor.execute("ALTER TABLE materials ADD COLUMN IF NOT EXISTS user_requirement TEXT;")
            cursor.execute("ALTER TABLE materials ADD COLUMN IF NOT EXISTS purchase_confirmed BOOLEAN DEFAULT FALSE;")
            cursor.execute("ALTER TABLE materials ADD COLUMN IF NOT EXISTS confirmed_quantity NUMERIC(10,3);")
            cursor.execute("ALTER TABLE materials ADD COLUMN IF NOT EXISTS purchase_status VARCHAR(20);")
            cursor.execute("ALTER TABLE materials ADD COLUMN IF NOT EXISTS purchase_confirmed_by VARCHAR(100);")
            cursor.execute("ALTER TABLE materials ADD COLUMN IF NOT EXISTS purchase_confirmed_at TIMESTAMP;")
            cursor.execute("ALTER TABLE materials ADD COLUMN IF NOT EXISTS material_hash VARCHAR(64);")
            cursor.execute("ALTER TABLE materials ADD COLUMN IF NOT EXISTS normalized_description TEXT;")
            cursor.execute("ALTER TABLE materials ADD COLUMN IF NOT EXISTS revision_status VARCHAR(20);")
            print("✅ materials 테이블 누락된 컬럼들 추가 완료")
            
            # 기타 누락될 수 있는 테이블들
            missing_tables = [
                """
                CREATE TABLE IF NOT EXISTS excel_exports (
                    id SERIAL PRIMARY KEY,
                    file_id INTEGER REFERENCES files(id),
                    export_type VARCHAR(50),
                    file_path VARCHAR(500),
                    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
                );
                """,
                """
                CREATE TABLE IF NOT EXISTS user_activity_logs (
                    id SERIAL PRIMARY KEY,
                    user_id INTEGER REFERENCES users(user_id),
                    activity_type VARCHAR(50),
                    description TEXT,
                    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
                );
                """,
                """
                CREATE TABLE IF NOT EXISTS excel_export_history (
                    id SERIAL PRIMARY KEY,
                    request_id VARCHAR(50) REFERENCES purchase_requests(request_id),
                    export_path VARCHAR(500),
                    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
                );
                """,
                """
                CREATE TABLE IF NOT EXISTS exported_materials (
                    id SERIAL PRIMARY KEY,
                    export_id INTEGER REFERENCES excel_exports(id),
                    material_id INTEGER REFERENCES materials(id),
                    quantity NUMERIC(10, 3),
                    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
                );
                """,
                """
                CREATE TABLE IF NOT EXISTS purchase_status_history (
                    id SERIAL PRIMARY KEY,
                    material_id INTEGER REFERENCES materials(id),
                    old_status VARCHAR(50),
                    new_status VARCHAR(50),
                    changed_by INTEGER REFERENCES users(user_id),
                    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
                );
                """
            ]
            
            for table_sql in missing_tables:
                try:
                    cursor.execute(table_sql)
                    table_name = table_sql.split("CREATE TABLE IF NOT EXISTS ")[1].split(" (")[0]
                    print(f"✅ {table_name} 테이블 생성 완료")
                except Exception as e:
                    print(f"⚠️  테이블 생성 중 오류 (무시하고 계속): {e}")
            
            conn.commit()
            print("✅ 모든 누락된 테이블 생성 완료")
            
    except Exception as e:
        print(f"❌ 테이블 생성 실패: {e}")
        return False
    finally:
        if conn:
            conn.close()
    
    return True

if __name__ == "__main__":
    print("🚀 누락된 테이블 생성 스크립트 시작")
    success = create_missing_tables()
    if success:
        print("✅ 누락된 테이블 생성 완료")
    else:
        print("❌ 누락된 테이블 생성 실패")
        sys.exit(1)
