/*
 *                                      Copyright 2025 WebPros International, LLC
 *                                                           All rights reserved.
 * copyright@cpanel.net                                         http://cpanel.net
 * This code is subject to the cPanel license. Unauthorized copying is prohibited.
 */

import { SSH } from "@classes/ssh";

export class CompanyId {
  /** SSH object */
  ssh: SSH;

  constructor(ssh: SSH) {
    this.ssh = ssh;
  }

  /**
   * Get server company ID according to the companyid.fast file.
   *
   * @returns The server company ID as a number
   *
   * TODO: Move this logic into the ulc Server class MM-1433
   */
  async getCompanyID(): Promise<number> {
    const serverCidSymlink = "/var/cpanel/companyid.fast";
    let readCidLink: string | null = null;

    readCidLink = await this.ssh.readLink(serverCidSymlink);

    const companyId = Number(readCidLink);
    if (isNaN(companyId)) {
      throw new Error(
        `Invalid company ID: ${readCidLink} read from ${serverCidSymlink}`,
      );
    }

    return companyId;
  }
}
