import pg from 'pg'

const { Pool } = pg

// 환경 설정
const isDev = process.env.NODE_ENV !== 'production'

// PostgreSQL 연결 풀 (싱글톤)
let pool: pg.Pool | null = null

/**
 * PostgreSQL 연결 풀 가져오기
 */
export function getPool(): pg.Pool {
  if (!pool) {
    const config = {
      host: process.env.DB_HOST || 'localhost',
      port: parseInt(process.env.DB_PORT || '5432'),
      database: process.env.DB_NAME || 'osolit_monitor',
      user: process.env.DB_USER || 'postgres',
      password: process.env.DB_PASSWORD || '',
      max: 10,
      idleTimeoutMillis: 30000,
      connectionTimeoutMillis: 2000,
    }
    
    console.log(`[DB] Connecting to ${config.host}:${config.port}/${config.database}`)
    
    pool = new Pool(config)

    pool.on('error', (err) => {
      console.error('[DB] Unexpected pool error:', err)
    })

    console.log(`[DB] PostgreSQL pool created (${isDev ? 'development' : 'production'})`)
  }
  return pool
}

/**
 * 단일 쿼리 실행 (자동 커넥션 반환)
 */
export async function query<T = any>(sql: string, params?: any[]): Promise<T[]> {
  const pool = getPool()
  const result = await pool.query(sql, params)
  return result.rows as T[]
}

/**
 * 단일 행 조회
 */
export async function queryOne<T = any>(sql: string, params?: any[]): Promise<T | null> {
  const rows = await query<T>(sql, params)
  return rows[0] || null
}

/**
 * INSERT/UPDATE/DELETE 실행
 */
export async function execute(sql: string, params?: any[]): Promise<number> {
  const pool = getPool()
  const result = await pool.query(sql, params)
  return result.rowCount || 0
}

/**
 * 스케줄러 자동시작 여부
 */
export function shouldAutoStartScheduler(): boolean {
  const envValue = process.env.AUTO_START_SCHEDULER
  if (envValue !== undefined) {
    return envValue === 'true'
  }
  return !isDev
}

/**
 * 연결 풀 종료
 */
export async function closeDb(): Promise<void> {
  if (pool) {
    await pool.end()
    pool = null
    console.log('[DB] PostgreSQL pool closed')
  }
}
