const mariadb = require('mariadb');

const gameInfo = async (req, res) => {

// Create a connection pool
const pool = mariadb.createPool({
    host: process.env.MARIA_HOST,
    user: process.env.MARIA_USER,
    password: process.env.MARIA_PASS,
    database: process.env.MARIA_DBNM,
    connectionLimit: 5,
  });
  
  const { schoolId, teacherId } = req.query;

  if (!schoolId && !teacherId) {
    return res.status(400).send({ error: 'Either schoolId or teacherId is required in the query parameters.' });
  }

  let connection;
  try {
    // Establish a connection to the database
    connection = await pool.getConnection();

    let queryCondition;
    let queryValue;

    // Determine the query condition based on the parameters provided
    if (schoolId) {
      queryCondition = 'schoolId = ?';
      queryValue = schoolId;
    } else {
      queryCondition = 'teacherId = ?';
      queryValue = teacherId;
    }

    // Query both game1 and game2 tables
    const query1 = `SELECT * FROM game_drawing WHERE ${queryCondition}`;
    const query2 = `SELECT * FROM game_drawing_v2 WHERE ${queryCondition}`;
    const query3 = `SELECT * FROM game_drawing_v3 WHERE ${queryCondition}`;
    const query4 = `SELECT * FROM drawing_phonics WHERE ${queryCondition}`;
    const query5 = `SELECT * FROM game_tick_variant1 WHERE ${queryCondition}`;
    const query6 = `SELECT * FROM game_tick_variant2 WHERE ${queryCondition}`;
    const query7 = `SELECT * FROM game_tick_varient3 WHERE ${queryCondition}`;
    const query8 = `SELECT * FROM game_cross_varient1 WHERE ${queryCondition}`;
    const query9 = `SELECT * FROM game_cross_varient2 WHERE ${queryCondition}`;
    const query10 = `SELECT * FROM game_cross_varient3 WHERE ${queryCondition}`;
    const query11 = `SELECT * FROM game_drag WHERE ${queryCondition}`;
    const query12 = `SELECT * FROM game_drag_v2 WHERE ${queryCondition}`;
    const query13 = `SELECT * FROM game_drag_v4 WHERE ${queryCondition}`;
    const query14 = `SELECT * FROM game_tick_v3_phonics WHERE ${queryCondition}`;
    const query15 = `SELECT * FROM tick_phonics_option_6 WHERE ${queryCondition}`;
    const query16 = `SELECT * FROM tick_phonics_option_9 WHERE ${queryCondition}`;
    const query17 = `SELECT * FROM drag_drop_phonics WHERE ${queryCondition}`;
    const query18 = `SELECT * FROM drag_phonics WHERE ${queryCondition}`;
    const query19 = `SELECT * FROM cross_phonics_option_9 WHERE ${queryCondition}`;

    const [game_drawing, game_drawing_v2, game_drawing_v3, drawing_phonics, game_tick_variant1, game_tick_variant2, game_tick_varient3, game_cross_varient1, game_cross_varient2,  game_cross_varient3, game_drag, game_drag_v2, game_drag_v4, game_tick_v3_phonics, tick_phonics_option_6, tick_phonics_option_9, drag_drop_phonics, drag_phonics, cross_phonics_option_9 ] = await Promise.all([
      connection.query(query1, [queryValue]),
      connection.query(query2, [queryValue]),
      connection.query(query3, [queryValue]),
      connection.query(query4, [queryValue]),
      connection.query(query5, [queryValue]),
      connection.query(query6, [queryValue]),
      connection.query(query7, [queryValue]),
      connection.query(query8, [queryValue]),
      connection.query(query9, [queryValue]),
      connection.query(query10, [queryValue]),
      connection.query(query11, [queryValue]),
      connection.query(query12, [queryValue]),
      connection.query(query13, [queryValue]),
      connection.query(query14, [queryValue]),
      connection.query(query15, [queryValue]),
      connection.query(query16, [queryValue]),
      connection.query(query17, [queryValue]),
      connection.query(query18, [queryValue]),
      connection.query(query19, [queryValue])
    ]);

    // const domain = 'https://domain.com/gamelink/?id=';
    const domain =  process.env.GAME_DOMAIN;

    const formattedGame1  = game_drawing.map(game => ({ ...game, game_id: `game_drawing-${game.id}`, game_link: `${domain}/drawing/?id=${game.id}`, }));
    const formattedGame2  = game_drawing_v2.map(({ status, ...game }) => ({ ...game, game_id: `game_drawing_v2-${game.id}`, game_link: `${domain}/drawing/v2/?id=${game.id}`, }));
    const formattedGame3  = game_drawing_v3.map(({ status, ...game }) => ({ ...game, game_id: `game_drawing_v3-${game.id}`, game_link: `${domain}/drawing/v3/?id=${game.id}`, }));
    const formattedGame4  = drawing_phonics.map(({ status, ...game }) => ({ ...game, game_id: `drawing_phonics-${game.id}`, game_link: `${domain}/drawing/drawing_phonics/?id=${game.id}`, }));
    const formattedGame5  = game_tick_variant1.map(({ status, ...game }) => ({ ...game, game_id: `game_tick_variant1-${game.id}`, game_link: `${domain}/tick/v1/?id=${game.id}`, }));
    const formattedGame6  = game_tick_variant2.map(({ status, ...game }) => ({ ...game, game_id: `game_tick_variant2-${game.id}`, game_link: `${domain}/tick/v2/?id=${game.id}`, }));
    const formattedGame7  = game_tick_varient3.map(({ status, ...game }) => ({ ...game, game_id: `game_tick_varient3-${game.id}`, game_link: `${domain}/tick/v3/?id=${game.id}`, }));
    const formattedGame8  = game_cross_varient1.map(({ status, ...game }) => ({ ...game, game_id: `game_cross_varient1-${game.id}`, game_link: `${domain}/cross/v1/?id=${game.id}`, }));
    const formattedGame9  = game_cross_varient2.map(({ status, ...game }) => ({ ...game, game_id: `game_cross_varient2-${game.id}`, game_link: `${domain}/cross/v2/?id=${game.id}`, }));
    const formattedGame10  = game_cross_varient3.map(({ status, ...game }) => ({ ...game, game_id: `game_cross_varient3-${game.id}`, game_link: `${domain}/cross/v3/?id=${game.id}`, }));
    const formattedGame11  = game_drag.map(({ status, ...game }) => ({ ...game, game_id: `game_drag-${game.id}`, game_link: `${domain}/drag/?id=${game.id}`, }));
    const formattedGame12  = game_drag_v2.map(({ status, ...game }) => ({ ...game, game_id: `game_drag_v2-${game.id}`, game_link: `${domain}/drag/v2/?id=${game.id}`, }));
    const formattedGame13  = game_drag_v4.map(({ status, ...game }) => ({ ...game, game_id: `game_drag_v4-${game.id}`, game_link: `${domain}/drag/v4/?id=${game.id}`, }));
    const formattedGame14  = game_tick_v3_phonics.map(({ status, ...game }) => ({ ...game, game_id: `game_tick_v3_phonics-${game.id}`, game_link: `${domain}/tick/tick_phonics_v1/?id=${game.id}`, }));
    const formattedGame15  = tick_phonics_option_6.map(({ status, ...game }) => ({ ...game, game_id: `tick_phonics_option_6-${game.id}`, game_link: `${domain}/tick/tick_phonics_v2/?id=${game.id}`, }));
    const formattedGame16  = tick_phonics_option_9.map(({ status, ...game }) => ({ ...game, game_id: `tick_phonics_option_9-${game.id}`, game_link: `${domain}/tick/tick_phonics_v3/?id=${game.id}`, }));
    const formattedGame17  = drag_drop_phonics.map(({ status, ...game }) => ({ ...game, game_id: `drag_drop_phonics-${game.id}`, game_link: `${domain}/drag/dragdrop_phonics/?id=${game.id}`, }));
    const formattedGame18  = drag_phonics.map(({ status, ...game }) => ({ ...game, game_id: `drag_phonics-${game.id}`, game_link: `${domain}/drag/match_phonics/?id=${game.id}`, }));
    const formattedGame19  = cross_phonics_option_9.map(({ status, ...game }) => ({ ...game, game_id: `cross_phonics_option_9-${game.id}`, game_link: `${domain}/cross/cross_phonics_v2/?id=${game.id}`, }));


    const combinedResults = {
      game_drawing: formattedGame1,
      game_drawing_v2: formattedGame2,
      game_drawing_v3: formattedGame3,
      drawing_phonics: formattedGame4,
      game_tick_variant1: formattedGame5,
      game_tick_variant2: formattedGame6,
      game_tick_varient3: formattedGame7,
      game_cross_varient1: formattedGame8,
      game_cross_varient2: formattedGame9,
      game_cross_varient3: formattedGame10,
      game_drag: formattedGame11,
      game_drag_v2: formattedGame12,
      game_drag_v4: formattedGame13,
      game_tick_v3_phonics: formattedGame14,
      tick_phonics_option_6: formattedGame15,
      tick_phonics_option_9: formattedGame16,
      drag_drop_phonics: formattedGame17,
      drag_phonics: formattedGame18,
      cross_phonics_option_9: formattedGame19
    };

    res.status(200).send(combinedResults);
  } catch (error) {
    console.error('Error querying game tables:', error);
    res.status(500).send({ error: 'An error occurred while fetching game data.' });
  } finally {
    // Release the connection back to the pool
    if (connection) connection.release();
  }
};

module.exports = gameInfo;
