import mysql from "mysql2";
import dotenv from "dotenv";
dotenv.config();

const pool = mysql
  .createPool({
    host: process.env.DB_HOST,
    user: process.env.DB_USER,
    password: process.env.DB_PASSWORD,
    database: process.env.DB_NAME,
  })
  .promise();

export async function loginUser(username, password) {
  const [result] = await pool.query(
    "SELECT * FROM users WHERE username = ? AND password = ?",
    [username, password]
  );

  if (result.length > 0) {
    console.log("User found: ", result[0]);
    return { success: true, user: result[0] };
  } else {
    console.error(`Invalid username or password!; ${result[0]}`);
    return { success: false, message: "Invalid username or password" };
  }
}
