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

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

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

const createCountry = async (country_id, country_name) => {
  await pool.query('INSERT INTO country (country_id, country_name) VALUES (?, ?)', [country_id, country_name]);
};

const updateCountry = async (id, country_name) => {
  const [result] = await pool.query('UPDATE country SET country_name = ? WHERE country_id = ?', [country_name, id]);
  return result.affectedRows > 0;
};

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

module.exports = {
  getAllCountries,
  getCountryById,
  createCountry,
  updateCountry,
  deleteCountry
};

