import { z } from "zod";
import { ZodOpenApiOperationObject, ZodOpenApiPathsObject } from "zod-openapi";
import { ZUser } from "@formbricks/database/zod/users";
import { ZOrganizationIdSchema } from "@/modules/api/v2/organizations/[organizationId]/types/organizations";
import {
  ZGetUsersFilter,
  ZUserInput,
  ZUserInputPatch,
} from "@/modules/api/v2/organizations/[organizationId]/users/types/users";
import { makePartialSchema, responseWithMetaSchema } from "@/modules/api/v2/types/openapi-response";

export const getUsersEndpoint: ZodOpenApiOperationObject = {
  operationId: "getUsers",
  summary: "Get users",
  description: `Gets users from the database.<br />Only available for self-hosted Formbricks.`,
  requestParams: {
    path: z.object({
      organizationId: ZOrganizationIdSchema,
    }),
    query: ZGetUsersFilter,
  },
  tags: ["Organizations API - Users"],
  responses: {
    "200": {
      description: "Users retrieved successfully.",
      content: {
        "application/json": {
          schema: responseWithMetaSchema(makePartialSchema(ZUser)),
        },
      },
    },
  },
};

export const createUserEndpoint: ZodOpenApiOperationObject = {
  operationId: "createUser",
  summary: "Create a user",
  description: `Create a new user in the database.<br />Only available for self-hosted Formbricks.`,
  requestParams: {
    path: z.object({
      organizationId: ZOrganizationIdSchema,
    }),
  },
  tags: ["Organizations API - Users"],
  requestBody: {
    required: true,
    description: "The user to create",
    content: {
      "application/json": {
        schema: ZUserInput,
      },
    },
  },
  responses: {
    "201": {
      description: "User created successfully.",
      content: {
        "application/json": {
          schema: makePartialSchema(ZUser),
        },
      },
    },
  },
};

export const updateUserEndpoint: ZodOpenApiOperationObject = {
  operationId: "updateUser",
  summary: "Update a user",
  description: `Updates an existing user in the database.<br />Only available for self-hosted Formbricks.<br />\`name\` and \`isActive\` apply to the whole account, so they can only be changed for a user who belongs to this organization alone. For a user who also belongs to other organizations, a request that changes either is rejected with 409 and nothing in it is applied.<br />An organization keeps at least one active owner, so a request that would demote or deactivate its last active owner is rejected with 409, naming \`role\` or \`isActive\`, and nothing in it is applied.`,
  requestParams: {
    path: z.object({
      organizationId: ZOrganizationIdSchema,
    }),
  },
  tags: ["Organizations API - Users"],
  requestBody: {
    required: true,
    description: "The user to update",
    content: {
      "application/json": {
        schema: ZUserInputPatch,
      },
    },
  },
  responses: {
    "200": {
      description: "User updated successfully.",
      content: {
        "application/json": {
          schema: makePartialSchema(ZUser),
        },
      },
    },
  },
};

export const userPaths: ZodOpenApiPathsObject = {
  "/organizations/{organizationId}/users": {
    get: getUsersEndpoint,
    post: createUserEndpoint,
    patch: updateUserEndpoint,
  },
};
