"use server";

import { z } from "zod";
import { logger } from "@formbricks/logger";
import { ZId } from "@formbricks/types/common";
import { AuthenticationError } from "@formbricks/types/errors";
import { ZUserEmail, ZUserName } from "@formbricks/types/user";
import { INVITE_DISABLED } from "@/lib/constants";
import { authenticatedActionClient } from "@/lib/utils/action-client";
import { withAuditLogging } from "@/modules/ee/audit-logs/lib/handler";
import { sendInviteMemberEmail } from "@/modules/email";
import {
  reserveInviteRateLimit,
  settleInviteRateLimit,
} from "@/modules/organization/settings/teams/lib/invite-rate-limit";
import { checkSetupInviteAuthorization } from "@/modules/setup/organization/[organizationId]/invite/lib/authorization";
import { inviteUser } from "@/modules/setup/organization/[organizationId]/invite/lib/invite";

const ZInviteOrganizationMemberAction = z.object({
  email: ZUserEmail,
  organizationId: ZId,
  name: ZUserName,
});

export const inviteOrganizationMemberAction = authenticatedActionClient
  .inputSchema(ZInviteOrganizationMemberAction)
  .action(
    withAuditLogging("created", "invite", async ({ ctx, parsedInput }) => {
      if (INVITE_DISABLED) {
        throw new AuthenticationError("Invite disabled");
      }

      // Owner-only — see `SETUP_INVITE_ACTION` for why this path is narrower than the org settings
      // invite path.
      await checkSetupInviteAuthorization(ctx.user.id, parsedInput.organizationId);

      ctx.auditLoggingCtx.organizationId = parsedInput.organizationId;

      // Shares one recipient-counted budget with settings, bulk, and resend invite paths.
      const rateLimitReservation = await reserveInviteRateLimit(parsedInput.organizationId, ctx.user.id);

      let invitedUserId: Awaited<ReturnType<typeof inviteUser>>;
      try {
        invitedUserId = await inviteUser({
          organizationId: parsedInput.organizationId,
          invitee: {
            email: parsedInput.email,
            name: parsedInput.name,
          },
          currentUserId: ctx.user.id,
        });
      } catch (error) {
        await settleInviteRateLimit(rateLimitReservation, 0);
        throw error;
      }
      await settleInviteRateLimit(rateLimitReservation, 1);

      // Best-effort, like the other invite actions: the invite already exists and can be shared by link,
      // so a failed or rate-limited email must not strand it behind "Invite already exists" on a retry.
      try {
        await sendInviteMemberEmail(
          invitedUserId,
          parsedInput.email,
          ctx.user.name,
          "",
          parsedInput.organizationId,
          ctx.user.id
        );
      } catch (error) {
        logger.error({ err: error }, "Failed to send onboarding invite email");
      }

      ctx.auditLoggingCtx.inviteId = invitedUserId;
      ctx.auditLoggingCtx.newObject = {
        invitedUserId,
        email: parsedInput.email,
        name: parsedInput.name,
      };

      return invitedUserId;
    })
  );
