import "server-only";
import { cache as reactCache } from "react";
import { prisma } from "@formbricks/database";
import { Prisma } from "@formbricks/database/prisma";
import type { TAuthenticationApiKey } from "@formbricks/types/auth";
import { DatabaseError } from "@formbricks/types/errors";
import { getFeedbackDirectoryAssignmentObjectId } from "@/lib/authzed/feedback-directory-assignment-id";

/**
 * Light, cached lookups the legacy authorization evaluator uses to walk a
 * resource up to the boundary its permission is enforced at (a workspace, an
 * organization). They select only the id needed — never the full record — so
 * an authorization check never over-fetches. They wrap `react.cache` for
 * best-effort request-scoped dedup during Server Component render; they are
 * deliberately NOT cached across requests (`cache.withCache`/Redis) because
 * these are cheap indexed lookups and stale authorization data would be a
 * security bug. Operational failures propagate as `DatabaseError`; a missing
 * record resolves to `null` (a genuine "no such resource", which the evaluator
 * treats as a denial, never an error).
 */

const rethrowAsDatabaseError = (error: unknown): never => {
  if (error instanceof Prisma.PrismaClientKnownRequestError) {
    throw new DatabaseError(error.message);
  }
  throw error;
};

export type TAuthorizationWorkspaceScope = Readonly<{
  organizationId: string;
  workspaceId: string;
}>;

export type TFeedbackDirectoryAuthorizationScope = Readonly<{
  isArchived: boolean;
  organizationId: string;
  workspaceIds: ReadonlyArray<string>;
}>;

export type TFeedbackDirectoryAssignmentAuthorizationScope = Readonly<{
  assignmentId: string;
  organizationId: string;
  workspaceId: string;
}>;

export type TWorkspaceOrganizationReference = Readonly<{
  id: string;
  organizationId: string;
}>;

const WORKSPACE_ORGANIZATION_RESOLUTION_BATCH_SIZE = 500;

/** Whether a user principal still exists and is active. */
export const isAuthorizationUserActive = reactCache(async (userId: string): Promise<boolean> => {
  try {
    const user = await prisma.user.findUnique({
      where: { id: userId },
      select: { isActive: true },
    });
    return user?.isActive === true;
  } catch (error) {
    return rethrowAsDatabaseError(error);
  }
});

/** The active subset of one bounded authorization batch. No cross-request cache. */
export const getActiveAuthorizationUserIds = async (
  userIds: ReadonlyArray<string>
): Promise<ReadonlySet<string>> => {
  try {
    const users = await prisma.user.findMany({
      where: { id: { in: [...userIds] }, isActive: true },
      select: { id: true },
    });
    return new Set(users.map(({ id }) => id));
  } catch (error) {
    return rethrowAsDatabaseError(error);
  }
};

/** Whether an organization resource still exists. */
export const getAuthorizationOrganizationId = reactCache(
  async (organizationId: string): Promise<string | null> => {
    try {
      const organization = await prisma.organization.findUnique({
        where: { id: organizationId },
        select: { id: true },
      });
      return organization?.id ?? null;
    } catch (error) {
      return rethrowAsDatabaseError(error);
    }
  }
);

/** The organization a workspace belongs to (`Workspace.organizationId`). */
export const getWorkspaceOrganizationId = reactCache(async (workspaceId: string): Promise<string | null> => {
  try {
    const workspace = await prisma.workspace.findUnique({
      where: { id: workspaceId },
      select: { organizationId: true },
    });
    return workspace?.organizationId ?? null;
  } catch (error) {
    return rethrowAsDatabaseError(error);
  }
});

/**
 * Resolve workspace IDs to organizations in bounded `IN` queries.
 *
 * LookupResources returns a stream-sized set. Resolving that set one row at a time would turn one
 * authorization observation into an N+1 database path, so this helper keeps the number of queries
 * proportional to bounded chunks instead of result cardinality.
 */
export const getWorkspaceOrganizationReferences = async (
  workspaceIds: ReadonlyArray<string>
): Promise<ReadonlyArray<TWorkspaceOrganizationReference>> => {
  const uniqueWorkspaceIds = [...new Set(workspaceIds)];
  if (uniqueWorkspaceIds.length === 0) return [];

  try {
    const batches: string[][] = [];
    for (
      let offset = 0;
      offset < uniqueWorkspaceIds.length;
      offset += WORKSPACE_ORGANIZATION_RESOLUTION_BATCH_SIZE
    ) {
      batches.push(uniqueWorkspaceIds.slice(offset, offset + WORKSPACE_ORGANIZATION_RESOLUTION_BATCH_SIZE));
    }

    const references = await Promise.all(
      batches.map((batch) =>
        prisma.workspace.findMany({
          where: { id: { in: batch } },
          select: { id: true, organizationId: true },
        })
      )
    );
    return references.flat();
  } catch (error) {
    return rethrowAsDatabaseError(error);
  }
};

/** The workspace and organization a survey belongs to. */
export const getSurveyAuthorizationWorkspaceScope = reactCache(
  async (surveyId: string): Promise<TAuthorizationWorkspaceScope | null> => {
    try {
      const survey = await prisma.survey.findUnique({
        where: { id: surveyId },
        select: {
          workspaceId: true,
          workspace: { select: { organizationId: true } },
        },
      });
      return survey
        ? { organizationId: survey.workspace.organizationId, workspaceId: survey.workspaceId }
        : null;
    } catch (error) {
      return rethrowAsDatabaseError(error);
    }
  }
);

/**
 * A survey's authorization facts once survey visibility is enforced (ENG-3282): its tenant boundary
 * plus the ownership and version pair the evaluator needs to tell a settled survey from one whose
 * visibility change is still being projected.
 */
export type TSurveyAuthorizationScopeRow = Readonly<{
  id: string;
  organizationId: string;
  ownerId: string | null;
  visibility: "restricted" | "workspace";
  visibilityProjectedVersion: number;
  visibilityVersion: number;
  workspaceId: string;
}>;

export const getSurveyAuthorizationScopeRow = reactCache(
  async (surveyId: string): Promise<TSurveyAuthorizationScopeRow | null> => {
    try {
      const survey = await prisma.survey.findUnique({
        where: { id: surveyId },
        select: {
          id: true,
          ownerId: true,
          visibility: true,
          visibilityProjectedVersion: true,
          visibilityVersion: true,
          workspaceId: true,
          workspace: { select: { organizationId: true } },
        },
      });
      if (!survey) return null;
      const { workspace, ...facts } = survey;
      return { ...facts, organizationId: workspace.organizationId };
    } catch (error) {
      return rethrowAsDatabaseError(error);
    }
  }
);

/** The workspace and organization a dashboard belongs to. */
export const getDashboardAuthorizationWorkspaceScope = reactCache(
  async (dashboardId: string): Promise<TAuthorizationWorkspaceScope | null> => {
    try {
      const dashboard = await prisma.dashboard.findUnique({
        where: { id: dashboardId },
        select: {
          workspaceId: true,
          workspace: { select: { organizationId: true } },
        },
      });
      return dashboard
        ? { organizationId: dashboard.workspace.organizationId, workspaceId: dashboard.workspaceId }
        : null;
    } catch (error) {
      return rethrowAsDatabaseError(error);
    }
  }
);

/** The workspace and organization a response's survey belongs to. */
export const getResponseAuthorizationWorkspaceScope = reactCache(
  async (responseId: string): Promise<TAuthorizationWorkspaceScope | null> => {
    try {
      const response = await prisma.response.findUnique({
        where: { id: responseId },
        select: {
          survey: {
            select: {
              workspaceId: true,
              workspace: { select: { organizationId: true } },
            },
          },
        },
      });
      return response
        ? {
            organizationId: response.survey.workspace.organizationId,
            workspaceId: response.survey.workspaceId,
          }
        : null;
    } catch (error) {
      return rethrowAsDatabaseError(error);
    }
  }
);

/** The workspace a survey belongs to (`Survey.workspaceId`). */
export const getSurveyWorkspaceId = reactCache(async (surveyId: string): Promise<string | null> => {
  try {
    const survey = await prisma.survey.findUnique({
      where: { id: surveyId },
      select: { workspaceId: true },
    });
    return survey?.workspaceId ?? null;
  } catch (error) {
    return rethrowAsDatabaseError(error);
  }
});

/** The workspace a dashboard belongs to (`Dashboard.workspaceId`). */
export const getDashboardWorkspaceId = reactCache(async (dashboardId: string): Promise<string | null> => {
  try {
    const dashboard = await prisma.dashboard.findUnique({
      where: { id: dashboardId },
      select: { workspaceId: true },
    });
    return dashboard?.workspaceId ?? null;
  } catch (error) {
    return rethrowAsDatabaseError(error);
  }
});

/** The survey a response belongs to (`Response.surveyId`). */
export const getResponseSurveyId = reactCache(async (responseId: string): Promise<string | null> => {
  try {
    const response = await prisma.response.findUnique({
      where: { id: responseId },
      select: { surveyId: true },
    });
    return response?.surveyId ?? null;
  } catch (error) {
    return rethrowAsDatabaseError(error);
  }
});

/** The organization a team belongs to (`Team.organizationId`). */
export const getTeamOrganizationId = reactCache(async (teamId: string): Promise<string | null> => {
  try {
    const team = await prisma.team.findUnique({
      where: { id: teamId },
      select: { organizationId: true },
    });
    return team?.organizationId ?? null;
  } catch (error) {
    return rethrowAsDatabaseError(error);
  }
});

/** The organization an API key belongs to (`ApiKey.organizationId`). */
export const getApiKeyOrganizationId = reactCache(async (apiKeyId: string): Promise<string | null> => {
  try {
    const apiKey = await prisma.apiKey.findUnique({
      where: { id: apiKeyId },
      select: { organizationId: true },
    });
    return apiKey?.organizationId ?? null;
  } catch (error) {
    return rethrowAsDatabaseError(error);
  }
});

export const getFeedbackDirectoryAuthorizationScope = reactCache(
  async (feedbackDirectoryId: string): Promise<TFeedbackDirectoryAuthorizationScope | null> => {
    try {
      const directory = await prisma.feedbackDirectory.findUnique({
        where: { id: feedbackDirectoryId },
        select: {
          isArchived: true,
          organizationId: true,
          workspaces: { select: { workspaceId: true }, orderBy: { workspaceId: "asc" } },
        },
      });
      return directory
        ? {
            isArchived: directory.isArchived,
            organizationId: directory.organizationId,
            workspaceIds: directory.workspaces.map(({ workspaceId }) => workspaceId),
          }
        : null;
    } catch (error) {
      return rethrowAsDatabaseError(error);
    }
  }
);

export const getFeedbackDirectoryAssignmentAuthorizationScope = reactCache(
  async (
    feedbackDirectoryId: string,
    workspaceId: string
  ): Promise<TFeedbackDirectoryAssignmentAuthorizationScope | null> => {
    try {
      const assignment = await prisma.feedbackDirectoryWorkspace.findUnique({
        where: {
          feedbackDirectoryId_workspaceId: { feedbackDirectoryId, workspaceId },
        },
        select: {
          feedbackDirectory: { select: { isArchived: true, organizationId: true } },
          workspace: { select: { organizationId: true } },
        },
      });
      if (
        !assignment ||
        assignment.feedbackDirectory.isArchived ||
        assignment.feedbackDirectory.organizationId !== assignment.workspace.organizationId
      ) {
        return null;
      }

      return {
        assignmentId: getFeedbackDirectoryAssignmentObjectId(feedbackDirectoryId, workspaceId),
        organizationId: assignment.feedbackDirectory.organizationId,
        workspaceId,
      };
    } catch (error) {
      return rethrowAsDatabaseError(error);
    }
  }
);

/**
 * Resolve an API key acting as a principal (by `ApiKey.id`) into its effective
 * scopes: per-workspace permissions and organization-level access control. This
 * is the by-id counterpart to `getApiKeyWithPermissions`, which resolves by raw
 * secret during request authentication. Returns `null` if the key is gone.
 */
export const getApiKeyAuthById = reactCache(
  async (apiKeyId: string): Promise<TAuthenticationApiKey | null> => {
    try {
      const apiKey = await prisma.apiKey.findUnique({
        where: { id: apiKeyId },
        select: {
          id: true,
          organizationId: true,
          organizationAccess: true,
          apiKeyWorkspaces: {
            select: {
              permission: true,
              workspaceId: true,
              workspace: { select: { name: true, organizationId: true } },
            },
          },
        },
      });

      if (!apiKey) return null;

      return {
        type: "apiKey",
        apiKeyId: apiKey.id,
        organizationId: apiKey.organizationId,
        organizationAccess: apiKey.organizationAccess as TAuthenticationApiKey["organizationAccess"],
        workspacePermissions: apiKey.apiKeyWorkspaces
          .filter(
            (workspacePermission) => workspacePermission.workspace.organizationId === apiKey.organizationId
          )
          .map((workspacePermission) => ({
            permission: workspacePermission.permission,
            workspaceId: workspacePermission.workspaceId,
            workspaceName: workspacePermission.workspace.name,
          })),
      };
    } catch (error) {
      return rethrowAsDatabaseError(error);
    }
  }
);
