const CoolifyClient = require('../coolifyClient');

/**
 * List all databases.
 * @param {string} host_url
 * @param {string} token
 * @returns {Promise<Object[]>}
 * @example
 * list('https://coolify.example.com', 'TOKEN');
 */
async function list(host_url, token) {
  const client = new CoolifyClient(host_url, token);
  return client.get('/api/v1/databases');
}

/**
 * Get database by UUID.
 * @param {string} host_url
 * @param {string} token
 * @param {string} uuid - Database UUID
 * @returns {Promise<Object>}
 * @example
 * get('https://coolify.example.com', 'TOKEN', 'db-uuid');
 */
async function get(host_url, token, uuid) {
  const client = new CoolifyClient(host_url, token);
  return client.get(`/api/v1/databases/${uuid}`);
}

/**
 * Create a new PostgreSQL database.
 * @param {string} host_url
 * @param {string} token
 * @param {Object} data - Database creation payload.
 * @param {string} data.server_uuid - UUID of the server. (required)
 * @param {string} data.project_uuid - UUID of the project. (required)
 * @param {string} data.environment_name - Name of the environment. (required, at least one of environment_name or environment_uuid)
 * @param {string} data.environment_uuid - UUID of the environment. (required, at least one of environment_name or environment_uuid)
 * @param {string} [data.postgres_user] - PostgreSQL user.
 * @param {string} [data.postgres_password] - PostgreSQL password.
 * @param {string} [data.postgres_db] - PostgreSQL database.
 * @param {string} [data.postgres_initdb_args] - PostgreSQL initdb args.
 * @param {string} [data.postgres_host_auth_method] - PostgreSQL host auth method.
 * @param {string} [data.postgres_conf] - PostgreSQL conf.
 * @param {string} [data.destination_uuid] - UUID of the destination if the server has multiple destinations.
 * @param {string} [data.name] - Name of the database.
 * @param {string} [data.description] - Description of the database.
 * @param {string} [data.image] - Docker Image of the database.
 * @param {boolean} [data.is_public] - Is the database public?
 * @param {integer} [data.public_port] - Public port of the database.
 * @param {string} [data.limits_memory] - Memory limit of the database.
 * @param {string} [data.limits_memory_swap] - Memory swap limit of the database.
 * @param {integer} [data.limits_memory_swappiness] - Memory swappiness of the database.
 * @param {string} [data.limits_memory_reservation] - Memory reservation of the database.
 * @param {string} [data.limits_cpus] - CPU limit of the database.
 * @param {string} [data.limits_cpuset] - CPU set of the database.
 * @param {integer} [data.limits_cpu_shares] - CPU shares of the database.
 * @param {boolean} [data.instant_deploy] - Instant deploy the database.
 * @returns {Promise<Object>}
 * @example
 * createPostgreSQL('https://coolify.example.com', 'TOKEN', {
 *   server_uuid: 'server-uuid',
 *   project_uuid: 'project-uuid',
 *   environment_name: 'production',
 *   environment_uuid: 'env-uuid',
 *   postgres_user: 'user',
 *   postgres_password: 'pass',
 *   postgres_db: 'dbname',
 *   // ...other optional fields
 * });
 */
async function createPostgreSQL(host_url, token, data) {
  const client = new CoolifyClient(host_url, token);
  return client.post('/api/v1/databases/postgresql', data);
}

/**
 * Update database by UUID.
 * @param {string} host_url
 * @param {string} token
 * @param {string} uuid - Database UUID
 * @param {Object} data - Update payload
 * @returns {Promise<Object>}
 * @example
 * update('https://coolify.example.com', 'TOKEN', 'db-uuid', { ... });
 */
async function update(host_url, token, uuid, data) {
  const client = new CoolifyClient(host_url, token);
  return client.patch(`/api/v1/databases/${uuid}`, data);
}

/**
 * Delete database by UUID.
 * @param {string} host_url
 * @param {string} token
 * @param {string} uuid - Database UUID
 * @param {Object} params - Optional query params (delete_configurations, delete_volumes, etc.)
 * @returns {Promise<Object>}
 * @example
 * remove('https://coolify.example.com', 'TOKEN', 'db-uuid', { delete_configurations: true });
 */
async function remove(host_url, token, uuid, params = {}) {
  const client = new CoolifyClient(host_url, token);
  return client.delete(`/api/v1/databases/${uuid}`, { params });
}

/**
 * Start database by UUID.
 * @param {string} host_url
 * @param {string} token
 * @param {string} uuid - Database UUID
 * @returns {Promise<Object>}
 * @example
 * start('https://coolify.example.com', 'TOKEN', 'db-uuid');
 */
async function start(host_url, token, uuid) {
  const client = new CoolifyClient(host_url, token);
  return client.get(`/api/v1/databases/${uuid}/start`);
}

/**
 * Stop database by UUID.
 * @param {string} host_url
 * @param {string} token
 * @param {string} uuid - Database UUID
 * @returns {Promise<Object>}
 * @example
 * stop('https://coolify.example.com', 'TOKEN', 'db-uuid');
 */
async function stop(host_url, token, uuid) {
  const client = new CoolifyClient(host_url, token);
  return client.get(`/api/v1/databases/${uuid}/stop`);
}

/**
 * Restart database by UUID.
 * @param {string} host_url
 * @param {string} token
 * @param {string} uuid - Database UUID
 * @returns {Promise<Object>}
 * @example
 * restart('https://coolify.example.com', 'TOKEN', 'db-uuid');
 */
async function restart(host_url, token, uuid) {
  const client = new CoolifyClient(host_url, token);
  return client.get(`/api/v1/databases/${uuid}/restart`);
}

module.exports = {
  list,
  get,
  update,
  remove,
  start,
  stop,
  restart,
}; 