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

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

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

/**
 * Create a new server.
 * @param {string} host_url
 * @param {string} token
 * @param {Object} data - Server creation payload.
 * @param {string} data.name - The name of the server. (required)
 * @param {string} [data.description] - The description of the server.
 * @param {string} data.ip - The IP of the server. (required)
 * @param {integer} data.port - The port of the server. (required)
 * @param {string} data.user - The user of the server. (required)
 * @param {string} data.private_key_uuid - The UUID of the private key. (required)
 * @param {boolean} [data.is_build_server] - Is build server.
 * @param {boolean} [data.instant_validate] - Instant validate.
 * @param {string} [data.proxy_type] - The proxy type. Enum: ['traefik', 'caddy', 'none']
 * @returns {Promise<Object>}
 * @example
 * create('https://coolify.example.com', 'TOKEN', {
 *   name: 'My Server',
 *   ip: '127.0.0.1',
 *   port: 22,
 *   user: 'root',
 *   private_key_uuid: 'key-uuid',
 *   proxy_type: 'traefik',
 * });
 */
async function create(host_url, token, data) {
  const client = new CoolifyClient(host_url, token);
  return client.post('/api/v1/servers', data);
}

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

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

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

module.exports = {
  list,
  get,
  create,
  getResources,
  getDomains,
  validate,
}; 