const pool = require("../config/db");

const getAllTicketsTypes = async () => {
  const [rows] = await pool.query('SELECT * FROM tickets_type');
  return rows;
};

const getTicketsTypeById = async (id) => {
  const [rows] = await pool.query('SELECT * FROM tickets_type WHERE type_id = ?', [id]);
  return rows.length ? rows[0] : null;
};

const createTicketsType = async (type_id, type_name) => {
  await pool.query('INSERT INTO tickets_type (type_id, type_name) VALUES (?, ?)', [type_id, type_name]);
};

const updateTicketsType = async (id, type_name) => {
  const [result] = await pool.query('UPDATE tickets_type SET type_name = ? WHERE type_id = ?', [type_name, id]);
  return result.affectedRows > 0;
};

const deleteTicketsType = async (id) => {
  const [result] = await pool.query('DELETE FROM tickets_type WHERE type_id = ?', [id]);
  return result.affectedRows > 0;
};

module.exports = {
  getAllTicketsTypes,
  getTicketsTypeById,
  createTicketsType,
  updateTicketsType,
  deleteTicketsType
};

