import type { CwMember } from "../../generated/prisma/client";

/**
 * CW Member Controller
 *
 * Domain model class that encapsulates a ConnectWise Member entity,
 * providing access to member data and serialization for the API.
 */
export class CwMemberController {
  public readonly id: string;
  public readonly cwMemberId: number;
  public readonly identifier: string;
  public firstName: string;
  public lastName: string;
  public officeEmail: string | null;
  public inactiveFlag: boolean;
  public apiKey: string | null;
  public cwLastUpdated: Date | null;
  public readonly createdAt: Date;
  public readonly updatedAt: Date;

  constructor(data: CwMember) {
    this.id = data.id;
    this.cwMemberId = data.cwMemberId;
    this.identifier = data.identifier;
    this.firstName = data.firstName;
    this.lastName = data.lastName;
    this.officeEmail = data.officeEmail;
    this.inactiveFlag = data.inactiveFlag;
    this.apiKey = data.apiKey;
    this.cwLastUpdated = data.cwLastUpdated;
    this.createdAt = data.createdAt;
    this.updatedAt = data.updatedAt;
  }

  /**
   * Full Name
   *
   * Returns the member's full name, falling back to the identifier.
   */
  public get fullName(): string {
    const name = `${this.firstName} ${this.lastName}`.trim();
    return name || this.identifier;
  }

  /**
   * To JSON
   *
   * Serializes the member into a safe, API-friendly object.
   */
  public toJson(): Record<string, any> {
    return {
      id: this.id,
      cwMemberId: this.cwMemberId,
      identifier: this.identifier,
      firstName: this.firstName,
      lastName: this.lastName,
      fullName: this.fullName,
      officeEmail: this.officeEmail,
      inactiveFlag: this.inactiveFlag,
      apiKey: this.apiKey,
      cwLastUpdated: this.cwLastUpdated,
      createdAt: this.createdAt,
      updatedAt: this.updatedAt,
    };
  }
}
