import { config } from "dotenv"
import mysql from "mysql2/promise"

async function main() {
  config()
  const url = process.env.DATABASE_URL
  if (!url) {
    console.error("Missing DATABASE_URL")
    process.exit(1)
    return
  }
  const conn = await mysql.createConnection(url)
  try {
    const [rows] = await conn.query("SHOW COLUMNS FROM class_subject_teachers")
    const [keys] = await conn.query("SHOW KEYS FROM class_subject_teachers")
    let migrations: Array<{ id: number | string; hash: string; created_at: number | string }> | null = null
    try {
      const [m] = await conn.query("SELECT id, hash, created_at FROM __drizzle_migrations ORDER BY id DESC LIMIT 5")
      migrations = m as Array<{ id: number | string; hash: string; created_at: number | string }>
    } catch (error: unknown) {
      console.error(error)
    }
    const columns = rows as Array<{ Field: string; Type: string; Null: string; Key: string }>
    const indexes = keys as Array<{ Key_name: string; Column_name: string }>
    console.log(columns.map((r) => `${r.Field}:${r.Type}:${r.Null}:${r.Key}`).join("\n"))
    console.log(indexes.map((r) => `${r.Key_name}:${r.Column_name}`).join("\n"))
    if (migrations) {
      console.log(migrations.map((r) => `${r.id}:${r.hash}:${r.created_at}`).join("\n"))
    }
  } finally {
    await conn.end()
  }
}

main().catch((e) => {
  console.error(e)
  process.exit(1)
})
