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

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

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

/**
 * Create a new project.
 * @param {string} host_url
 * @param {string} token
 * @param {Object} data - Project creation payload.
 * @param {string} data.name - The name of the project. (required)
 * @param {string} [data.description] - The description of the project.
 * @returns {Promise<Object>}
 * @example
 * create('https://coolify.example.com', 'TOKEN', {
 *   name: 'My Project',
 *   description: 'A sample project',
 * });
 */
async function create(host_url, token, data) {
  const client = new CoolifyClient(host_url, token);
  return client.post('/api/v1/projects', data);
}

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

/**
 * Delete project by UUID.
 * @param {string} host_url
 * @param {string} token
 * @param {string} uuid - Project UUID
 * @returns {Promise<Object>}
 * @example
 * remove('https://coolify.example.com', 'TOKEN', 'project-uuid');
 */
async function remove(host_url, token, uuid) {
  const client = new CoolifyClient(host_url, token);
  return client.delete(`/api/v1/projects/${uuid}`);
}

/**
 * Get project environment by name or UUID.
 * @param {string} host_url
 * @param {string} token
 * @param {string} uuid - Project UUID
 * @param {string} environment - Environment name or UUID
 * @returns {Promise<Object>}
 * @example
 * getEnvironment('https://coolify.example.com', 'TOKEN', 'project-uuid', 'production');
 */
async function getEnvironment(host_url, token, uuid, environment) {
  const client = new CoolifyClient(host_url, token);
  return client.get(`/api/v1/projects/${uuid}/${environment}`);
}

module.exports = {
  list,
  get,
  create,
  update,
  remove,
  getEnvironment,
}; 