"use server";

import { z } from "zod";
import { ZId, ZUuid } from "@formbricks/types/common";
import { AuthenticationError, ValidationError } from "@formbricks/types/errors";
import { ZMembershipUpdateInput } from "@formbricks/types/memberships";
import { assertCan } from "@/lib/authorization";
import { MCP_GRANT_REVOKE_TRANSACTION_TIMEOUT_MS } from "@/lib/authzed/mcp-grant-timeouts";
import { getMembershipByUserIdOrganizationId } from "@/lib/membership/service";
import { assertUserManagementAllowed } from "@/lib/membership/user-management";
import { lockUserActiveState } from "@/lib/user/reactivation";
import { authenticatedActionClient } from "@/lib/utils/action-client";
import { getOrganizationIdFromInviteId } from "@/lib/utils/helper";
import { finishRevokedMcpGrants } from "@/modules/auth/lib/mcp-grant-revocation";
import { applyRateLimit } from "@/modules/core/rate-limit/helpers";
import { rateLimitConfigs } from "@/modules/core/rate-limit/rate-limit-configs";
import { withAuditLogging } from "@/modules/ee/audit-logs/lib/handler";
import { updateInvite } from "@/modules/ee/role-management/lib/invite";
import {
  projectMembershipRoleChange,
  updateMembership,
  updateMembershipInTransaction,
} from "@/modules/ee/role-management/lib/membership";
import {
  assertCanAssignRole,
  checkRoleManagementPermission,
} from "@/modules/ee/role-management/lib/permission";
import { ZInviteUpdateInput } from "@/modules/ee/role-management/types/invites";
import { getInvite } from "@/modules/organization/settings/teams/lib/invite";
import {
  LastActiveOwnerError,
  runOwnerGuardedTransaction,
} from "@/modules/organization/settings/teams/lib/owner-guard";

const ZUpdateInviteAction = z.object({
  inviteId: ZUuid,
  data: ZInviteUpdateInput,
});

export type TUpdateInviteAction = z.infer<typeof ZUpdateInviteAction>;

export const updateInviteAction = authenticatedActionClient.inputSchema(ZUpdateInviteAction).action(
  withAuditLogging("updated", "invite", async ({ ctx, parsedInput }) => {
    const organizationId = await getOrganizationIdFromInviteId(parsedInput.inviteId);

    const currentUserMembership = await getMembershipByUserIdOrganizationId(ctx.user.id, organizationId);
    if (!currentUserMembership) {
      throw new AuthenticationError("User not a member of this organization");
    }

    await assertUserManagementAllowed(ctx.user.id, organizationId);
    await assertCan({ type: "user", id: ctx.user.id }, "organization.manage", {
      type: "organization",
      id: organizationId,
    });
    await applyRateLimit(rateLimitConfigs.actions.stateMutation, organizationId);

    await assertCanAssignRole({
      organizationId,
      assignerRole: currentUserMembership.role,
      roles: [parsedInput.data.role],
    });
    await checkRoleManagementPermission(organizationId);

    ctx.auditLoggingCtx.organizationId = organizationId;
    ctx.auditLoggingCtx.inviteId = parsedInput.inviteId;
    const oldInvite = { ...(await getInvite(parsedInput.inviteId)) };
    ctx.auditLoggingCtx.oldObject = oldInvite;

    const result = await updateInvite(parsedInput.inviteId, parsedInput.data, {
      allowOwnerInvite: currentUserMembership.role === "owner",
    });

    // Built from the write rather than re-read: `getInvite` is request-cached, so a second read in this
    // action would return the snapshot from before the update.
    ctx.auditLoggingCtx.newObject = { ...oldInvite, ...parsedInput.data };
    return result;
  })
);

/**
 * Demotes an owner under the owners' lock, refusing to demote the last active one. `actorUserId` is the
 * user making the change: the actor of the audit entries of any MCP grant it ends, which are finished once
 * the transaction commits. Each attempt returns its own records, so a retried attempt's are dropped.
 *
 * The new role reaches SpiceDB right after the commit, as in every other role change: left to the outbox,
 * the demoted user would keep the owner's permissions until it delivers (ENG-7282).
 */
const demoteOwner = async (
  organizationId: string,
  userId: string,
  data: z.infer<typeof ZMembershipUpdateInput>,
  actorUserId: string
) => {
  try {
    const { membership, grantAudits, affectedTeamIds } = await runOwnerGuardedTransaction(
      organizationId,
      userId,
      async (tx) => {
        // The user's row, as removing a member takes it: a demotion to Dashboard viewer decides whether only
        // viewer memberships remain (and revokes the wider OAuth grants), and must see a removal from another
        // organization that commits at the same moment. After the owners' lock, before the user's MCP grant
        // and consent rows.
        await lockUserActiveState(tx, userId);
        return updateMembershipInTransaction(tx, userId, organizationId, data);
      },
      // A demotion to Dashboard viewer ends the user's MCP grants, which can wait on a reconciling pass.
      { timeout: MCP_GRANT_REVOKE_TRANSACTION_TIMEOUT_MS }
    );
    // The role first: it is what the demoted user could still act on. The projection logs its own failures
    // and never throws, so the grants are always finished after it.
    await projectMembershipRoleChange(organizationId, userId, affectedTeamIds);
    await finishRevokedMcpGrants(grantAudits, { actorUserId });
    return membership;
  } catch (error) {
    if (error instanceof LastActiveOwnerError) {
      throw new ValidationError("You cannot demote the last owner of the organization");
    }
    throw error;
  }
};

const ZUpdateMembershipAction = z.object({
  userId: ZId,
  organizationId: ZId,
  data: ZMembershipUpdateInput,
});

export const updateMembershipAction = authenticatedActionClient.inputSchema(ZUpdateMembershipAction).action(
  withAuditLogging("updated", "membership", async ({ ctx, parsedInput }) => {
    const currentUserMembership = await getMembershipByUserIdOrganizationId(
      ctx.user.id,
      parsedInput.organizationId
    );
    if (!currentUserMembership) {
      throw new AuthenticationError("User not a member of this organization");
    }
    await assertUserManagementAllowed(ctx.user.id, parsedInput.organizationId);
    await assertCan({ type: "user", id: ctx.user.id }, "organization.manage", {
      type: "organization",
      id: parsedInput.organizationId,
    });
    await applyRateLimit(rateLimitConfigs.actions.stateMutation, parsedInput.organizationId);

    const targetMembership = await getMembershipByUserIdOrganizationId(
      parsedInput.userId,
      parsedInput.organizationId
    );
    await assertCanAssignRole({
      organizationId: parsedInput.organizationId,
      assignerRole: currentUserMembership.role,
      roles: [parsedInput.data.role],
      targetCurrentRole: targetMembership?.role,
    });
    await checkRoleManagementPermission(parsedInput.organizationId);

    ctx.auditLoggingCtx.organizationId = parsedInput.organizationId;
    ctx.auditLoggingCtx.membershipId = `${parsedInput.userId}-${parsedInput.organizationId}`;
    ctx.auditLoggingCtx.oldObject = targetMembership;

    const isDemotingOwner = targetMembership?.role === "owner" && parsedInput.data.role !== "owner";

    // The owner count and the role update must be one atomic unit: read then act, in two separate
    // statements, lets two owners demoting each other concurrently both read "more than one owner"
    // and both writes land, leaving zero owners. The owners' lock (`runOwnerGuardedTransaction`, the
    // one every last-owner check takes) makes the second wait, then count what the first committed.
    // Only a demotion of an owner runs in a transaction, as before; deciding it under the lock instead
    // of from the lookup above is ENG-2177.
    const result = isDemotingOwner
      ? await demoteOwner(parsedInput.organizationId, parsedInput.userId, parsedInput.data, ctx.user.id)
      : await updateMembership(parsedInput.userId, parsedInput.organizationId, parsedInput.data, ctx.user.id);

    ctx.auditLoggingCtx.newObject = result;
    return result;
  })
);
