import type Field from "@arcgis/core/layers/support/Field.js";
import type { RequestOptions } from "@arcgis/core/request/types.js";
import type { default as ExternalPortal } from "@arcgis/core/portal/Portal.js";

/**
 * Information about a data catalog datastore.
 *
 * @since 5.1
 */
export interface DataCatalogDatastoreInfo {
  /**
   * The name of the datastore.
   *
   * @since 5.1
   */
  name: string;
  /**
   * The datastore type.
   *
   * @since 5.1
   */
  type: string;
  /**
   * The datastore identifier.
   *
   * @since 5.1
   */
  datastoreId: string;
  /**
   * The database platform.
   *
   * @since 5.1
   */
  databasePlatform: DatabasePlatform;
}

/**
 * Information about a dataset in a data catalog datastore.
 *
 * @since 5.1
 */
export interface DataCatalogDatasetInfo {
  /**
   * The dataset name.
   *
   * @since 5.1
   */
  name: string;
  /**
   * The dataset type.
   *
   * @since 5.1
   */
  type: string;
}

/**
 * Response for the data catalog datasets endpoint.
 *
 * @since 5.1
 */
export interface DataCatalogDatasetsResponse {
  /**
   * The list of datasets in the datastore.
   *
   * @since 5.1
   */
  datasets: DataCatalogDatasetInfo[];
}

/**
 * Information about a field from a table description response.
 *
 * @since 5.1
 */
export interface DataCatalogFieldInfo {
  /**
   * The field name.
   *
   * @since 5.1
   */
  name: string;
  /**
   * The normalized field type.
   *
   * This is converted from the server's `esriFieldType*` value into the same field type family
   * used elsewhere in the editor codebase so icon and value-type helpers can operate on it.
   *
   * @since 5.1
   */
  type: Field["type"];
  /**
   * The raw field type returned by the tableDescription endpoint.
   *
   * @since 5.1
   */
  serverType: string;
  /**
   * Indicates whether the field allows null values.
   *
   * @since 5.1
   */
  nullable: boolean;
}

/**
 * Response for a table description request.
 *
 * @since 5.1
 */
export interface DataCatalogTableDescription {
  /**
   * The fields available in the table.
   *
   * @since 5.1
   */
  fields: DataCatalogFieldInfo[];
}

/**
 * Represents a portal data catalog datastore and provides helper methods to query
 * datastore details, datasets, and table descriptions.
 *
 * @since 5.1
 */
export default class DataCatalogDatastore {
  constructor(options: {
      itemId: string;
      portal: Portal;
      serviceUrl: string;
      requestOptions?: Nil | RequestOptions;
  });
  readonly itemId: string;
  readonly portal: Portal;
  readonly serviceUrl: string;
  /**
   * Fetches datasets for the datastore.
   *
   * @param options - Additional options used for the request.
   * @returns Resolves to an array of dataset information.
   * @since 5.1
   */
  fetchDatasets(options?: Nil | RequestOptions): Promise<DataCatalogDatasetInfo[]>;
  /**
   * Fetches information about the datastore.
   *
   * @param options - Additional options used for the request.
   * @returns Resolves to datastore information.
   * @since 5.1
   */
  fetchInfo(options?: Nil | RequestOptions): Promise<DataCatalogDatastoreInfo>;
  /**
   * Fetches the table description for the provided table name.
   *
   * @param tableName - The table name.
   * @param options - Additional options used for the request.
   * @returns Resolves to the table description.
   * @since 5.1
   */
  fetchTableDescription(tableName: string, options?: Nil | RequestOptions): Promise<DataCatalogTableDescription>;
}

export type Portal = ExternalPortal
  & {
  request: <T>(url: string, options?: RequestOptions) => Promise<T>;
};

export type Nil = null | undefined;

export type DatabasePlatform = "esriDBMS_PostgreSQL" | "esriDBMS_SQLServer";