import "server-only";
import { prisma } from "@formbricks/database";
import { Prisma } from "@formbricks/database/prisma";
import { logger } from "@formbricks/logger";
import { ZId } from "@formbricks/types/common";
import {
  DatabaseError,
  InvalidInputError,
  OperationNotAllowedError,
  ResourceNotFoundError,
  ValidationError,
} from "@formbricks/types/errors";
import { TLogo, TWorkspace, TWorkspaceUpdateInput, ZWorkspaceUpdateInput } from "@formbricks/types/workspace";
import { reconcileFeedbackDirectoryRelationships } from "@/lib/authzed/feedback-directory";
import { runPostCommitProjection } from "@/lib/authzed/projection-boundary";
import { reconcileTeamWorkspaceRelationships } from "@/lib/authzed/team-workspace";
import { DEFAULT_LOCALE } from "@/lib/constants";
import { isPrismaKnownRequestError, isUniqueConstraintError } from "@/lib/utils/prisma-error";
import { validateInputs } from "@/lib/utils/validate";
import { getWorkspaceLegacyStoragePrefixes } from "@/lib/workspace/service";
import { requestEmailAuditErasureRequired } from "@/modules/email/lib/email-audit-maintenance";
import { deleteFile, deleteWorkspaceFilesBestEffort } from "@/modules/storage/service";
import { parseStorageFileUrl } from "@/modules/storage/utils";

// Keep v5 defaults aligned with current production camelCase keys.
// Safe-identifier migration (with backwards compatibility) is intentionally deferred to v5.1.
const DEFAULT_CONTACT_ATTRIBUTE_KEYS: Prisma.ContactAttributeKeyCreateWithoutWorkspaceInput[] = [
  {
    key: "userId",
    name: "User Id",
    description: "The user id of a contact",
    type: "default",
    isUnique: true,
  },
  {
    key: "email",
    name: "Email",
    description: "The email of a contact",
    type: "default",
    isUnique: true,
  },
  {
    key: "firstName",
    name: "First Name",
    description: "Your contact's first name",
    type: "default",
  },
  {
    key: "lastName",
    name: "Last Name",
    description: "Your contact's last name",
    type: "default",
  },
  {
    key: "language",
    name: "Language",
    description: "The language preference of a contact",
    type: "default",
  },
];

const DEFAULT_WORKSPACE_LANGUAGE: Prisma.LanguageCreateWithoutWorkspaceInput = {
  code: DEFAULT_LOCALE,
  alias: null,
};

const selectWorkspace = {
  id: true,
  createdAt: true,
  updatedAt: true,
  name: true,
  organizationId: true,
  languages: true,
  recontactDays: true,
  linkSurveyBranding: true,
  inAppSurveyBranding: true,
  config: true,
  placement: true,
  clickOutsideClose: true,
  overlay: true,
  overlayColor: true,
  overlayOpacity: true,
  appSetupCompleted: true,
  styling: true,
  logo: true,
  customHeadScripts: true,
};

// Identifies an object by what it actually resolves to in the bucket, so two URLs that differ only
// in origin or percent-encoding still compare equal.
const storageObjectKey = (fileUrl: string): string | null => {
  const parsed = parseStorageFileUrl(fileUrl);
  if (!parsed) return null;

  try {
    return `${parsed.storageId}/${parsed.accessType}/${decodeURIComponent(parsed.fileName)}`;
  } catch {
    // `logo.url` is caller-supplied and nothing upstream validates percent escapes, so a name like
    // `bad%zz.png` reaches this far and makes decodeURIComponent throw. The caller is compared and
    // cleaned up *after* the workspace write has committed, so throwing here would report a save
    // that actually succeeded as failed. An unresolvable name is simply not a key we can match.
    return null;
  }
};

/**
 * Whether the object is one the organization still points at.
 *
 * Organization favicons and email logos upload through the same `/api/v1/management/storage` route
 * as workspace logos, so they land under `{workspaceId}/public/` and clear both guards below.
 * Changing them takes `organization.manage`; this path takes only `workspace.manage`. Without this
 * check a workspace manager could point the workspace logo at an organization asset, clear it, and
 * delete a file they have no permission to touch — while the organization row still references it.
 */
const isOrganizationOwnedAsset = async (organizationId: string, objectKey: string): Promise<boolean> => {
  const organization = await prisma.organization.findUnique({
    where: { id: organizationId },
    select: { whitelabel: true },
  });

  const whitelabel = organization?.whitelabel;
  if (!whitelabel) return false;

  return [whitelabel.logoUrl, whitelabel.faviconUrl].some(
    (url) => url && storageObjectKey(url) === objectKey
  );
};

/**
 * Deletes the storage object a removed or replaced workspace logo used to point at.
 *
 * `logo.url` arrives through the update input and `parseStorageFileUrl` derives the storage id from
 * the URL itself without checking its origin, so that id alone decides whose object gets deleted.
 * Acting on it unguarded would be a cross-tenant delete primitive: point the logo at another
 * workspace's storage URL, clear it, and the app removes their file. So the parsed id is checked
 * against the prefixes this workspace actually owns first — the same guard ENG-1981 and ENG-2258
 * put on response files.
 *
 * Best-effort: the user asked to change the logo, and the database already says so. A storage
 * failure is logged and never thrown, or a dead bucket would fail an update that did happen.
 */
const deleteOrphanedWorkspaceLogoFile = async (
  workspaceId: string,
  organizationId: string,
  previousUrl: string
) => {
  try {
    const storageFile = parseStorageFileUrl(previousUrl);
    // An external logo URL (a CDN link, which the UI supports) is not ours to delete.
    if (!storageFile) return;

    const ownedPrefixes = await getWorkspaceLegacyStoragePrefixes(workspaceId);
    if (!ownedPrefixes.includes(storageFile.storageId)) {
      logger.error(
        { workspaceId, storageId: storageFile.storageId },
        "Refusing to delete a workspace logo stored outside the workspace"
      );
      return;
    }

    // Owning the prefix says the object is this workspace's, not that it is a logo — `logo.url` is
    // caller-supplied, so it can name any object under that prefix. Logo uploads always go through
    // the management storage route, which issues `public` keys, so refusing anything else keeps this
    // path away from `private/` objects (response attachments) even when the url is chosen.
    if (storageFile.accessType !== "public") {
      logger.error(
        { workspaceId, accessType: storageFile.accessType },
        "Refusing to delete a non-public object through workspace logo cleanup"
      );
      return;
    }

    const objectKey = `${storageFile.storageId}/${storageFile.accessType}/${decodeURIComponent(storageFile.fileName)}`;
    if (await isOrganizationOwnedAsset(organizationId, objectKey)) {
      logger.error(
        { workspaceId, organizationId },
        "Refusing to delete an organization-owned asset through workspace logo cleanup"
      );
      return;
    }

    // Upload percent-encodes the file name into the URL, but the object is stored under the decoded
    // name, so a logo called "my logo.png" misses its key entirely unless it is decoded here.
    const result = await deleteFile(
      storageFile.storageId,
      storageFile.accessType,
      decodeURIComponent(storageFile.fileName)
    );

    if (!result.ok) {
      logger.error({ workspaceId, error: result.error }, "Failed to delete the previous workspace logo");
    }
  } catch (error) {
    logger.error({ error, workspaceId }, "Failed to delete the previous workspace logo");
  }
};

export const updateWorkspace = async (
  workspaceId: string,
  inputWorkspace: TWorkspaceUpdateInput
): Promise<TWorkspace> => {
  validateInputs([workspaceId, ZId], [inputWorkspace, ZWorkspaceUpdateInput]);
  // ENG-1919: organizationId is the workspace's tenant anchor, set at creation and immutable on
  // update. Persisting a caller-supplied organizationId here would let an authorized workspace
  // owner move their workspace (and all its data) into another organization, so it is stripped.
  // expectedUpdatedAt is a concurrency baseline, not a column — writing it would be nonsense and
  // would also fight Prisma's own @updatedAt.
  //
  // ENG-2949: custom CSS is written only by `updateWorkspaceCustomCss`, which checks the stricter role,
  // the plan and the processor. `ZWorkspaceUpdateInput` has no such keys, but validation above does not
  // replace the input, so a caller passing them would otherwise reach the write untouched.
  const {
    organizationId: _organizationId,
    expectedUpdatedAt,
    customCss: _customCss,
    customCssPrevious: _customCssPrevious,
    ...data
  } = inputWorkspace as TWorkspaceUpdateInput & { customCss?: unknown; customCssPrevious?: unknown };
  let updatedWorkspace;
  let previousLogoUrl: string | undefined;
  try {
    // Only an update carrying a logo can orphan one, so a rename or a styling change costs no read.
    if ("logo" in inputWorkspace) {
      // Required rather than optional: an opt-in guard protects nobody who forgets it, and a save
      // with no baseline is exactly the stale write that restores a url whose object is gone.
      if (!expectedUpdatedAt) {
        throw new ValidationError("expectedUpdatedAt is required when the update carries a logo");
      }

      // Read, version check and write are one transaction over a locked row. Without it, two saves
      // that both loaded logo A interleave: the replace writes C and deletes A, then the stale save
      // restores A, leaving the row pointing at an object that no longer exists.
      ({ updatedWorkspace, previousLogoUrl } = await prisma.$transaction(async (tx) => {
        const [current] = await tx.$queryRaw<{ logo: TLogo | null; updatedAt: Date }[]>`
          SELECT "logo", "updated_at" AS "updatedAt"
          FROM "Workspace"
          WHERE "id" = ${workspaceId}
          FOR UPDATE
        `;

        if (!current) {
          throw new ResourceNotFoundError("workspace", workspaceId);
        }

        if (current.updatedAt.getTime() !== expectedUpdatedAt.getTime()) {
          throw new OperationNotAllowedError(
            "This workspace was changed somewhere else. Reload the page and try again."
          );
        }

        return {
          updatedWorkspace: await tx.workspace.update({
            where: { id: workspaceId },
            data,
            select: selectWorkspace,
          }),
          previousLogoUrl: current.logo?.url,
        };
      }));
    } else {
      updatedWorkspace = await prisma.workspace.update({
        where: {
          id: workspaceId,
        },
        data,
        select: selectWorkspace,
      });
    }
  } catch (error) {
    if (isPrismaKnownRequestError(error)) {
      throw new DatabaseError(error.message);
    }
    throw error;
  }

  await runPostCommitProjection("workspace_update", () =>
    reconcileTeamWorkspaceRelationships({ workspaceIds: [workspaceId] })
  );

  // Compared against what was actually persisted, not against the input: Prisma treats
  // `logo: undefined` as "leave this field alone", so trusting the input would delete the object
  // while the row still points at it. Every upload gets a unique `--fid--{uuid}` key, so the old
  // object has exactly one referrer and losing it orphans the file.
  //
  // Compared on the resolved object key, not the raw url: one object can be named by an absolute
  // and a relative url, and with the file name percent-encoded or not, so a string compare would
  // read a re-spelled url as a change and delete the object the row still points at.
  const previousObjectKey = previousLogoUrl ? storageObjectKey(previousLogoUrl) : null;
  const persistedObjectKey = updatedWorkspace.logo?.url ? storageObjectKey(updatedWorkspace.logo.url) : null;

  if (previousLogoUrl && previousObjectKey !== persistedObjectKey) {
    await deleteOrphanedWorkspaceLogoFile(workspaceId, updatedWorkspace.organizationId, previousLogoUrl);
  }

  return updatedWorkspace;
};

/**
 * Enforces the organization's workspace cap inside the creating transaction (ENG-3921).
 *
 * A count read outside the transaction is a check-then-act race: a burst of concurrent creates all
 * read the same count before any of them commits, so every one passes and the organization lands as
 * far past its cap as the burst is wide. Locking the organization row first makes concurrent creates
 * for one organization take turns, and under READ COMMITTED the count that follows sees every
 * workspace an earlier lock holder committed.
 *
 * The parent row is locked rather than the workspaces because it always exists: `FOR UPDATE` over an
 * organization's workspaces locks nothing while it has none. FOR NO KEY UPDATE (not FOR UPDATE)
 * because concurrent creates only need to serialize with each other; it does not conflict with the
 * FOR KEY SHARE lock every insert of an organization-owned row takes on this row through its foreign
 * key, so teams, memberships and invites are not held up behind a workspace create. It must be the
 * transaction's first lock: the caller holds nothing yet while it waits, so it cannot close a
 * deadlock cycle.
 */
const assertWorkspaceLimitNotReached = async (
  tx: Prisma.TransactionClient,
  organizationId: string,
  workspaceLimit: number
): Promise<void> => {
  const organizations = await tx.$queryRaw<Array<{ id: string }>>`
    SELECT "id"
    FROM "Organization"
    WHERE "id" = ${organizationId}
    FOR NO KEY UPDATE
  `;

  if (organizations.length === 0) {
    throw new ResourceNotFoundError("Organization", organizationId);
  }

  const workspaceCount = await tx.workspace.count({ where: { organizationId } });

  if (workspaceCount >= workspaceLimit) {
    throw new OperationNotAllowedError("Organization workspace limit reached");
  }
};

export type TCreateWorkspaceOptions = {
  /**
   * The most workspaces the organization may own once this one exists, as resolved by
   * `getOrganizationWorkspacesLimit`. Resolve it before calling: it can read through to Stripe, and
   * that must not happen while the organization row is locked. `Infinity` means unlimited.
   *
   * `null` only for the first workspace of an organization the caller has just created, which owns
   * none yet. It is required rather than optional so a new path that adds a workspace to an existing
   * organization cannot skip the cap by omission.
   */
  workspaceLimit: number | null;
};

export const createWorkspace = async (
  organizationId: string,
  workspaceInput: TWorkspaceUpdateInput,
  { workspaceLimit }: TCreateWorkspaceOptions
): Promise<TWorkspace> => {
  validateInputs([organizationId, ZId], [workspaceInput, ZWorkspaceUpdateInput]);

  if (!workspaceInput.name) {
    throw new ValidationError("Workspace Name is required");
  }

  // expectedUpdatedAt shares ZWorkspaceUpdateInput with the update path but is a concurrency
  // baseline, not a column; leaving it in would hand Prisma an unknown field on create.
  // ENG-2949: a new workspace starts without custom CSS; see `updateWorkspace` for why it is stripped.
  const {
    teamIds,
    config: configInput,
    expectedUpdatedAt: _expectedUpdatedAt,
    customCss: _customCss,
    customCssPrevious: _customCssPrevious,
    ...data
  } = workspaceInput as TWorkspaceUpdateInput & { customCss?: unknown; customCssPrevious?: unknown };
  // Captured out here so the guard above still narrows it: inside the transaction callback below,
  // TypeScript widens workspaceInput.name back to `string | undefined`.
  const name = workspaceInput.name;
  let workspace: TWorkspace;

  try {
    // The cap check, the ownership check and both writes share one transaction: it keeps each check
    // atomic with the write it guards (the cap cannot be raced past, a team cannot leave the
    // organization in between), and stops a failed createMany from leaving an orphan workspace with no
    // team links.
    workspace = (await prisma.$transaction(async (tx) => {
      if (workspaceLimit !== null && Number.isFinite(workspaceLimit)) {
        await assertWorkspaceLimitNotReached(tx, organizationId, workspaceLimit);
      }

      // ENG-1922: teamIds are caller-supplied. Validate that every team belongs to this
      // organization before linking it — otherwise a caller could attach another org's team
      // to their workspace (a cross-tenant WorkspaceTeam write). The FK only enforces that the
      // team exists, not that it belongs here, so this must be checked at the app layer. A
      // foreign-but-real id and a nonexistent id fail identically, so this is not an
      // existence oracle for other orgs' teams.
      if (teamIds && teamIds.length > 0) {
        const uniqueTeamIds = new Set(teamIds);
        if (uniqueTeamIds.size !== teamIds.length) {
          throw new ValidationError("teamIds must be unique");
        }
        const teams = await tx.team.findMany({
          where: { id: { in: teamIds }, organizationId },
          select: { id: true },
        });
        if (teams.length !== uniqueTeamIds.size) {
          const foundTeamIds = new Set(teams.map((team) => team.id));
          const foreignTeamIds = [...uniqueTeamIds].filter((teamId) => !foundTeamIds.has(teamId));
          // ENG-1922: log the rejected cross-organization attempt for security observability.
          // Only tenant identifiers (org id + the caller-supplied team ids) are logged — no PII.
          logger.warn(
            { organizationId, foreignTeamIds },
            "Rejected cross-organization team assignment on workspace creation (ENG-1922)"
          );
          throw new ValidationError("teamIds must belong to the organization");
        }
      }

      const workspace = await tx.workspace.create({
        data: {
          ...data,
          // Page font (ENG-3864): on for every new workspace. Workspaces from before it have no flag and
          // keep the survey font, which is why the column default and STYLE_DEFAULTS leave it out.
          styling: {
            ...(data.styling ?? { allowStyleOverwrite: true }),
            pageFont: data.styling?.pageFont ?? true,
          },
          // Built explicitly rather than spread from the caller: the default survey language has to be
          // one of the workspace's own languages, and a workspace being created has only the seeded
          // `DEFAULT_WORKSPACE_LANGUAGE`. It is configured afterwards from workspace settings, where
          // `updateWorkspaceAction` can validate it against the languages that exist (ENG-2816).
          config: {
            channel: configInput?.channel ?? null,
            industry: configInput?.industry ?? null,
          },
          name,
          organizationId,
          contactAttributeKeys: {
            create: DEFAULT_CONTACT_ATTRIBUTE_KEYS,
          },
          languages: {
            create: [DEFAULT_WORKSPACE_LANGUAGE],
          },
        },
        select: selectWorkspace,
      });

      if (teamIds) {
        await tx.workspaceTeam.createMany({
          data: teamIds.map((teamId) => ({
            workspaceId: workspace.id,
            teamId,
          })),
        });
      }

      return workspace;
    })) as TWorkspace;
  } catch (error) {
    if (isUniqueConstraintError(error)) {
      throw new InvalidInputError("A workspace with this name already exists in your organization");
    }
    if (isPrismaKnownRequestError(error)) {
      throw new DatabaseError(error.message);
    }
    throw error;
  }

  await runPostCommitProjection("workspace_create", () =>
    reconcileTeamWorkspaceRelationships({
      workspaceIds: [workspace.id],
      workspaceTeamGrants: (teamIds ?? []).map((teamId) => ({ teamId, workspaceId: workspace.id })),
    })
  );

  return workspace;
};

type TWorkspaceDeletionDbClient = Prisma.TransactionClient;

const deleteWorkspaceRecord = async (db: TWorkspaceDeletionDbClient, workspaceId: string) => {
  const feedbackDirectoryAssignments = await db.feedbackDirectoryWorkspace.findMany({
    where: { workspaceId },
    select: { feedbackDirectoryId: true, workspaceId: true },
  });
  await requestEmailAuditErasureRequired({ workspaceId }, db);
  const workspace = await db.workspace.delete({
    where: {
      id: workspaceId,
    },
    // legacyEnvironmentId rides along on the deleted row so storage cleanup has the legacy prefix
    // after the row is gone. It is not part of selectWorkspace because no other caller needs it.
    select: { ...selectWorkspace, legacyEnvironmentId: true },
  });

  return { feedbackDirectoryAssignments, workspace };
};

const completeWorkspaceDeletion = async (
  workspaceId: string,
  { feedbackDirectoryAssignments, workspace }: Awaited<ReturnType<typeof deleteWorkspaceRecord>>
) => {
  await runPostCommitProjection("workspace_delete", () =>
    reconcileTeamWorkspaceRelationships({ workspaceIds: [workspaceId] })
  );
  await runPostCommitProjection("workspace_delete_feedback_directory_cleanup", () =>
    reconcileFeedbackDirectoryRelationships({ assignments: feedbackDirectoryAssignments })
  );

  await deleteWorkspaceFilesBestEffort(workspace);

  return workspace;
};

const throwWorkspaceDeletionError = (error: unknown): never => {
  if (isPrismaKnownRequestError(error)) {
    throw new DatabaseError(error.message);
  }

  throw error;
};

export const deleteWorkspace = async (workspaceId: string): Promise<TWorkspace> => {
  try {
    const deletion = await prisma.$transaction((tx) => deleteWorkspaceRecord(tx, workspaceId), {
      timeout: 60_000,
    });
    return await completeWorkspaceDeletion(workspaceId, deletion);
  } catch (error) {
    return throwWorkspaceDeletionError(error);
  }
};

export const deleteWorkspaceIfNotLast = async (
  workspaceId: string,
  organizationId: string
): Promise<TWorkspace> => {
  try {
    const deletion = await prisma.$transaction(async (tx) => {
      // Lock every workspace in a stable order. Concurrent deletion requests for the organization
      // then serialize, so the second request sees the first deletion before evaluating the guard.
      const workspaces = await tx.$queryRaw<Array<{ id: string }>>`
        SELECT "id"
        FROM "Workspace"
        WHERE "organizationId" = ${organizationId}
        ORDER BY "id"
        FOR UPDATE
      `;

      if (!workspaces.some((workspace) => workspace.id === workspaceId)) {
        throw new ResourceNotFoundError("workspace", workspaceId);
      }

      if (workspaces.length <= 1) {
        throw new OperationNotAllowedError("You can't delete the last workspace.");
      }

      return deleteWorkspaceRecord(tx, workspaceId);
    });

    return await completeWorkspaceDeletion(workspaceId, deletion);
  } catch (error) {
    return throwWorkspaceDeletionError(error);
  }
};
