///////////////////////////////////////////////////////////////////////////////
// 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";

/**
 * Provides properties and methods for obtaining information about a OAuth 2.0 client that have access
 * the Open Cloud Server API.
 */
export class OAuthClient extends Endpoint {
  private _data: any;

  /**
   * @param data - Raw client data received from the server. For more information, see
   *   {@link https://cloud.opendesign.com/docs//pages/server/api.html#OAuthClient | Open Cloud OAuth Clients API}.
   * @param httpClient - HTTP client instance used to send requests to the REST API server.
   */
  constructor(data: any, httpClient: IHttpClient) {
    super(`/oauth/clients/${data.clientId}`, httpClient);
    this.data = data;
  }

  /**
   * OAuth 2.0 server authorization endpoint.
   */
  get authUrl(): string {
    return this.data.authUrl;
  }

  /**
   * OAuth 2.0 server token endpoint.
   */
  get accessTokenUrl(): string {
    return this.data.accessTokenUrl;
  }

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

  /**
   * Client creation time (UTC) in the format specified in
   * {@link https://www.wikipedia.org/wiki/ISO_8601 | ISO 8601}.
   */
  get createdAt(): string {
    return this.data.createdAt;
  }

  /**
   * Client application description.
   */
  get description(): string {
    return this.data.description;
  }

  set description(value: string) {
    this._data.description = value;
  }

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

  set data(value: any) {
    this._data = value;
  }

  /**
   * Client application name.
   */
  get name(): string {
    return this.data.name;
  }

  set name(value: string) {
    this._data.name = value;
  }

  /**
   * The endpoint to which the OAuth 2.0 server sends the response.
   */
  get redirectUrl(): string {
    return this.data.redirectUrl;
  }

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

  /**
   * Client secret.
   *
   * @readonly
   */
  get secret(): string {
    return this.data.secret;
  }

  /**
   * Client last update time (UTC) in the format specified in
   * {@link https://www.wikipedia.org/wiki/ISO_8601 | ISO 8601}.
   */
  get updatedAt(): string {
    return this.data.updatedAt;
  }

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

  /**
   * Updates client data on the server.
   *
   * Only administrators can update OAuth clients. If the current logged in user is not an administrator,
   * an exception will be thrown.
   *
   * @param data - Raw client data. For more information, see
   *   {@link https://cloud.opendesign.com/docs//pages/server/api.html#OAuthClient | Open Cloud OAuth Clients API}.
   */
  async update(data: any): Promise<this> {
    const response = await this.put("", data);
    this.data = await response.json();
    return this;
  }

  /**
   * Deletes a client from the server.
   *
   * Only administrators can delete OAuth clients. If the current logged in user is not an administrator,
   * an exception will be thrown.
   *
   * @returns Returns the raw data of a deleted client. For more information, see
   *   {@link https://cloud.opendesign.com/docs//pages/server/api.html#OAuthClient | Open Cloud OAuth Clients API}.
   */
  override delete(): Promise<any> {
    return super.delete("").then((response) => response.json());
  }

  /**
   * Saves client properties changes to the server. Call this method to update client data on the server
   * after any property changes.
   *
   * Only administrators can update OAuth clients. If the current logged in user is not an administrator,
   * an exception will be thrown.
   */
  save(): Promise<this> {
    return this.update(this.data);
  }

  /**
   * Revokes the access tokens for all users of the client application.
   */
  async revoke(): Promise<this> {
    await this.post("/revoke");
    return this;
  }
}
