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

/**
 * List all services.
 * @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/services');
}

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

/**
 * Create a new service.
 * @param {string} host_url
 * @param {string} token
 * @param {Object} data - Service creation payload.
 * @param {string} data.name - The name of the service. (required)
 * @param {string} [data.description] - The description of the service.
 * @param {string} data.server_uuid - The UUID of the server. (required)
 * @param {string} data.project_uuid - The UUID of the project. (required)
 * @param {string} [data.environment_name] - The environment name.
 * @param {string} [data.environment_uuid] - The environment UUID.
 * @returns {Promise<Object>}
 * @example
 * create('https://coolify.example.com', 'TOKEN', {
 *   name: 'My Service',
 *   server_uuid: 'server-uuid',
 *   project_uuid: 'project-uuid',
 *   environment_name: 'production',
 * });
 */
async function create(host_url, token, data) {
  const client = new CoolifyClient(host_url, token);
  return client.post('/api/v1/services', data);
}

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

module.exports = {
  list,
  get,
  remove,
}; 