import { Auth, DatabaseReader, DatabaseWriter } from ".";
import { GenericDataModel } from "./data_model.js";

/**
 * A set of services for use within Convex mutation functions.
 *
 * The mutation context is passed as the first argument to any Convex mutation
 * function run on the server.
 *
 * If you're using code generation, use the `MutationCtx` type in
 * `convex/_generated/server.d.ts` which is typed for your data model.
 *
 * @public
 */
export type MutationCtx<DataModel extends GenericDataModel> = {
  db: DatabaseWriter<DataModel>;
  auth: Auth;
};

/**
 * A set of services for use within Convex query functions.
 *
 * The query context is passed as the first argument to any Convex query
 * function run on the server.
 *
 * This differs from the {@link MutationCtx} because all of the services are
 * read-only.
 *
 * If you're using code generation, use the `QueryCtx` type in
 * `convex/_generated/server.d.ts` which is typed for your data model.
 *
 * @public
 */
export type QueryCtx<DataModel extends GenericDataModel> = {
  db: DatabaseReader<DataModel>;
  auth: Auth;
};

/**
 * A mutation function that is part of this app's public API.
 *
 * You can create public mutations by wrapping your function in
 * {@link mutationGeneric} and exporting it.
 *
 * @public
 */
export type PublicMutation<
  DataModel extends GenericDataModel,
  Args extends any[],
  Output
> = {
  args: Args;
  output: Output;

  (ctx: MutationCtx<DataModel>, ...args: Args): Output;
  isMutation: true;
  isRegistered?: true;

  /** @internal */
  invokeMutation(argsStr: string): Promise<string>;
};

/**
 * A query function that is part of this app's public API.
 *
 * You can create public queries by wrapping your function in
 * {@link queryGeneric} and exporting it.
 *
 * @public
 */
export type PublicQuery<
  DataModel extends GenericDataModel,
  Args extends any[],
  Output
> = {
  args: Args;
  output: Output;

  (ctx: QueryCtx<DataModel>, ...args: Args): Output;
  isQuery: true;
  isRegistered?: true;

  /** @internal */
  invokeQuery(argsStr: string): Promise<string>;
};

/**
 * Internal type helper used by Convex code generation.
 *
 * Used to give {@link mutationGeneric} a type specific to your data model.
 * @public
 */
export type MutationBuilderForDataModel<DataModel extends GenericDataModel> = <
  Args extends any[],
  Output
>(
  func: (ctx: MutationCtx<DataModel>, ...args: Args) => Output
) => PublicMutation<DataModel, Args, Output>;

/**
 * Internal type helper used by Convex code generation.
 *
 * Used to give {@link queryGeneric} a type specific to your data model.
 * @public
 */
export type QueryBuilderForDataModel<DataModel extends GenericDataModel> = <
  Args extends any[],
  Output
>(
  func: (ctx: QueryCtx<DataModel>, ...args: Args) => Output
) => PublicQuery<DataModel, Args, Output>;
