import { Injectable } from '@nestjs/common';
import { InjectKysely } from 'nestjs-kysely';
import { KyselyDB, KyselyTransaction } from '../../types/kysely.types';
import { dbOrTx, executeTx } from '../../utils';
import {
  InsertablePage,
  Page,
  UpdatablePage,
} from '@docmost/db/types/entity.types';
import { PaginationOptions } from '@docmost/db/pagination/pagination-options';
import { executeWithCursorPagination } from '@docmost/db/pagination/cursor-pagination';
import { validate as isValidUUID } from 'uuid';
import { ExpressionBuilder, sql } from 'kysely';
import { DB } from '@docmost/db/types/db';
import { jsonArrayFrom, jsonObjectFrom } from 'kysely/helpers/postgres';
import { SpaceMemberRepo } from '@docmost/db/repos/space/space-member.repo';
import { EventEmitter2 } from '@nestjs/event-emitter';
import { EventName } from '../../../common/events/event.contants';
import type { RecentlyUpdatedDto } from '../../../core/page/dto/recently-updated.dto';
import { recentlyUpdatedFilter } from './recently-updated-filter';
import { PagePermissionRepo } from './page-permission.repo';
import { UserRecentPageActivity } from '../../../core/user/user.types';

@Injectable()
export class PageRepo {
  constructor(
    @InjectKysely() private readonly db: KyselyDB,
    private spaceMemberRepo: SpaceMemberRepo,
    private eventEmitter: EventEmitter2,
    private readonly pagePermissionRepo: PagePermissionRepo,
  ) {}

  private baseFields: Array<keyof Page> = [
    'id',
    'slugId',
    'title',
    'icon',
    'coverPhoto',
    'coverPosition',
    'coverAttribution',
    'position',
    'parentPageId',
    'creatorId',
    'lastUpdatedById',
    'spaceId',
    'workspaceId',
    'isLocked',
    'isBase',
    'createdAt',
    'updatedAt',
    'deletedAt',
    'contributorIds',
  ];

  async findById(
    pageId: string,
    opts?: {
      includeContent?: boolean;
      includeTextContent?: boolean;
      includeYdoc?: boolean;
      includeSpace?: boolean;
      includeCreator?: boolean;
      includeLastUpdatedBy?: boolean;
      includeContributors?: boolean;
      includeDeletedBy?: boolean;
      includeHasChildren?: boolean;
      withLock?: boolean;
      trx?: KyselyTransaction;
    },
  ): Promise<Page> {
    const db = dbOrTx(this.db, opts?.trx);

    let query = db
      .selectFrom('pages')
      .select(this.baseFields)
      .$if(opts?.includeContent, (qb) => qb.select('content'))
      .$if(opts?.includeYdoc, (qb) => qb.select('ydoc'))
      .$if(opts?.includeTextContent, (qb) => qb.select('textContent'))
      .$if(opts?.includeHasChildren, (qb) =>
        qb.select((eb) => this.withHasChildren(eb)),
      );

    if (opts?.includeCreator) {
      query = query.select((eb) => this.withCreator(eb));
    }

    if (opts?.includeLastUpdatedBy) {
      query = query.select((eb) => this.withLastUpdatedBy(eb));
    }

    if (opts?.includeContributors) {
      query = query.select((eb) => this.withContributors(eb));
    }

    if (opts?.includeDeletedBy) {
      query = query.select((eb) => this.withDeletedBy(eb));
    }

    if (opts?.includeSpace) {
      query = query.select((eb) => this.withSpace(eb));
    }

    if (opts?.withLock && opts?.trx) {
      query = query.forUpdate();
    }

    if (isValidUUID(pageId)) {
      query = query.where('id', '=', pageId);
    } else {
      query = query.where('slugId', '=', pageId);
    }

    return query.executeTakeFirst();
  }

  async findManyByIds(
    pageIds: string[],
    opts?: {
      trx?: KyselyTransaction;
      workspaceId?: string;
    },
  ): Promise<Page[]> {
    if (pageIds.length === 0) return [];
    const db = dbOrTx(this.db, opts?.trx);

    let query = db
      .selectFrom('pages')
      .select(this.baseFields)
      .where('id', 'in', pageIds);

    if (opts?.workspaceId) {
      query = query
        .where('workspaceId', '=', opts.workspaceId)
        .where('deletedAt', 'is', null);
    }

    return query.execute();
  }

  async updatePage(
    updatablePage: UpdatablePage,
    pageId: string,
    trx?: KyselyTransaction,
  ) {
    return this.updatePages(updatablePage, [pageId], trx);
  }

  async saveYdocState(pageId: string, ydoc: Buffer, trx?: KyselyTransaction) {
    await dbOrTx(this.db, trx)
      .updateTable('pages')
      .set({ ydoc })
      .where(isValidUUID(pageId) ? 'id' : 'slugId', '=', pageId)
      .where('ydoc', 'is distinct from', ydoc)
      .executeTakeFirst();
  }

  async updatePages(
    updatePageData: UpdatablePage,
    pageIds: string[],
    trx?: KyselyTransaction,
  ) {
    const result = await dbOrTx(this.db, trx)
      .updateTable('pages')
      // a caller's updatedAt wins, so saves that aren't edits can keep it
      .set({ updatedAt: new Date(), ...updatePageData })
      .where(
        pageIds.some((pageId) => !isValidUUID(pageId)) ? 'slugId' : 'id',
        'in',
        pageIds,
      )
      .executeTakeFirst();

    this.eventEmitter.emit(EventName.PAGE_UPDATED, {
      pageIds: pageIds,
      workspaceId: updatePageData.workspaceId,
    });

    return result;
  }

  async lockPageHierarchySpaces(
    spaceIds: string[],
    trx: KyselyTransaction,
  ): Promise<void> {
    const sortedSpaceIds = [...new Set(spaceIds)].sort();

    for (const spaceId of sortedSpaceIds) {
      await sql`
        SELECT pg_advisory_xact_lock(
          hashtext('page-hierarchy'),
          hashtext(${spaceId})
        )
      `.execute(trx);
    }
  }

  async insertPage(
    insertablePage: InsertablePage,
    trx?: KyselyTransaction,
  ): Promise<Page> {
    const db = dbOrTx(this.db, trx);
    const result = await db
      .insertInto('pages')
      .values(insertablePage)
      .returning(this.baseFields)
      .executeTakeFirst();

    this.eventEmitter.emit(EventName.PAGE_CREATED, {
      pageIds: [result.id],
      workspaceId: result.workspaceId,
    });

    return result;
  }

  async deletePage(pageId: string): Promise<void> {
    let query = this.db.deleteFrom('pages');

    if (isValidUUID(pageId)) {
      query = query.where('id', '=', pageId);
    } else {
      query = query.where('slugId', '=', pageId);
    }

    await query.execute();
  }

  async removePage(
    pageId: string,
    deletedById: string,
    workspaceId: string,
  ): Promise<void> {
    const currentDate = new Date();

    const descendants = await this.db
      .withRecursive('page_descendants', (db) =>
        db
          .selectFrom('pages')
          .select(['id'])
          .where('id', '=', pageId)
          .where('deletedAt', 'is', null)
          .unionAll((exp) =>
            exp
              .selectFrom('pages as p')
              .select(['p.id'])
              .innerJoin('page_descendants as pd', 'pd.id', 'p.parentPageId')
              .where('p.deletedAt', 'is', null),
          ),
      )
      .selectFrom('page_descendants')
      .selectAll()
      .execute();

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

    if (pageIds.length > 0) {
      await executeTx(this.db, async (trx) => {
        await trx
          .updateTable('pages')
          .set({
            deletedById: deletedById,
            deletedAt: currentDate,
          })
          .where('id', 'in', pageIds)
          .where('deletedAt', 'is', null)
          .execute();

        await trx.deleteFrom('shares').where('pageId', 'in', pageIds).execute();
        await trx
          .deleteFrom('baseViewShares')
          .where('pageId', 'in', pageIds)
          .execute();

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

      this.eventEmitter.emit(EventName.PAGE_SOFT_DELETED, {
        pageIds: pageIds,
        workspaceId,
      });
    }
  }

  async restorePage(pageId: string, workspaceId: string): Promise<void> {
    // First, check if the page being restored has a deleted parent
    const pageToRestore = await this.db
      .selectFrom('pages')
      .select(['id', 'parentPageId', 'spaceId'])
      .where('id', '=', pageId)
      .executeTakeFirst();

    if (!pageToRestore) {
      return;
    }

    // Check if the parent is also deleted
    let shouldDetachFromParent = false;
    if (pageToRestore.parentPageId) {
      const parent = await this.db
        .selectFrom('pages')
        .select(['id', 'deletedAt', 'spaceId'])
        .where('id', '=', pageToRestore.parentPageId)
        .executeTakeFirst();

      // If parent is deleted or in another space, we should detach this page from it
      shouldDetachFromParent =
        !parent ||
        parent.deletedAt !== null ||
        parent.spaceId !== pageToRestore.spaceId;
    }

    // Find all descendants to restore
    const pages = await this.db
      .withRecursive('page_descendants', (db) =>
        db
          .selectFrom('pages')
          .select(['id', 'spaceId'])
          .where('id', '=', pageId)
          .unionAll((exp) =>
            exp
              .selectFrom('pages as p')
              .select(['p.id', 'p.spaceId'])
              .innerJoin('page_descendants as pd', 'pd.id', 'p.parentPageId')
              .whereRef('p.spaceId', '=', 'pd.spaceId'),
          ),
      )
      .selectFrom('page_descendants')
      .selectAll()
      .execute();

    const pageIds = pages.map((p) => p.id);

    // Restore all pages, but only detach the root page if its parent is deleted
    await this.db
      .updateTable('pages')
      .set({ deletedById: null, deletedAt: null })
      .where('id', 'in', pageIds)
      .execute();

    // If we need to detach the restored page from its deleted parent
    if (shouldDetachFromParent) {
      await executeTx(this.db, async (trx) => {
        await this.pagePermissionRepo.copyInheritedRestriction([pageId], trx);
        await trx
          .updateTable('pages')
          .set({ parentPageId: null })
          .where('id', '=', pageId)
          .execute();
      });
    }
    this.eventEmitter.emit(EventName.PAGE_RESTORED, {
      pageIds: pageIds,
      workspaceId: workspaceId,
    });
  }

  async getRecentPagesInSpace(spaceId: string, pagination: PaginationOptions) {
    const query = this.db
      .selectFrom('pages')
      .select(this.baseFields)
      .select((eb) => [this.withSpace(eb), this.withLastUpdatedBy(eb)])
      .where('spaceId', '=', spaceId)
      .where('deletedAt', 'is', null);

    return executeWithCursorPagination(query, {
      perPage: pagination.limit,
      cursor: pagination.cursor,
      beforeCursor: pagination.beforeCursor,
      fields: [
        { expression: 'updatedAt', direction: 'desc' },
        { expression: 'id', direction: 'desc' },
      ],
      parseCursor: (cursor) => ({
        updatedAt: new Date(cursor.updatedAt),
        id: cursor.id,
      }),
    });
  }

  async getRecentlyUpdatedPages(userId: string, workspaceId: string, options: RecentlyUpdatedDto) {
    const hasNamedSpaces = options.spaceIds.length > 0;
    if ((options.scope === 'spaces') !== hasNamedSpaces) return [];

    return this.db
      .selectFrom('pages')
      .innerJoin('spaces', 'spaces.id', 'pages.spaceId')
      .leftJoin('users', (join) => join
        .onRef('users.id', '=', 'pages.lastUpdatedById')
        .onRef('users.workspaceId', '=', 'pages.workspaceId'))
      .select([
        'pages.id', 'pages.slugId', 'pages.title', 'pages.icon', 'pages.isBase',
        'pages.updatedAt', 'spaces.slug as spaceSlug', 'spaces.name as spaceName',
        'users.name as lastEditorName', 'users.avatarUrl as lastEditorAvatarUrl',
      ])
      .where('pages.workspaceId', '=', workspaceId)
      .whereRef('spaces.workspaceId', '=', 'pages.workspaceId')
      .where('pages.deletedAt', 'is', null)
      .where('spaces.deletedAt', 'is', null)
      .where('pages.spaceId', 'in', this.spaceMemberRepo.getUserSpaceIdsQuery(userId, {
        excludeArchived: true,
        keepArchivedSpaceId: options.spaceId,
      }))
      .where(this.pagePermissionRepo.canUserAccessPageExpression(userId))
      .where((eb) => recentlyUpdatedFilter(eb, options))
      .orderBy('pages.updatedAt', 'desc')
      .orderBy('pages.id', 'desc')
      .limit(options.limit)
      .execute();
  }

  async getRecentPages(userId: string, pagination: PaginationOptions) {
    const query = this.db
      .selectFrom('pages')
      .select(this.baseFields)
      .select((eb) => [this.withSpace(eb), this.withLastUpdatedBy(eb)])
      .where(
        'spaceId',
        'in',
        this.spaceMemberRepo.getUserSpaceIdsQuery(userId, {
          excludeArchived: true,
        }),
      )
      .where('deletedAt', 'is', null);

    return executeWithCursorPagination(query, {
      perPage: pagination.limit,
      cursor: pagination.cursor,
      beforeCursor: pagination.beforeCursor,
      fields: [
        { expression: 'updatedAt', direction: 'desc' },
        { expression: 'id', direction: 'desc' },
      ],
      parseCursor: (cursor) => ({
        updatedAt: new Date(cursor.updatedAt),
        id: cursor.id,
      }),
    });
  }

  async getCreatedByPages(creatorId: string, requestingUserId: string, pagination: PaginationOptions, spaceId?: string) {
    let query = this.db
      .selectFrom('pages')
      .select(this.baseFields)
      .select((eb) => this.withSpace(eb))
      .where('creatorId', '=', creatorId)
      .where('deletedAt', 'is', null);

    if (spaceId) {
      query = query.where('spaceId', '=', spaceId);
    } else {
      query = query.where(
        'spaceId',
        'in',
        this.spaceMemberRepo.getUserSpaceIdsQuery(requestingUserId, {
          excludeArchived: true,
        }),
      );
    }

    return executeWithCursorPagination(query, {
      perPage: pagination.limit,
      cursor: pagination.cursor,
      beforeCursor: pagination.beforeCursor,
      fields: [
        { expression: 'updatedAt', direction: 'desc' },
        { expression: 'id', direction: 'desc' },
      ],
      parseCursor: (cursor) => ({
        updatedAt: new Date(cursor.updatedAt),
        id: cursor.id,
      }),
    });
  }

  async getRecentPagesByUser(
    userId: string,
    viewerId: string,
    workspaceId: string,
    pagination: PaginationOptions,
  ) {
    const editedByUser = sql`pages.last_updated_by_id = ${userId} and (pages.creator_id is distinct from ${userId} or pages.updated_at > pages.created_at)`;

    const baseQuery = this.db
      .selectFrom('pages')
      .select(['id', 'slugId', 'title', 'icon', 'isBase', 'spaceId'])
      .select((eb) =>
        jsonObjectFrom(
          eb
            .selectFrom('spaces')
            .select(['spaces.id', 'spaces.name', 'spaces.slug', 'spaces.logo'])
            .whereRef('spaces.id', '=', 'pages.spaceId'),
        ).as('space'),
      )
      .select(
        sql<UserRecentPageActivity>`case when ${editedByUser} then 'edited' else 'created' end`.as(
          'activity',
        ),
      )
      // truncated to the millisecond precision cursors carry
      .select(
        sql<Date>`date_trunc('milliseconds', case when ${editedByUser} then pages.updated_at else pages.created_at end)`.as(
          'activityAt',
        ),
      )
      .where('workspaceId', '=', workspaceId)
      .where('deletedAt', 'is', null)
      .where((eb) =>
        eb.or([
          eb('creatorId', '=', userId),
          eb('lastUpdatedById', '=', userId),
        ]),
      )
      .where(
        'spaceId',
        'in',
        this.spaceMemberRepo.getUserSpaceIdsQuery(viewerId, {
          excludeArchived: true,
        }),
      )
      .where(this.pagePermissionRepo.canUserAccessPageExpression(viewerId));

    const query = this.db.selectFrom(baseQuery.as('sub')).selectAll('sub');

    return executeWithCursorPagination(query, {
      perPage: pagination.limit,
      cursor: pagination.cursor,
      beforeCursor: pagination.beforeCursor,
      fields: [
        { expression: 'sub.activityAt', direction: 'desc', key: 'activityAt' },
        { expression: 'sub.id', direction: 'desc', key: 'id' },
      ],
      parseCursor: (cursor) => ({
        activityAt: new Date(cursor.activityAt),
        id: cursor.id,
      }),
    });
  }

  async getDeletedPagesInSpace(spaceId: string, pagination: PaginationOptions) {
    const query = this.db
      .selectFrom('pages')
      .select(this.baseFields)
      .select('content')
      .select((eb) => this.withSpace(eb))
      .select((eb) => this.withDeletedBy(eb))
      .where('spaceId', '=', spaceId)
      .where('deletedAt', 'is not', null)
      // Only include pages that are either root pages (no parent) or whose parent is not deleted
      // This prevents showing orphaned pages when their parent has been soft-deleted
      .where((eb) =>
        eb.or([
          eb('parentPageId', 'is', null),
          eb.not(
            eb.exists(
              eb
                .selectFrom('pages as parent')
                .select('parent.id')
                .where('parent.id', '=', eb.ref('pages.parentPageId'))
                .where('parent.deletedAt', 'is not', null),
            ),
          ),
        ]),
      );

    return executeWithCursorPagination(query, {
      perPage: pagination.limit,
      cursor: pagination.cursor,
      beforeCursor: pagination.beforeCursor,
      fields: [
        { expression: 'deletedAt', direction: 'desc' },
        { expression: 'id', direction: 'desc' },
      ],
      parseCursor: (cursor) => ({
        deletedAt: new Date(cursor.deletedAt),
        id: cursor.id,
      }),
    });
  }

  withSpace(
    eb: ExpressionBuilder<DB, 'pages'>,
    opts?: { includeArchivedAt?: boolean },
  ) {
    return jsonObjectFrom(
      eb
        .selectFrom('spaces')
        .select(['spaces.id', 'spaces.name', 'spaces.slug'])
        .$if(Boolean(opts?.includeArchivedAt), (qb) =>
          qb.select('spaces.archivedAt'),
        )
        .whereRef('spaces.id', '=', 'pages.spaceId'),
    ).as('space');
  }

  withCreator(eb: ExpressionBuilder<DB, 'pages'>) {
    return jsonObjectFrom(
      eb
        .selectFrom('users')
        .select(['users.id', 'users.name', 'users.avatarUrl'])
        .whereRef('users.id', '=', 'pages.creatorId'),
    ).as('creator');
  }

  withLastUpdatedBy(eb: ExpressionBuilder<DB, 'pages'>) {
    return jsonObjectFrom(
      eb
        .selectFrom('users')
        .select(['users.id', 'users.name', 'users.avatarUrl'])
        .whereRef('users.id', '=', 'pages.lastUpdatedById'),
    ).as('lastUpdatedBy');
  }

  withDeletedBy(eb: ExpressionBuilder<DB, 'pages'>) {
    return jsonObjectFrom(
      eb
        .selectFrom('users')
        .select(['users.id', 'users.name', 'users.avatarUrl'])
        .whereRef('users.id', '=', 'pages.deletedById'),
    ).as('deletedBy');
  }

  withContributors(eb: ExpressionBuilder<DB, 'pages'>) {
    return jsonArrayFrom(
      eb
        .selectFrom('users')
        .select(['users.id', 'users.name', 'users.avatarUrl'])
        .whereRef('users.id', '=', sql`ANY(${eb.ref('pages.contributorIds')})`),
    ).as('contributors');
  }

  withHasChildren(eb: ExpressionBuilder<DB, 'pages'>) {
    return eb
      .selectFrom('pages as child')
      .select((eb) =>
        eb
          .case()
          .when(eb.fn.countAll(), '>', 0)
          .then(true)
          .else(false)
          .end()
          .as('count'),
      )
      .whereRef('child.parentPageId', '=', 'pages.id')
      .where('child.deletedAt', 'is', null)
      .limit(1)
      .as('hasChildren');
  }

  async getPageAndDescendants(
    parentPageId: string,
    opts: { includeContent: boolean; trx?: KyselyTransaction },
  ) {
    return dbOrTx(this.db, opts.trx)
      .withRecursive('page_hierarchy', (db) =>
        db
          .selectFrom('pages')
          .select([
            'id',
            'slugId',
            'title',
            'icon',
            'coverPhoto',
            'coverPosition',
            'coverAttribution',
            'position',
            'parentPageId',
            'spaceId',
            'workspaceId',
            'createdAt',
            'updatedAt',
          ])
          .$if(opts?.includeContent, (qb) => qb.select('content'))
          .where('id', '=', parentPageId)
          .where('deletedAt', 'is', null)
          .unionAll((exp) =>
            exp
              .selectFrom('pages as p')
              .select([
                'p.id',
                'p.slugId',
                'p.title',
                'p.icon',
                'p.coverPhoto',
                'p.coverPosition',
                'p.coverAttribution',
                'p.position',
                'p.parentPageId',
                'p.spaceId',
                'p.workspaceId',
                'p.createdAt',
                'p.updatedAt',
              ])
              .$if(opts?.includeContent, (qb) => qb.select('p.content'))
              .innerJoin('page_hierarchy as ph', 'p.parentPageId', 'ph.id')
              .where('p.deletedAt', 'is', null)
              .whereRef('p.spaceId', '=', 'ph.spaceId'),
          ),
      )
      .selectFrom('page_hierarchy')
      .selectAll()
      .execute();
  }

  async isPageDescendant(
    ancestorPageId: string,
    descendantPageId: string,
    trx?: KyselyTransaction,
  ): Promise<boolean> {
    const result = await dbOrTx(this.db, trx)
      .withRecursive('page_ancestors', (db) =>
        db
          .selectFrom('pages')
          .select(['id', 'parentPageId'])
          .where('id', '=', descendantPageId)
          .union((exp) =>
            exp
              .selectFrom('pages as parent')
              .select(['parent.id', 'parent.parentPageId'])
              .innerJoin(
                'page_ancestors as ancestor',
                'ancestor.parentPageId',
                'parent.id',
              ),
          ),
      )
      .selectFrom('page_ancestors')
      .select('id')
      .where('id', '=', ancestorPageId)
      .executeTakeFirst();

    return Boolean(result);
  }

  /**
   * Get page and all descendants, excluding restricted pages and their subtrees.
   * More efficient than getPageAndDescendants + filtering because:
   * 1. Single DB query (no separate restricted IDs query)
   * 2. Stops traversing at restricted pages (doesn't fetch data to discard)
   * 3. No in-memory filtering needed
   */
  async getPageAndDescendantsExcludingRestricted(
    parentPageId: string,
    opts: { includeContent: boolean },
  ) {
    return (
      this.db
        .withRecursive('page_hierarchy', (db) =>
          db
            .selectFrom('pages')
            .leftJoin('pageAccess', 'pageAccess.pageId', 'pages.id')
            .select([
              'pages.id',
              'pages.slugId',
              'pages.title',
              'pages.icon',
              'pages.position',
              'pages.parentPageId',
              'pages.spaceId',
              'pages.workspaceId',
              'pages.updatedAt',
              sql<boolean>`page_access.id IS NOT NULL`.as('isRestricted'),
            ])
            .$if(opts?.includeContent, (qb) => qb.select('pages.content'))
            .where('pages.id', '=', parentPageId)
            .where('pages.deletedAt', 'is', null)
            .unionAll((exp) =>
              exp
                .selectFrom('pages as p')
                .innerJoin('page_hierarchy as ph', 'p.parentPageId', 'ph.id')
                .leftJoin('pageAccess', 'pageAccess.pageId', 'p.id')
                .select([
                  'p.id',
                  'p.slugId',
                  'p.title',
                  'p.icon',
                  'p.position',
                  'p.parentPageId',
                  'p.spaceId',
                  'p.workspaceId',
                  'p.updatedAt',
                  sql<boolean>`page_access.id IS NOT NULL`.as('isRestricted'),
                ])
                .$if(opts?.includeContent, (qb) => qb.select('p.content'))
                .where('p.deletedAt', 'is', null)
                .whereRef('p.spaceId', '=', 'ph.spaceId')
                // Only recurse into children of non-restricted pages
                .where('ph.isRestricted', '=', false),
            ),
        )
        .selectFrom('page_hierarchy')
        .select([
          'id',
          'slugId',
          'title',
          'icon',
          'position',
          'parentPageId',
          'spaceId',
          'workspaceId',
          'updatedAt',
        ])
        .$if(opts?.includeContent, (qb) => qb.select('content'))
        // Filter out restricted pages from the result
        .where('isRestricted', '=', false)
        .execute()
    );
  }

  /**
   * All pages of a space excluding restricted subtrees.
   * Used by public spaces; a restricted page hides its whole subtree.
   */
  async getSpacePagesExcludingRestricted(spaceId: string) {
    return this.db
      .withRecursive('page_hierarchy', (db) =>
        db
          .selectFrom('pages')
          .leftJoin('pageAccess', 'pageAccess.pageId', 'pages.id')
          .select([
            'pages.id',
            'pages.slugId',
            'pages.title',
            'pages.icon',
            'pages.position',
            'pages.parentPageId',
            'pages.spaceId',
            'pages.workspaceId',
            'pages.updatedAt',
            sql<boolean>`page_access.id IS NOT NULL`.as('isRestricted'),
          ])
          .where('pages.spaceId', '=', spaceId)
          .where('pages.parentPageId', 'is', null)
          .where('pages.deletedAt', 'is', null)
          .unionAll((exp) =>
            exp
              .selectFrom('pages as p')
              .innerJoin('page_hierarchy as ph', 'p.parentPageId', 'ph.id')
              .leftJoin('pageAccess', 'pageAccess.pageId', 'p.id')
              .select([
                'p.id',
                'p.slugId',
                'p.title',
                'p.icon',
                'p.position',
                'p.parentPageId',
                'p.spaceId',
                'p.workspaceId',
                'p.updatedAt',
                sql<boolean>`page_access.id IS NOT NULL`.as('isRestricted'),
              ])
              .where('p.deletedAt', 'is', null)
              .whereRef('p.spaceId', '=', 'ph.spaceId')
              .where('ph.isRestricted', '=', false),
          ),
      )
      .selectFrom('page_hierarchy')
      .select([
        'id',
        'slugId',
        'title',
        'icon',
        'position',
        'parentPageId',
        'spaceId',
        'workspaceId',
        'updatedAt',
      ])
      .where('isRestricted', '=', false)
      .execute();
  }

  async getFirstUnrestrictedRootPage(spaceId: string) {
    return this.db
      .selectFrom('pages')
      .select(['id', 'slugId'])
      .where('spaceId', '=', spaceId)
      .where('parentPageId', 'is', null)
      .where('deletedAt', 'is', null)
      .where(({ not, exists, selectFrom }) =>
        not(
          exists(
            selectFrom('pageAccess')
              .select('pageAccess.id')
              .whereRef('pageAccess.pageId', '=', 'pages.id'),
          ),
        ),
      )
      .orderBy('position', (ob) => ob.collate('C').asc())
      .executeTakeFirst();
  }
}
