import { isDeepStrictEqual } from 'node:util';
import {
  BadRequestException,
  ConflictException,
  Injectable,
  Logger,
  NotFoundException,
} from '@nestjs/common';
import { CreatePageDto, ContentFormat } from '../dto/create-page.dto';
import { ContentOperation, UpdatePageDto } from '../dto/update-page.dto';
import { PageRepo } from '@docmost/db/repos/page/page.repo';
import { PagePermissionRepo } from '@docmost/db/repos/page/page-permission.repo';
import {
  InsertablePage,
  Page,
  Share,
  UpdatablePage,
  User,
} from '@docmost/db/types/entity.types';
import { PaginationOptions } from '@docmost/db/pagination/pagination-options';
import {
  CursorPaginationResult,
  executeWithCursorPagination,
} from '@docmost/db/pagination/cursor-pagination';
import { InjectKysely } from 'nestjs-kysely';
import { KyselyDB, KyselyTransaction } from '@docmost/db/types/kysely.types';
import { generateJitteredKeyBetween } from 'fractional-indexing-jittered';
import { MovePageDto } from '../dto/move-page.dto';
import { generateSlugId } from '../../../common/helpers';
import { getPageTitle } from '../../../common/helpers';
import { dbOrTx, executeTx } from '@docmost/db/utils';
import { AttachmentRepo } from '@docmost/db/repos/attachment/attachment.repo';
import { v7 as uuid7 } from 'uuid';
import {
  createYdocFromJson,
  getAttachmentIds,
  getProsemirrorContent,
  replaceAttachmentIds,
  removeMarkTypeFromDoc,
} from '../../../common/helpers/prosemirror/utils';
import {
  htmlToJson,
  jsonToNode,
  jsonToText,
  parseDocumentJson,
  templateExtensions,
  tiptapExtensions,
} from 'src/collaboration/collaboration.util';
import { CopyPageMapEntry } from '../dto/duplicate-page.dto';
import { Node as PMNode } from '@tiptap/pm/model';
import { StorageService } from '../../../integrations/storage/storage.service';
import { InjectQueue } from '@nestjs/bullmq';
import { Queue } from 'bullmq';
import { QueueJob, QueueName } from '../../../integrations/queue/constants';
import { EventName } from '../../../common/events/event.contants';
import { EventEmitter2 } from '@nestjs/event-emitter';
import { CollaborationGateway } from '../../../collaboration/collaboration.gateway';
import {
  INTERNAL_LINK_REGEX,
  extractPageSlugId,
} from '../../../integrations/export/utils';
import { markdownToHtml } from '@docmost/editor-ext';
import { WatcherService } from '../../watcher/watcher.service';
import { sql } from 'kysely';
import { TransclusionService } from '../transclusion/transclusion.service';
import { LabelRepo } from '@docmost/db/repos/label/label.repo';
import { WsService } from '../../../ws/ws.service';
import { CollabAccessService } from '../../../collaboration/services/collab-access.service';
import { ShareService } from '../../share/share.service';
import {
  buildCoverFileUrl,
  getCoverAttachmentId,
  getUnsplashPhotoId,
  isPresetCover,
  queueCoverCleanup,
  remapCopiedCover,
} from '../page-cover.utils';
import { COVER_IMAGE_MIME_TYPES } from '../page-cover.constants';
import { CoverFields } from '../page-cover.types';
import { UnsplashService } from '../../../integrations/unsplash/unsplash.service';
import { AttachmentType } from '../../attachment/attachment.constants';
import { PageVerificationInvalidationService } from '../verification/page-verification-invalidation.service';
import { isSameTitle } from '../verification/page-verification-invalidation.utils';

@Injectable()
export class PageService {
  private readonly logger = new Logger(PageService.name);

  constructor(
    private pageRepo: PageRepo,
    private pagePermissionRepo: PagePermissionRepo,
    private attachmentRepo: AttachmentRepo,
    @InjectKysely() private readonly db: KyselyDB,
    private readonly storageService: StorageService,
    @InjectQueue(QueueName.ATTACHMENT_QUEUE) private attachmentQueue: Queue,
    @InjectQueue(QueueName.AI_QUEUE) private aiQueue: Queue,
    @InjectQueue(QueueName.GENERAL_QUEUE) private generalQueue: Queue,
    private eventEmitter: EventEmitter2,
    private collaborationGateway: CollaborationGateway,
    private readonly watcherService: WatcherService,
    private readonly transclusionService: TransclusionService,
    private readonly labelRepo: LabelRepo,
    private readonly wsService: WsService,
    private readonly collabAccessService: CollabAccessService,
    private readonly shareService: ShareService,
    private readonly unsplashService: UnsplashService,
    private readonly pageVerificationInvalidationService: PageVerificationInvalidationService,
  ) {}

  async findById(
    pageId: string,
    includeContent?: boolean,
    includeYdoc?: boolean,
    includeSpace?: boolean,
  ): Promise<Page> {
    return this.pageRepo.findById(pageId, {
      includeContent,
      includeYdoc,
      includeSpace,
    });
  }

  async create(
    userId: string,
    workspaceId: string,
    createPageDto: CreatePageDto,
    trx?: KyselyTransaction,
    isBase: boolean = false,
  ): Promise<Page> {
    let parentPageId = undefined;

    // check if parent page exists
    if (createPageDto.parentPageId) {
      const parentPage = await this.pageRepo.findById(
        createPageDto.parentPageId,
      );

      if (
        !parentPage ||
        parentPage.deletedAt ||
        parentPage.spaceId !== createPageDto.spaceId
      ) {
        throw new NotFoundException('Parent page not found');
      }

      parentPageId = parentPage.id;
    }

    let content = undefined;
    let textContent = undefined;
    let ydoc = undefined;

    if (createPageDto?.content && createPageDto?.format) {
      const prosemirrorJson = await this.parseProsemirrorContent(
        createPageDto.content,
        createPageDto.format,
      );

      content = prosemirrorJson;
      textContent = jsonToText(prosemirrorJson);
      ydoc = createYdocFromJson(prosemirrorJson);
    }

    const page = await this.pageRepo.insertPage({
      slugId: generateSlugId(),
      title: createPageDto.title,
      position: await this.nextPagePosition(
        createPageDto.spaceId,
        parentPageId,
      ),
      icon: createPageDto.icon,
      parentPageId: parentPageId,
      spaceId: createPageDto.spaceId,
      creatorId: userId,
      workspaceId: workspaceId,
      lastUpdatedById: userId,
      isBase,
      content,
      textContent,
      ydoc,
    }, trx);

    if (trx) {
      // Add the watcher inside the caller's transaction so the async worker
      // never inserts against an uncommitted page (FK violation on bases).
      await this.watcherService.addPageWatchers(
        [userId],
        page.id,
        createPageDto.spaceId,
        workspaceId,
        trx,
      );
    } else {
      this.generalQueue
        .add(QueueJob.ADD_PAGE_WATCHERS, {
          userIds: [userId],
          pageId: page.id,
          spaceId: createPageDto.spaceId,
          workspaceId,
        })
        .catch((err) =>
          this.logger.warn(`Failed to queue add-page-watchers: ${err.message}`),
        );
    }

    return page;
  }

  async nextPagePosition(
    spaceId: string,
    parentPageId?: string,
    trx?: KyselyTransaction,
  ) {
    let pagePosition: string;

    const lastPageQuery = dbOrTx(this.db, trx)
      .selectFrom('pages')
      .select(['position'])
      .where('spaceId', '=', spaceId)
      .where('deletedAt', 'is', null)
      .orderBy('position', (ob) => ob.collate('C').desc())
      .limit(1);

    if (parentPageId) {
      // check for children of this page
      const lastPage = await lastPageQuery
        .where('parentPageId', '=', parentPageId)
        .executeTakeFirst();

      if (!lastPage) {
        pagePosition = generateJitteredKeyBetween(null, null);
      } else {
        // if there is an existing page, we should get a position below it
        pagePosition = generateJitteredKeyBetween(lastPage.position, null);
      }
    } else {
      // for root page
      const lastPage = await lastPageQuery
        .where('parentPageId', 'is', null)
        .executeTakeFirst();

      // if no existing page, make this the first
      if (!lastPage) {
        pagePosition = generateJitteredKeyBetween(null, null); // we expect "a0"
      } else {
        // if there is an existing page, we should get a position below it
        pagePosition = generateJitteredKeyBetween(lastPage.position, null);
      }
    }

    return pagePosition;
  }

  async update(
    page: Page,
    updatePageDto: UpdatePageDto,
    user: User,
  ): Promise<Page> {
    const { title, icon, content, operation, format } = updatePageDto;
    const hasContentUpdate = Boolean(content && operation && format);
    const coverFields = await this.resolveCoverFields(
      page,
      updatePageDto,
      user,
    );
    const otherFields = { icon, ...coverFields };

    const edit = await executeTx(this.db, async (trx) => {
      const current = await this.pageRepo.findById(page.id, {
        withLock: true,
        trx,
      });
      if (!current) throw new NotFoundException('Page not found');

      const titleChanged =
        title !== undefined && !isSameTitle(current.title, title);
      const otherFieldsChanged = Object.entries(otherFields).some(
        ([key, value]) =>
          value !== undefined && !isDeepStrictEqual(current[key], value),
      );
      if (title !== undefined && !titleChanged && !otherFieldsChanged) {
        if (current.title !== title) {
          // Save the literal spacing without including updatedAt in the SQL update.
          await this.pageRepo.updatePage(
            { title, updatedAt: undefined },
            page.id,
            trx,
          );
        }
        return null;
      }

      const contributors = new Set(current.contributorIds);
      contributors.add(user.id);
      await this.pageRepo.updatePage(
        {
          title,
          ...otherFields,
          lastUpdatedById: user.id,
          contributorIds: Array.from(contributors),
        },
        page.id,
        trx,
      );

      if (!titleChanged) return { invalidation: null };

      const invalidation =
        await this.pageVerificationInvalidationService.invalidateForEdit(
          {
            pageId: page.id,
            userId: user.id,
            changedAt: null,
            isMeaningfulChange: () => true,
            loadPreviousState: async () => {
              const { content } = await this.pageRepo.findById(page.id, {
                includeContent: true,
                trx,
              });
              return { ...current, content };
            },
          },
          trx,
        );
      return { invalidation };
    });

    if (edit?.invalidation) {
      await this.pageVerificationInvalidationService.notifyInvalidated(
        edit.invalidation,
        user.id,
        'title',
      );
    }

    if (edit || hasContentUpdate) {
      this.scheduleCoverCleanup(page, coverFields);
      this.generalQueue
        .add(QueueJob.ADD_PAGE_WATCHERS, {
          userIds: [user.id],
          pageId: page.id,
          spaceId: page.spaceId,
          workspaceId: page.workspaceId,
        })
        .catch((err) =>
          this.logger.warn(`Failed to queue add-page-watchers: ${err.message}`),
        );
    }

    if (hasContentUpdate) {
      await this.updatePageContent(page.id, content, operation, format, user);
    }

    return await this.pageRepo.findById(page.id, {
      includeSpace: true,
      includeContent: true,
      includeCreator: true,
      includeLastUpdatedBy: true,
      includeContributors: true,
    });
  }

  private async resolveCoverFields(
    page: Page,
    dto: UpdatePageDto,
    user: User,
  ): Promise<CoverFields> {
    const fields: CoverFields = {};

    if (dto.coverPhoto !== undefined && dto.coverPhoto !== page.coverPhoto) {
      const cover = await this.resolveCover(page, dto.coverPhoto, user);
      fields.coverPhoto = cover.coverPhoto;
      if (cover.coverPhoto !== page.coverPhoto) {
        fields.coverPosition = null;
        fields.coverAttribution = cover.coverAttribution;
      }
    }

    if (dto.coverPosition !== undefined) {
      fields.coverPosition = dto.coverPosition;
    }

    const coverPhoto =
      fields.coverPhoto !== undefined ? fields.coverPhoto : page.coverPhoto;
    if (coverPhoto === null && fields.coverPosition !== undefined) {
      fields.coverPosition = null;
    }

    return fields;
  }

  private async resolveCover(
    page: Page,
    coverPhoto: string | null,
    user: User,
  ): Promise<Pick<CoverFields, 'coverPhoto' | 'coverAttribution'>> {
    if (coverPhoto === null || isPresetCover(coverPhoto)) {
      return { coverPhoto, coverAttribution: null };
    }

    const unsplashPhotoId = getUnsplashPhotoId(coverPhoto);
    if (unsplashPhotoId) {
      return this.unsplashService.getCover(unsplashPhotoId, user.id);
    }

    return {
      coverPhoto: await this.validateCoverFile(page, coverPhoto),
      coverAttribution: null,
    };
  }

  private async validateCoverFile(
    page: Page,
    coverPhoto: string,
  ): Promise<string> {
    const attachmentId = getCoverAttachmentId(coverPhoto);
    const attachment = attachmentId
      ? await this.attachmentRepo.findById(attachmentId)
      : null;

    // only cover uploads, so the cleanup job never touches a file the page shows elsewhere
    if (
      !attachment ||
      attachment.type !== AttachmentType.PageCover ||
      attachment.workspaceId !== page.workspaceId ||
      attachment.pageId !== page.id ||
      !COVER_IMAGE_MIME_TYPES.includes(attachment.mimeType)
    ) {
      throw new BadRequestException('Invalid cover photo');
    }

    return buildCoverFileUrl(attachment);
  }

  private scheduleCoverCleanup(page: Page, fields: CoverFields): void {
    if (fields.coverPhoto === undefined) return;

    const attachmentId = getCoverAttachmentId(page.coverPhoto);
    if (!attachmentId || attachmentId === getCoverAttachmentId(fields.coverPhoto)) {
      return;
    }

    queueCoverCleanup(this.attachmentQueue, {
      pageId: page.id,
      attachmentId,
    }).catch((err) =>
      this.logger.warn(`Failed to queue cover cleanup: ${err.message}`),
    );
  }

  async updatePageContent(
    pageId: string,
    content: string | object,
    operation: ContentOperation,
    format: ContentFormat,
    user: User,
  ): Promise<void> {
    const prosemirrorJson = await this.parseProsemirrorContent(content, format);

    const documentName = `page.${pageId}`;
    await this.collaborationGateway.handleYjsEvent(
      'updatePageContent',
      documentName,
      { operation, prosemirrorJson, user },
    );
  }

  async getSidebarPages(
    spaceId: string,
    pagination: PaginationOptions,
    pageId?: string,
    userId?: string,
    spaceCanEdit?: boolean,
  ): Promise<CursorPaginationResult<Partial<Page> & { hasChildren: boolean }>> {
    let query = this.db
      .selectFrom('pages')
      .select([
        'id',
        'slugId',
        'title',
        'icon',
        'coverPhoto',
        'position',
        'parentPageId',
        'spaceId',
        'creatorId',
        'isBase',
        'updatedAt',
        'deletedAt',
      ])
      .select((eb) => this.pageRepo.withHasChildren(eb))
      .where('deletedAt', 'is', null)
      .where('spaceId', '=', spaceId);

    if (pageId) {
      query = query.where('parentPageId', '=', pageId);
    } else {
      query = query.where('parentPageId', 'is', null);
    }

    const result = await executeWithCursorPagination(query, {
      perPage: pagination.limit,
      cursor: pagination.cursor,
      beforeCursor: pagination.beforeCursor,
      fields: [
        {
          expression: 'position',
          direction: 'asc',
          orderModifier: (ob) => ob.collate('C').asc(),
          cursorExpression: sql`position collate "C"`,
        },
        { expression: 'id', direction: 'asc' },
      ],
      parseCursor: (cursor) => ({
        position: cursor.position,
        id: cursor.id,
      }),
    });

    if (userId && result.items.length > 0) {
      const hasRestrictions =
        await this.pagePermissionRepo.hasRestrictedPagesInSpace(spaceId);

      if (!hasRestrictions) {
        result.items = result.items.map((p: any) => ({
          ...p,
          canEdit: spaceCanEdit ?? true,
        }));
      } else {
        const pageIds = result.items.map((p: any) => p.id);

        const accessiblePages =
          await this.pagePermissionRepo.filterAccessiblePageIdsWithPermissions(
            pageIds,
            userId,
          );

        const permissionMap = new Map(
          accessiblePages.map((p) => [p.id, p.canEdit]),
        );

        result.items = result.items
          .filter((p: any) => permissionMap.has(p.id))
          .map((p: any) => ({
            ...p,
            canEdit: permissionMap.get(p.id) && (spaceCanEdit ?? true),
          }));

        const pagesWithChildren = result.items.filter(
          (p: any) => p.hasChildren,
        );
        if (pagesWithChildren.length > 0) {
          const parentIds = pagesWithChildren.map((p: any) => p.id);
          const parentsWithAccessibleChildren =
            await this.pagePermissionRepo.getParentIdsWithAccessibleChildren(
              parentIds,
              userId,
            );
          const hasAccessibleChildrenSet = new Set(
            parentsWithAccessibleChildren,
          );

          result.items = result.items.map((p: any) => ({
            ...p,
            hasChildren: p.hasChildren && hasAccessibleChildrenSet.has(p.id),
          }));
        }
      }
    }

    return result;
  }

  async movePageToSpace(rootPage: Page, spaceId: string, userId: string) {
    const sharingAllowed = await this.shareService.isSharingAllowed(
      rootPage.workspaceId,
      spaceId,
    );

    const result = await executeTx(this.db, async (trx) => {
      await this.pageRepo.lockPageHierarchySpaces(
        [rootPage.spaceId, spaceId],
        trx,
      );

      const currentRootPage = await this.pageRepo.findById(rootPage.id, {
        trx,
      });
      if (!currentRootPage || currentRootPage.deletedAt) {
        throw new NotFoundException('Page to move not found');
      }
      if (currentRootPage.spaceId !== rootPage.spaceId) {
        throw new ConflictException('Page location changed; retry the move');
      }

      const allPages = await this.pageRepo.getPageAndDescendants(
        currentRootPage.id,
        { includeContent: false, trx },
      );
      const accessiblePages = await this.filterAccessibleTreePages(
        allPages,
        currentRootPage.id,
        userId,
        currentRootPage.spaceId,
      );
      const accessibleIds = new Set(accessiblePages.map((p) => p.id));
      const pagesToOrphan = allPages.filter(
        (p) =>
          !accessibleIds.has(p.id) &&
          p.parentPageId &&
          accessibleIds.has(p.parentPageId),
      );

      // Orphan inaccessible child pages (make them root pages in original space)
      for (const page of pagesToOrphan) {
        const orphanPosition = await this.nextPagePosition(
          currentRootPage.spaceId,
          null,
          trx,
        );
        await this.pageRepo.updatePage(
          { parentPageId: null, position: orphanPosition },
          page.id,
          trx,
        );
      }

      const pageIdsToMove = accessiblePages.map((p) => p.id);

      // trashed pages below the moved ones go along, keeping their parents and restrictions
      const trashedPages =
        pageIdsToMove.length > 0
          ? await trx
              .withRecursive('trashed_pages', (db) =>
                db
                  .selectFrom('pages')
                  .select(['id'])
                  .where('spaceId', '=', currentRootPage.spaceId)
                  .where('deletedAt', 'is not', null)
                  .where('parentPageId', 'in', pageIdsToMove)
                  .unionAll((exp) =>
                    exp
                      .selectFrom('pages as p')
                      .select(['p.id'])
                      .innerJoin(
                        'trashed_pages as tp',
                        'tp.id',
                        'p.parentPageId',
                      )
                      .where('p.spaceId', '=', currentRootPage.spaceId)
                      .where('p.deletedAt', 'is not', null),
                  ),
              )
              .selectFrom('trashed_pages')
              .select('id')
              .execute()
          : [];
      const trashedPageIds = trashedPages.map((p) => p.id);
      const allPageIdsToMove = [...pageIdsToMove, ...trashedPageIds];

      // Update root page
      const nextPosition = await this.nextPagePosition(spaceId, null, trx);
      await this.pageRepo.updatePage(
        { spaceId, parentPageId: null, position: nextPosition },
        currentRootPage.id,
        trx,
      );

      const childPageIds = pageIdsToMove.filter(
        (id) => id !== currentRootPage.id,
      );
      let deletedShares: Pick<Share, 'id' | 'pageId' | 'spaceId'>[] = [];

      if (pageIdsToMove.length > 1) {
        // Update sub pages (all accessible pages except root)
        await this.pageRepo.updatePages({ spaceId }, childPageIds, trx);
      }

      if (trashedPageIds.length > 0) {
        await trx
          .updateTable('pages')
          .set({ spaceId })
          .where('id', 'in', trashedPageIds)
          .execute();
      }

      if (pageIdsToMove.length > 0) {
        await trx
          .updateTable('pageAccess')
          .set({ spaceId: spaceId })
          .where('pageId', 'in', allPageIdsToMove)
          .execute();

        // shares only follow pages into spaces that allow public sharing
        if (sharingAllowed) {
          await trx
            .updateTable('shares')
            .set({ spaceId: spaceId })
            .where('pageId', 'in', allPageIdsToMove)
            .execute();
        } else {
          deletedShares = await trx
            .deleteFrom('shares')
            .where('pageId', 'in', allPageIdsToMove)
            .returning(['id', 'pageId', 'spaceId'])
            .execute();
        }

        await trx
          .updateTable('spaces')
          .set({ homePageId: null })
          .where('homePageId', 'in', allPageIdsToMove)
          .execute();

        // Update comments
        await trx
          .updateTable('comments')
          .set({ spaceId: spaceId })
          .where('pageId', 'in', allPageIdsToMove)
          .execute();

        // Update page verifications
        await trx
          .updateTable('pageVerifications')
          .set({ spaceId: spaceId })
          .where('pageId', 'in', allPageIdsToMove)
          .execute();

        // Update page history
        await trx
          .updateTable('pageHistory')
          .set({ spaceId: spaceId })
          .where('pageId', 'in', allPageIdsToMove)
          .execute();

        // Update notifications — access follows the page after a move
        await trx
          .updateTable('notifications')
          .set({ spaceId: spaceId })
          .where('pageId', 'in', allPageIdsToMove)
          .execute();

        // Update attachments
        await this.attachmentRepo.updateAttachmentsByPageId(
          { spaceId },
          allPageIdsToMove,
          trx,
        );

        // Update watchers and remove those without access to new space
        await this.watcherService.movePageWatchersToSpace(
          allPageIdsToMove,
          spaceId,
          {
            trx,
          },
        );

        // trashed pages are embedded again when restored
        await this.aiQueue.add(
          QueueJob.PAGE_MOVED_TO_SPACE,
          {
            pageIds: pageIdsToMove,
            spaceId,
            workspaceId: currentRootPage.workspaceId,
          },
          {
            attempts: 2,
            backoff: {
              type: 'fixed',
              delay: 2 * 60 * 1000,
            },
          },
        );
      }

      return { childPageIds, deletedShares };
    });

    await this.collabAccessService.recheckAccess({ pageIds: [rootPage.id] });
    await this.wsService.invalidateSpaceRestrictionCache(spaceId);
    await this.wsService.removeBaseSubscribersWithoutAccess([rootPage.id]);

    return result;
  }

  async duplicatePage(
    rootPage: Page,
    targetSpaceId: string | undefined,
    authUser: User,
  ) {
    const spaceId = targetSpaceId || rootPage.spaceId;
    const isDuplicateInSameSpace =
      !targetSpaceId || targetSpaceId === rootPage.spaceId;

    let nextPosition: string;

    if (isDuplicateInSameSpace) {
      // For duplicate in same space, position right after the original page
      nextPosition = generateJitteredKeyBetween(rootPage.position, null);
    } else {
      // For copy to different space, position at the end
      nextPosition = await this.nextPagePosition(spaceId);
    }

    const allPages = await this.pageRepo.getPageAndDescendants(rootPage.id, {
      includeContent: true,
    });

    // Filter to only accessible pages while maintaining tree integrity
    const pages = await this.filterAccessibleTreePages(
      allPages,
      rootPage.id,
      authUser.id,
      rootPage.spaceId,
    );

    const pageMap = new Map<string, CopyPageMapEntry>();
    pages.forEach((page) => {
      pageMap.set(page.id, {
        newPageId: uuid7(),
        newSlugId: generateSlugId(),
        oldSlugId: page.slugId,
      });
    });

    const slugIdMap = new Map<string, CopyPageMapEntry>();
    for (const [, entry] of pageMap) {
      slugIdMap.set(entry.oldSlugId, entry);
    }

    const restrictedAncestor = isDuplicateInSameSpace
      ? undefined
      : await this.pagePermissionRepo.findRestrictedAncestor(rootPage.id);
    const restrictionCopies = pages.map((page) => ({
      // a copy in another space leaves the original's parents behind, so its root takes their nearest restriction
      sourcePageId:
        page.id === rootPage.id && restrictedAncestor
          ? restrictedAncestor.pageId
          : page.id,
      pageId: pageMap.get(page.id).newPageId,
    }));

    const copiedAttachmentIds = new Map<string, string>();

    const pageAttachmentIds = new Map<string, string[]>();
    const referencedAttachmentIds = new Set<string>();
    for (const page of pages) {
      const ids = getAttachmentIds(getProsemirrorContent(page.content));
      pageAttachmentIds.set(page.id, ids);
      ids.forEach((id) => referencedAttachmentIds.add(id));
      const coverId = getCoverAttachmentId(page.coverPhoto);
      if (coverId) referencedAttachmentIds.add(coverId);
    }
    const referencedAttachments =
      referencedAttachmentIds.size > 0
        ? await this.db
            .selectFrom('attachments')
            .selectAll()
            .where('id', 'in', Array.from(referencedAttachmentIds))
            .where('workspaceId', '=', rootPage.workspaceId)
            .execute()
        : [];
    const ownedAttachmentIds = new Set(
      referencedAttachments
        .filter((attachment) => pageMap.has(attachment.pageId))
        .map((attachment) => attachment.id),
    );

    const insertablePages: InsertablePage[] = await Promise.all(
      pages.map(async (page) => {
        const pageContent = getProsemirrorContent(page.content);
        const pageFromMap = pageMap.get(page.id);

        const doc = jsonToNode(pageContent);
        const prosemirrorDoc = removeMarkTypeFromDoc(doc, 'comment');

        const attachmentIds = pageAttachmentIds.get(page.id);

        attachmentIds
          .filter((attachmentId: string) =>
            ownedAttachmentIds.has(attachmentId),
          )
          .forEach((attachmentId: string) => {
            // pages that show the same file share one copy of it
            copiedAttachmentIds.set(
              attachmentId,
              copiedAttachmentIds.get(attachmentId) ?? uuid7(),
            );
          });

        const coverPhoto = remapCopiedCover(page.coverPhoto, copiedAttachmentIds, uuid7);

        // Update internal page links in mention nodes
        prosemirrorDoc.descendants((node: PMNode) => {
          if (
            node.type.name === 'mention' &&
            node.attrs.entityType === 'page'
          ) {
            const referencedPageId = node.attrs.entityId;

            // Check if the referenced page is within the pages being copied
            if (referencedPageId && pageMap.has(referencedPageId)) {
              const mappedPage = pageMap.get(referencedPageId);
              //@ts-ignore
              node.attrs.entityId = mappedPage.newPageId;
              //@ts-ignore
              node.attrs.slugId = mappedPage.newSlugId;
            }
          }

          // Remap transclusion-reference source pages to their copies when
          // the source page is also being duplicated in the same operation.
          if (node.type.name === 'transclusionReference') {
            const sourcePageId = node.attrs.sourcePageId;
            if (sourcePageId && pageMap.has(sourcePageId)) {
              const mappedPage = pageMap.get(sourcePageId);
              //@ts-ignore
              node.attrs.sourcePageId = mappedPage.newPageId;
            }
          }

          // Update internal page links in link marks
          for (const mark of node.marks) {
            if (
              mark.type.name === 'link' &&
              mark.attrs.internal &&
              mark.attrs.href
            ) {
              const match = mark.attrs.href.match(INTERNAL_LINK_REGEX);
              if (match) {
                const slugId = extractPageSlugId(match[5]);
                if (slugId && slugIdMap.has(slugId)) {
                  const mappedPage = slugIdMap.get(slugId);
                  //@ts-ignore
                  mark.attrs.href = mark.attrs.href.replace(
                    slugId,
                    mappedPage.newSlugId,
                  );
                }
              }
            }
          }
        });

        const prosemirrorJson = replaceAttachmentIds(
          prosemirrorDoc.toJSON(),
          copiedAttachmentIds,
        );

        // Add "Copy of " prefix to the root page title only for duplicates in same space
        let title = page.title;
        if (isDuplicateInSameSpace && page.id === rootPage.id) {
          const originalTitle = getPageTitle(page.title);
          title = `Copy of ${originalTitle}`;
        }

        return {
          id: pageFromMap.newPageId,
          slugId: pageFromMap.newSlugId,
          title: title,
          icon: page.icon,
          coverPhoto,
          coverPosition: page.coverPosition,
          coverAttribution: page.coverAttribution,
          content: prosemirrorJson,
          textContent: jsonToText(prosemirrorJson),
          ydoc: createYdocFromJson(prosemirrorJson),
          position: page.id === rootPage.id ? nextPosition : page.position,
          spaceId: spaceId,
          workspaceId: page.workspaceId,
          creatorId: authUser.id,
          lastUpdatedById: authUser.id,
          parentPageId:
            page.id === rootPage.id
              ? isDuplicateInSameSpace
                ? rootPage.parentPageId
                : null
              : page.parentPageId
                ? pageMap.get(page.parentPageId)?.newPageId
                : null,
        };
      }),
    );

    const copiedRestrictions = await executeTx(this.db, async (trx) => {
      await trx.insertInto('pages').values(insertablePages).execute();
      await this.labelRepo.copyLabelsToPages(
        new Map([...pageMap].map(([oldId, entry]) => [oldId, entry.newPageId])),
        trx,
      );
      return this.pagePermissionRepo.copyRestrictions(
        restrictionCopies,
        {
          spaceId,
          workspaceId: rootPage.workspaceId,
          writerId: authUser.id,
        },
        trx,
      );
    });

    if (copiedRestrictions > 0) {
      await this.wsService.invalidateSpaceRestrictionCache(spaceId);
    }

    // Extract transclusions from every duplicated page and persist them in
    // one statement. Duplication bypasses Yjs onStoreDocument; brand-new
    // pages never have prior rows so we can skip the diff and just bulk-insert.
    try {
      await this.transclusionService.insertTransclusionsForPages(
        insertablePages.map((p) => ({
          id: p.id,
          workspaceId: p.workspaceId,
          content: p.content,
        })),
      );
    } catch (err) {
      this.logger.error(
        'Failed to insert transclusions for duplicated pages',
        err,
      );
    }

    try {
      await this.transclusionService.insertReferencesForPages(
        insertablePages.map((p) => ({
          id: p.id,
          workspaceId: p.workspaceId,
          content: p.content,
        })),
      );
    } catch (err) {
      this.logger.error(
        'Failed to insert transclusion references for duplicated pages',
        err,
      );
    }

    const insertedPageIds = insertablePages.map((page) => page.id);
    this.eventEmitter.emit(EventName.PAGE_CREATED, {
      pageIds: insertedPageIds,
      workspaceId: authUser.workspaceId,
    });

    //TODO: best to handle this in a queue
    if (copiedAttachmentIds.size > 0) {
      for (const attachment of referencedAttachments) {
        try {
          // only files owned by a copied page are copied, and they go to that page's copy
          const owner = pageMap.get(attachment.pageId);
          if (!owner) {
            continue;
          }

          const newAttachmentId = copiedAttachmentIds.get(attachment.id);

          const newPageId = owner.newPageId;

          const newPathFile = attachment.filePath.replace(
            attachment.id,
            newAttachmentId,
          );

          try {
            await this.storageService.copy(attachment.filePath, newPathFile);

            await this.db
              .insertInto('attachments')
              .values({
                id: newAttachmentId,
                type: attachment.type,
                filePath: newPathFile,
                fileName: attachment.fileName,
                fileSize: attachment.fileSize,
                mimeType: attachment.mimeType,
                fileExt: attachment.fileExt,
                creatorId: authUser.id,
                workspaceId: attachment.workspaceId,
                pageId: newPageId,
                spaceId: spaceId,
              })
              .execute();
          } catch (err) {
            this.logger.error(
              `Duplicate page: failed to copy attachment ${attachment.id}`,
              err,
            );
            // Continue with other attachments even if one fails
          }
        } catch (err) {
          this.logger.error(err);
        }
      }
    }

    const newPageId = pageMap.get(rootPage.id).newPageId;
    const duplicatedPage = await this.pageRepo.findById(newPageId, {
      includeSpace: true,
    });

    const hasChildren = pages.length > 1;
    const childPageIds = insertedPageIds.filter((id) => id !== newPageId);

    return {
      ...duplicatedPage,
      hasChildren,
      childPageIds,
    };
  }

  async movePage(dto: MovePageDto, movedPage: Page) {
    // validate position value by attempting to generate a key
    try {
      generateJitteredKeyBetween(dto.position, null);
    } catch (err) {
      throw new BadRequestException('Invalid move position');
    }

    if (dto.parentPageId && dto.parentPageId === dto.pageId) {
      throw new BadRequestException('A page cannot be its own parent');
    }

    const mayLoseAccess = await executeTx(this.db, async (trx) => {
      await this.pageRepo.lockPageHierarchySpaces(
        [movedPage.spaceId],
        trx,
      );

      const currentPage = await this.pageRepo.findById(movedPage.id, { trx });
      if (!currentPage || currentPage.deletedAt) {
        throw new NotFoundException('Moved page not found');
      }
      if (currentPage.spaceId !== movedPage.spaceId) {
        throw new ConflictException('Page location changed; retry the move');
      }

      let parentPageId = null;
      if (currentPage.parentPageId === dto.parentPageId) {
        parentPageId = undefined;
      } else {
        if (dto.parentPageId) {
          const parentPage = await this.pageRepo.findById(dto.parentPageId, {
            trx,
          });
          if (
            !parentPage ||
            parentPage.deletedAt ||
            parentPage.spaceId !== currentPage.spaceId
          ) {
            throw new NotFoundException('Parent page not found');
          }
          if (
            await this.pageRepo.isPageDescendant(
              movedPage.id,
              parentPage.id,
              trx,
            )
          ) {
            throw new BadRequestException(
              'A page cannot be moved under its descendant',
            );
          }
          parentPageId = parentPage.id;
        }
      }

      await this.pageRepo.updatePage(
        {
          position: dto.position,
          parentPageId: parentPageId,
        },
        movedPage.id,
        trx,
      );

      // a new parent only changes access in a space with restricted pages
      return (
        parentPageId !== undefined &&
        (await this.pagePermissionRepo.hasRestrictedPagesInSpace(
          currentPage.spaceId,
          { trx },
        ))
      );
    });

    if (mayLoseAccess) {
      await this.collabAccessService.recheckAccess({ pageIds: [movedPage.id] });
      await this.wsService.removeBaseSubscribersWithoutAccess([movedPage.id]);
    }
  }

  async getPageBreadCrumbs(childPageId: string) {
    const ancestors = await this.db
      .withRecursive('page_ancestors', (db) =>
        db
          .selectFrom('pages')
          .select([
            'id',
            'slugId',
            'title',
            'icon',
            'isBase',
            'position',
            'parentPageId',
            'spaceId',
            'deletedAt',
          ])
          .where('id', '=', childPageId)
          .where('deletedAt', 'is', null)
          .unionAll((exp) =>
            exp
              .selectFrom('pages as p')
              .select([
                'p.id',
                'p.slugId',
                'p.title',
                'p.icon',
                'p.isBase',
                'p.position',
                'p.parentPageId',
                'p.spaceId',
                'p.deletedAt',
              ])
              .innerJoin('page_ancestors as pa', 'pa.parentPageId', 'p.id')
              .where('p.deletedAt', 'is', null)
              .whereRef('p.spaceId', '=', 'pa.spaceId'),
          ),
      )
      .selectFrom('page_ancestors')
      .selectAll('page_ancestors')
      .select((eb) =>
        eb
          .exists(
            eb
              .selectFrom('pages as child')
              .select(sql`1`.as('one'))
              .whereRef('child.parentPageId', '=', 'page_ancestors.id')
              .where('child.deletedAt', 'is', null),
          )
          .as('hasChildren'),
      )
      .execute();

    return ancestors.reverse();
  }

  async getRecentSpacePages(
    spaceId: string,
    userId: string,
    pagination: PaginationOptions,
  ): Promise<CursorPaginationResult<Page>> {
    const result = await this.pageRepo.getRecentPagesInSpace(
      spaceId,
      pagination,
    );

    if (result.items.length > 0) {
      const pageIds = result.items.map((p) => p.id);
      const accessibleIds =
        await this.pagePermissionRepo.filterAccessiblePageIds({
          pageIds,
          userId,
          spaceId,
        });
      const accessibleSet = new Set(accessibleIds);
      result.items = result.items.filter((p) => accessibleSet.has(p.id));
    }

    return result;
  }

  async getRecentPages(
    userId: string,
    pagination: PaginationOptions,
  ): Promise<CursorPaginationResult<Page>> {
    const result = await this.pageRepo.getRecentPages(userId, pagination);

    if (result.items.length > 0) {
      const pageIds = result.items.map((p) => p.id);
      const accessibleIds =
        await this.pagePermissionRepo.filterAccessiblePageIds({
          pageIds,
          userId,
        });
      const accessibleSet = new Set(accessibleIds);
      result.items = result.items.filter((p) => accessibleSet.has(p.id));
    }

    return result;
  }

  async getCreatedByPages(
    creatorId: string,
    requestingUserId: string,
    pagination: PaginationOptions,
    spaceId?: string,
  ): Promise<CursorPaginationResult<Page>> {
    const result = await this.pageRepo.getCreatedByPages(
      creatorId,
      requestingUserId,
      pagination,
      spaceId,
    );

    if (result.items.length > 0) {
      const pageIds = result.items.map((p) => p.id);
      const accessibleIds =
        await this.pagePermissionRepo.filterAccessiblePageIds({
          pageIds,
          userId: requestingUserId,
        });
      const accessibleSet = new Set(accessibleIds);
      result.items = result.items.filter((p) => accessibleSet.has(p.id));
    }

    return result;
  }

  async getDeletedSpacePages(
    spaceId: string,
    userId: string,
    pagination: PaginationOptions,
  ): Promise<CursorPaginationResult<Page>> {
    const result = await this.pageRepo.getDeletedPagesInSpace(
      spaceId,
      pagination,
    );

    if (result.items.length > 0) {
      const pageIds = result.items.map((p) => p.id);
      const accessibleIds =
        await this.pagePermissionRepo.filterAccessiblePageIds({
          pageIds,
          userId,
          spaceId,
        });
      const accessibleSet = new Set(accessibleIds);
      result.items = result.items.filter((p) => accessibleSet.has(p.id));
    }

    return result;
  }

  async forceDelete(pageId: string, workspaceId: string): Promise<void> {
    // Get all descendant IDs (including the page itself) using recursive CTE
    const descendants = await this.db
      .withRecursive('page_descendants', (db) =>
        db
          .selectFrom('pages')
          .select(['id'])
          .where('id', '=', pageId)
          .unionAll((exp) =>
            exp
              .selectFrom('pages as p')
              .select(['p.id'])
              .innerJoin('page_descendants as pd', 'pd.id', 'p.parentPageId'),
          ),
      )
      .selectFrom('page_descendants')
      .selectAll()
      .execute();

    const pageIds = descendants.map((d) => d.id);

    // Queue attachment deletion for all pages with unique job IDs to prevent duplicates
    for (const id of pageIds) {
      await this.attachmentQueue.add(
        QueueJob.DELETE_PAGE_ATTACHMENTS,
        {
          pageId: id,
        },
        {
          jobId: `delete-page-attachments-${id}`,
          attempts: 3,
          backoff: {
            type: 'exponential',
            delay: 5000,
          },
        },
      );
    }

    if (pageIds.length > 0) {
      await this.db.deleteFrom('pages').where('id', 'in', pageIds).execute();
      this.eventEmitter.emit(EventName.PAGE_DELETED, {
        pageIds: pageIds,
        workspaceId,
      });
    }
  }

  async removePage(
    pageId: string,
    userId: string,
    workspaceId: string,
  ): Promise<void> {
    await this.pageRepo.removePage(pageId, userId, workspaceId);
  }

  async parseProsemirrorContent(
    content: string | object,
    format: ContentFormat,
    context: 'page' | 'template' = 'page',
  ): Promise<any> {
    const extensions = context === 'template' ? templateExtensions : tiptapExtensions;
    let prosemirrorJson: any;

    switch (format) {
      case 'markdown': {
        const html = await markdownToHtml(content as string);
        prosemirrorJson = htmlToJson(html as string, extensions);
        break;
      }
      case 'html': {
        prosemirrorJson = htmlToJson(content as string, extensions);
        break;
      }
      case 'json':
      default: {
        prosemirrorJson = content;
        break;
      }
    }

    try {
      return parseDocumentJson(prosemirrorJson, extensions).content;
    } catch (err) {
      throw new BadRequestException('Invalid content format');
    }
  }

  /**
   * Filters a list of pages to only those accessible to the user while maintaining tree integrity.
   * A page is included only if:
   * 1. The user has access to it
   * 2. Its parent is also included (or it's the root page)
   * This ensures that if a middle page is inaccessible, its entire subtree is excluded.
   */
  private async filterAccessibleTreePages<
    T extends { id: string; parentPageId: string | null },
  >(
    pages: T[],
    rootPageId: string,
    userId: string,
    spaceId?: string,
  ): Promise<T[]> {
    if (pages.length === 0) return [];

    const pageIds = pages.map((p) => p.id);
    const accessibleIds = await this.pagePermissionRepo.filterAccessiblePageIds(
      {
        pageIds,
        userId,
        spaceId,
      },
    );
    const accessibleSet = new Set(accessibleIds);

    // Prune: include a page only if it's accessible AND its parent chain to root is included
    const includedIds = new Set<string>();

    // Process pages in a way that ensures parents are processed before children
    // We do this by iterating until no more pages can be added
    let changed = true;
    while (changed) {
      changed = false;
      for (const page of pages) {
        if (includedIds.has(page.id)) continue;
        if (!accessibleSet.has(page.id)) continue;

        // Root page: include if accessible
        if (page.id === rootPageId) {
          includedIds.add(page.id);
          changed = true;
          continue;
        }

        // Non-root: include if parent is already included
        if (page.parentPageId && includedIds.has(page.parentPageId)) {
          includedIds.add(page.id);
          changed = true;
        }
      }
    }

    return pages.filter((p) => includedIds.has(p.id));
  }
}
