///////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2002-2025, Open Design Alliance (the "Alliance").
// All rights reserved.
//
// This software and its documentation and related materials are owned by
// the Alliance. The software may only be incorporated into application
// programs owned by members of the Alliance, subject to a signed
// Membership Agreement and Supplemental Software License Agreement with the
// Alliance. The structure and organization of this software are the valuable
// trade secrets of the Alliance and its suppliers. The software is also
// protected by copyright law and international treaty provisions. Application
// programs incorporating this software must include the following statement
// with their copyright notices:
//
//   This application incorporates Open Design Alliance software pursuant to a
//   license agreement with Open Design Alliance.
//   Open Design Alliance Copyright (C) 2002-2025 by Open Design Alliance.
//   All rights reserved.
//
// By use of this software, its documentation or related materials, you
// acknowledge and accept the above terms.
///////////////////////////////////////////////////////////////////////////////

import { IHttpClient } from "./IHttpClient";
import { Endpoint } from "./Endpoint";
import { IShortUserDesc } from "./IUser";
import { userFullName, userInitials } from "./Utils";

/**
 * Provides properties and methods for obtaining information about a {@link User | user} who has access to
 * the {@link Project | project}.
 */
export class Member extends Endpoint {
  private _data: any;

  /**
   * @param data - Raw member data received from the server. For more information, see
   *   {@link https://cloud.opendesign.com/docs//pages/server/api.html#Project | Open Cloud Projects API}.
   * @param projectId - Owner project ID.
   * @param httpClient - HTTP client instance used to send requests to the REST API server.
   */
  constructor(data: any, projectId: string, httpClient: IHttpClient) {
    super(`/projects/${projectId}/members/${data.id}`, httpClient);
    this.data = data;
  }

  /**
   * Raw member data received from the server. For more information, see
   * {@link https://cloud.opendesign.com/docs//pages/server/api.html#Project | Open Cloud Projects API}.
   *
   * @readonly
   */
  get data(): any {
    return this._data;
  }

  private set data(value: any) {
    this._data = value;
    this._data.user.avatarUrl = `${this.httpClient.serverUrl}/users/${this._data.user.userId}/avatar`;
    this._data.user.fullName = userFullName(this._data.user);
    this._data.user.initials = userInitials(this._data.user.fullName);
  }

  /**
   * Unique member ID.
   *
   * @readonly
   */
  get id(): string {
    return this.data.id;
  }

  /**
   * Member role name in the project. See {@link Project.getRoles | Project.getRoles()} for list of
   * project roles.
   */
  get role(): string {
    return this.data.role;
  }

  set role(value: string) {
    this.data.role = value;
  }

  /**
   * Member type. Can be `owner` or `user`.
   *
   * @readonly
   */
  get type(): string {
    return this.data.type;
  }

  /**
   * User information.
   *
   * @readonly
   */
  get user(): IShortUserDesc {
    return this.data.user;
  }

  /**
   * Reloads member data from the server.
   */
  async checkout(): Promise<this> {
    const response = await this.get("");
    this.data = await response.json();
    return this;
  }

  /**
   * Updates member data on the server.
   *
   * @param data - Raw member data. For more information, see
   *   {@link https://cloud.opendesign.com/docs//pages/server/api.html#Project | Open Cloud Projects API}.
   */
  async update(data: any): Promise<this> {
    const response = await this.put("", data);
    this.data = await response.json();
    return this;
  }

  /**
   * Removes a member from the project.
   *
   * @returns Returns the raw data of a deleted member. For more information, see
   *   {@link https://cloud.opendesign.com/docs//pages/server/api.html#Project | Open Cloud Projects API}.
   */
  override delete(): Promise<any> {
    return super.delete("").then((response) => response.json());
  }

  /**
   * Saves member properties changes to the server. Call this method to update member data on the server
   * after any property changes.
   */
  save(): Promise<this> {
    return this.update(this.data);
  }
}
