// Topic CRUD, branch switching, ordering.

import { randomBytes } from 'node:crypto'

import { application } from '@application'
import { assistantTable } from '@data/db/schemas/assistant'
import { messageTable } from '@data/db/schemas/message'
import { pinTable } from '@data/db/schemas/pin'
import { topicTable } from '@data/db/schemas/topic'
import type { DbOrTx } from '@data/db/types'
import { loggerService } from '@logger'
import { DataApiErrorFactory } from '@shared/data/api'
import type { CursorPaginationResponse } from '@shared/data/api/apiTypes'
import type { OrderRequest } from '@shared/data/api/schemas/_endpointHelpers'
import type { EntitySearchItem } from '@shared/data/api/schemas/search'
import type {
  CreateTopicDto,
  DeleteTopicsResult,
  DuplicateTopicDto,
  ListTopicsQuery,
  UpdateTopicDto
} from '@shared/data/api/schemas/topics'
import { chatMessageSourceType } from '@shared/data/types/file'
import type { Topic } from '@shared/data/types/topic'
import type { SQL } from 'drizzle-orm'
import { and, asc, desc, eq, gt, gte, inArray, isNull, lt, notInArray, or, sql } from 'drizzle-orm'

import { getDataService, registerDataService } from './dataServiceRegistry'
import { fileRefService } from './FileRefService'
import { pinService } from './PinService'
import { tagService } from './TagService'
import { applyMoves, insertWithOrderKey } from './utils/orderKey'
import { nullsToUndefined, timestampToISO } from './utils/rowMappers'

const logger = loggerService.withContext('DataApi:TopicService')

const DEFAULT_LIMIT = 50
const MAX_LIMIT = 200

type TopicRow = typeof topicTable.$inferSelect
type TopicEntitySearchItem = Extract<EntitySearchItem, { type: 'topic' }>

function rowToTopic(row: TopicRow): Topic {
  // DB NULL ↔ domain `undefined` boundary — all of Topic's nullable columns are
  // `.optional()` (no `T | null`), so the `{...nullsToUndefined(row)}` skeleton
  // from data-api-in-main.md applies cleanly.
  const clean = nullsToUndefined(row)
  return {
    ...clean,
    createdAt: timestampToISO(row.createdAt),
    updatedAt: timestampToISO(row.updatedAt)
  }
}

function topicScopePredicate(groupId: string | null): SQL {
  return groupId === null ? isNull(topicTable.groupId) : eq(topicTable.groupId, groupId)
}

// Wire format: `pin:<orderKey>` / `topic:<updatedAt>:<id>` / `topic:` (pin exhausted).
type Cursor =
  | { section: 'pin'; orderKey: string }
  | { section: 'topic'; updatedAt: number; id: string }
  | { section: 'topic'; updatedAt: null; id: null }

const FIRST_PAGE_CURSOR: Cursor = { section: 'pin', orderKey: '' }

// Stale/legacy cursors fall back to first page (warn) instead of throwing —
// cursors are opaque server-issued tokens, a 422 here would lock out renderers.
function decodeCursor(raw: string): Cursor {
  const firstColon = raw.indexOf(':')
  if (firstColon < 0) return warnAndFallback(raw, 'no section separator')
  const section = raw.slice(0, firstColon)
  const rest = raw.slice(firstColon + 1)

  if (section === 'pin') {
    return { section: 'pin', orderKey: rest }
  }
  if (section === 'topic') {
    if (rest === '') return { section: 'topic', updatedAt: null, id: null }
    const sep = rest.indexOf(':')
    if (sep < 0) return warnAndFallback(raw, 'malformed topic cursor (missing id separator)')
    const updatedAt = Number(rest.slice(0, sep))
    const id = rest.slice(sep + 1)
    if (!Number.isFinite(updatedAt) || !id) {
      return warnAndFallback(raw, 'malformed topic cursor (bad updatedAt or empty id)')
    }
    return { section: 'topic', updatedAt, id }
  }
  return warnAndFallback(raw, `unknown cursor section "${section}"`)
}

function warnAndFallback(raw: string, reason: string): Cursor {
  logger.warn('decodeCursor: cursor unparseable, falling back to first page', { cursor: raw, reason })
  return FIRST_PAGE_CURSOR
}

function encodePinCursor(orderKey: string): string {
  return `pin:${orderKey}`
}

function encodeTopicCursor(updatedAt: number, id: string): string {
  return `topic:${updatedAt}:${id}`
}

function encodeTopicSectionStart(): string {
  return 'topic:'
}

function buildSearchPredicate(q: string | undefined): SQL | undefined {
  const trimmed = q?.trim()
  if (!trimmed) return undefined
  const escaped = trimmed.replace(/[\\%_]/g, '\\$&')
  const pattern = `%${escaped}%`
  return sql`${topicTable.name} LIKE ${pattern} ESCAPE '\\'`
}

export class TopicService {
  async getById(id: string): Promise<Topic> {
    const db = application.get('DbService').getDb()

    const [row] = await db
      .select()
      .from(topicTable)
      .where(and(eq(topicTable.id, id), isNull(topicTable.deletedAt)))
      .limit(1)

    if (!row) {
      throw DataApiErrorFactory.notFound('Topic', id)
    }

    return rowToTopic(row)
  }

  async ensureTraceId(topicId: string): Promise<string> {
    return application.get('DbService').withWriteTx(async (tx) => {
      const [row] = await tx
        .select({ traceId: topicTable.traceId })
        .from(topicTable)
        .where(and(eq(topicTable.id, topicId), isNull(topicTable.deletedAt)))
        .limit(1)

      if (!row) {
        throw DataApiErrorFactory.notFound('Topic', topicId)
      }
      if (row.traceId) {
        return row.traceId
      }

      const traceId = randomBytes(16).toString('hex')
      await tx.update(topicTable).set({ traceId }).where(eq(topicTable.id, topicId))
      return traceId
    })
  }

  async create(dto: CreateTopicDto): Promise<Topic> {
    const dbService = application.get('DbService')
    const messageService = getDataService('MessageService')
    const groupId = dto.groupId ?? null

    const row = await dbService.withWriteTx(async (tx) => {
      const topicRow = (await insertWithOrderKey(
        tx,
        topicTable,
        {
          name: dto.name,
          assistantId: dto.assistantId,
          groupId,
          activeNodeId: null
        },
        {
          pkColumn: topicTable.id,
          position: 'first',
          scope: topicScopePredicate(groupId)
        }
      )) as TopicRow
      await messageService.createRootMessageTx(tx, topicRow.id)
      return topicRow
    })

    logger.info('Created empty topic', { id: row.id })

    return rowToTopic(row)
  }

  async duplicate(sourceTopicId: string, dto: DuplicateTopicDto): Promise<Topic> {
    const dbService = application.get('DbService')
    const messageService = getDataService('MessageService')

    const copiedTopic = await dbService.withWriteTx(async (tx) => {
      const [sourceTopic] = await tx
        .select()
        .from(topicTable)
        .where(and(eq(topicTable.id, sourceTopicId), isNull(topicTable.deletedAt)))
        .limit(1)
      if (!sourceTopic) throw DataApiErrorFactory.notFound('Topic', sourceTopicId)

      const sourcePathRows = await messageService.getPathRowsToNodeTx(tx, dto.nodeId, { topicId: sourceTopicId })

      const newTopicRow = (await insertWithOrderKey(
        tx,
        topicTable,
        {
          name: dto.name ?? sourceTopic.name,
          isNameManuallyEdited: dto.name !== undefined ? true : sourceTopic.isNameManuallyEdited,
          assistantId: sourceTopic.assistantId,
          groupId: sourceTopic.groupId,
          activeNodeId: null
        },
        {
          pkColumn: topicTable.id,
          // Keep duplicated conversations aligned with newly created agent sessions: newest active work appears first.
          position: 'first',
          scope: topicScopePredicate(sourceTopic.groupId ?? null)
        }
      )) as TopicRow

      // New topic is a creation path → create its virtual root before copying the path
      // (copyPathRowsTx reparents the copied head onto it).
      await messageService.createRootMessageTx(tx, newTopicRow.id)

      const { copiedMessageIds, copiedActiveNodeId } = await messageService.copyPathRowsTx(tx, sourcePathRows, {
        topicId: newTopicRow.id
      })

      // Intentionally copies only topic metadata, root-to-node messages, and chat-message file refs.
      // Pins, tags, trace links, and pruned siblings/descendants stay with their original rows.
      await fileRefService.copyBySourceIdMapTx(tx, chatMessageSourceType, copiedMessageIds)

      const [updatedTopicRow] = await tx
        .update(topicTable)
        .set({ activeNodeId: copiedActiveNodeId })
        .where(eq(topicTable.id, newTopicRow.id))
        .returning()

      return rowToTopic(updatedTopicRow)
    })

    logger.info('Duplicated topic path into new topic', {
      sourceTopicId,
      nodeId: dto.nodeId,
      newTopicId: copiedTopic.id,
      activeNodeId: copiedTopic.activeNodeId
    })

    return copiedTopic
  }

  /** Pin state and ordering go through `/pins` and `/topics/:id/order` — not this DTO. */
  async update(id: string, dto: UpdateTopicDto): Promise<Topic> {
    const db = application.get('DbService').getDb()

    const topic = await db.transaction(async (tx) => {
      const [existing] = await tx
        .select({ id: topicTable.id })
        .from(topicTable)
        .where(and(eq(topicTable.id, id), isNull(topicTable.deletedAt)))
        .limit(1)
      if (!existing) throw DataApiErrorFactory.notFound('Topic', id)

      const updates: Partial<typeof topicTable.$inferInsert> = {}
      if (dto.name !== undefined) updates.name = dto.name
      if (dto.isNameManuallyEdited !== undefined) updates.isNameManuallyEdited = dto.isNameManuallyEdited
      if (dto.assistantId !== undefined) updates.assistantId = dto.assistantId
      if (dto.groupId !== undefined) updates.groupId = dto.groupId

      const [row] = await tx.update(topicTable).set(updates).where(eq(topicTable.id, id)).returning()
      if (!row) throw DataApiErrorFactory.notFound('Topic', id)

      return rowToTopic(row)
    })

    logger.info('Updated topic', { id, changes: Object.keys(dto) })

    return topic
  }

  /**
   * Hard delete + tag/pin purge. Any future soft-delete path MUST also
   * call `pinService.purgeForEntitiesTx(tx, 'topic', [id])` — a surviving pin row
   * makes `listByCursor`'s JOIN silently hide the topic from both sections.
   *
   * TODO: Clean up associated files (images, attachments) from disk.
   */
  async delete(id: string): Promise<void> {
    const dbService = application.get('DbService')
    await dbService.withWriteTx((tx) => this.deleteManyByIdsTx(tx, [id], { requireAll: true }))

    logger.info('Deleted topic', { id })
  }

  async deleteByIds(ids: string[]): Promise<DeleteTopicsResult> {
    const dbService = application.get('DbService')
    const deletedIds = await dbService.withWriteTx((tx) => this.deleteManyByIdsTx(tx, ids, { requireAll: true }))

    logger.info('Deleted topics', { count: deletedIds.length })

    return { deletedIds, deletedCount: deletedIds.length }
  }

  private async deleteManyByIdsTx(
    tx: DbOrTx,
    ids: string[],
    options: { requireAll?: boolean } = {}
  ): Promise<string[]> {
    const uniqueIds = Array.from(new Set(ids))
    if (uniqueIds.length === 0) return []

    const rows = await tx
      .select({ id: topicTable.id })
      .from(topicTable)
      .where(and(inArray(topicTable.id, uniqueIds), isNull(topicTable.deletedAt)))
    const deletedIds = rows.map((row) => row.id)

    if (options.requireAll && deletedIds.length !== uniqueIds.length) {
      const foundIds = new Set(deletedIds)
      const missingId = uniqueIds.find((candidate) => !foundIds.has(candidate)) ?? uniqueIds[0]
      throw DataApiErrorFactory.notFound('Topic', missingId)
    }
    if (deletedIds.length === 0) return []

    const messageService = getDataService('MessageService')
    await messageService.purgeByTopicIdsTx(tx, deletedIds)
    await tagService.purgeForEntitiesTx(tx, 'topic', deletedIds)
    await pinService.purgeForEntitiesTx(tx, 'topic', deletedIds)
    await tx.delete(topicTable).where(inArray(topicTable.id, deletedIds))

    return deletedIds
  }

  async setActiveNode(topicId: string, nodeId: string): Promise<{ activeNodeId: string }> {
    await application.get('DbService').withWriteTx((tx) => this.setActiveNodeTx(tx, topicId, nodeId))
    logger.info('Set active node', { topicId, activeNodeId: nodeId })
    return { activeNodeId: nodeId }
  }

  /**
   * Tx-aware variant — composes inside a caller's transaction (e.g.
   * MessageService.create / fork). Validates the topic is not soft-deleted
   * and the message belongs to it. Skip validation by passing `assumeValid`
   * when the caller has already verified the (topicId, nodeId) pair.
   */
  async setActiveNodeTx(
    tx: DbOrTx,
    topicId: string,
    nodeId: string,
    options: { assumeValid?: boolean } = {}
  ): Promise<void> {
    if (!options.assumeValid) {
      const [topic] = await tx
        .select({ id: topicTable.id })
        .from(topicTable)
        .where(and(eq(topicTable.id, topicId), isNull(topicTable.deletedAt)))
        .limit(1)
      if (!topic) throw DataApiErrorFactory.notFound('Topic', topicId)

      const [message] = await tx
        .select({ topicId: messageTable.topicId, role: messageTable.role })
        .from(messageTable)
        .where(and(eq(messageTable.id, nodeId), isNull(messageTable.deletedAt)))
        .limit(1)
      if (!message || message.topicId !== topicId) {
        throw DataApiErrorFactory.notFound('Message', nodeId)
      }
      // The virtual root is structural and never the active node — pointing activeNodeId
      // at it would make the branch/tree reads resolve to an empty conversation.
      if (message.role === 'root') {
        throw DataApiErrorFactory.invalidOperation(
          'set active node to the virtual root',
          'the virtual root cannot be the active node'
        )
      }
    }

    const updated = await tx
      .update(topicTable)
      .set({ activeNodeId: nodeId })
      .where(and(eq(topicTable.id, topicId), isNull(topicTable.deletedAt)))
      .returning({ id: topicTable.id })
    if (updated.length !== 1) throw DataApiErrorFactory.notFound('Topic', topicId)
  }

  async clearActiveNodeTx(tx: DbOrTx, topicId: string): Promise<void> {
    const updated = await tx
      .update(topicTable)
      .set({ activeNodeId: null })
      .where(and(eq(topicTable.id, topicId), isNull(topicTable.deletedAt)))
      .returning({ id: topicTable.id })
    if (updated.length !== 1) throw DataApiErrorFactory.notFound('Topic', topicId)
  }

  /**
   * Two-section page: pinned topics (via `pin` JOIN, ordered by pin.orderKey)
   * then unpinned (ordered by `updatedAt DESC, id ASC`). A partial pin page
   * spills into the unpinned section to fill `limit`. `topic.orderKey` is
   * maintained but unused at read time — it's there for a future drag-mode
   * toggle.
   */
  async listByCursor(query: ListTopicsQuery = {}): Promise<CursorPaginationResponse<Topic>> {
    const db = application.get('DbService').getDb()
    const limit = Math.min(query.limit ?? DEFAULT_LIMIT, MAX_LIMIT)
    const cursor: Cursor = query.cursor ? decodeCursor(query.cursor) : { section: 'pin', orderKey: '' }
    const search = buildSearchPredicate(query.q)

    const items: Array<{ topic: Topic; pinOrderKey?: string }> = []

    if (cursor.section === 'pin') {
      const pinAfter = cursor.orderKey ? gt(pinTable.orderKey, cursor.orderKey) : undefined
      const pinRows = await db
        .select({ topic: topicTable, pinOrderKey: pinTable.orderKey })
        .from(topicTable)
        .innerJoin(pinTable, and(eq(pinTable.entityType, 'topic'), eq(pinTable.entityId, topicTable.id)))
        .where(and(isNull(topicTable.deletedAt), pinAfter, search))
        .orderBy(asc(pinTable.orderKey), asc(topicTable.id))
        .limit(limit + 1)

      // Stale pin cursor (anchor row deleted between requests) → 0 rows for a
      // non-empty `cursor.orderKey`. Hand back a topic-section-start cursor so
      // the next call advances cleanly instead of restarting topics from the top.
      if (pinRows.length === 0 && cursor.orderKey !== '') {
        return { items: [], nextCursor: encodeTopicSectionStart() }
      }

      const hasMoreInPin = pinRows.length > limit
      for (const row of pinRows.slice(0, limit)) {
        items.push({ topic: rowToTopic(row.topic), pinOrderKey: row.pinOrderKey })
      }

      if (hasMoreInPin) {
        const last = items[items.length - 1]
        return {
          items: items.map((i) => i.topic),
          nextCursor: encodePinCursor(last.pinOrderKey ?? '')
        }
      }

      if (items.length >= limit) {
        return {
          items: items.map((i) => i.topic),
          nextCursor: encodeTopicSectionStart()
        }
      }
    }

    // Tuple cursor `(updatedAt, id)` over `ORDER BY updatedAt DESC, id ASC`:
    // the id tiebreaker prevents dedup/skip across pages when two rows share
    // an updatedAt.
    const remaining = limit - items.length
    const pinnedSubquery = db.select({ id: pinTable.entityId }).from(pinTable).where(eq(pinTable.entityType, 'topic'))

    let topicAfter: SQL | undefined
    if (cursor.section === 'topic' && cursor.updatedAt !== null) {
      topicAfter = or(
        lt(topicTable.updatedAt, cursor.updatedAt),
        and(eq(topicTable.updatedAt, cursor.updatedAt), gt(topicTable.id, cursor.id))
      )
    }

    const topicRows = await db
      .select()
      .from(topicTable)
      .where(and(isNull(topicTable.deletedAt), notInArray(topicTable.id, pinnedSubquery), topicAfter, search))
      .orderBy(desc(topicTable.updatedAt), asc(topicTable.id))
      .limit(remaining + 1)

    const hasMoreInTopic = topicRows.length > remaining
    for (const row of topicRows.slice(0, remaining)) {
      items.push({ topic: rowToTopic(row) })
    }

    let nextCursor: string | undefined
    if (hasMoreInTopic) {
      const last = topicRows[remaining - 1]
      nextCursor = encodeTopicCursor(last.updatedAt, last.id)
    }

    return { items: items.map((i) => i.topic), nextCursor }
  }

  async search(query: { q: string; limit: number; updatedAtFrom?: number }): Promise<TopicEntitySearchItem[]> {
    const db = application.get('DbService').getDb()
    const limit = Math.min(query.limit, MAX_LIMIT)
    const filters: SQL[] = [isNull(topicTable.deletedAt)]
    const search = buildSearchPredicate(query.q)
    if (search) filters.push(search)
    if (query.updatedAtFrom !== undefined) {
      filters.push(gte(topicTable.updatedAt, query.updatedAtFrom))
    }

    const rows = await db
      .select({
        id: topicTable.id,
        name: topicTable.name,
        assistantId: topicTable.assistantId,
        assistantName: assistantTable.name,
        updatedAt: topicTable.updatedAt
      })
      .from(topicTable)
      .leftJoin(assistantTable, and(eq(topicTable.assistantId, assistantTable.id), isNull(assistantTable.deletedAt)))
      .where(and(...filters))
      .orderBy(desc(topicTable.updatedAt), asc(topicTable.id))
      .limit(limit)

    return rows.map((row) => ({
      type: 'topic',
      id: row.id,
      title: row.name,
      subtitle: row.assistantName ?? undefined,
      updatedAt: timestampToISO(row.updatedAt),
      target: { topicId: row.id, assistantId: row.assistantId ?? undefined }
    }))
  }

  async reorder(id: string, anchor: OrderRequest): Promise<void> {
    const db = application.get('DbService').getDb()
    await db.transaction(async (tx) => {
      const [target] = await tx
        .select({ groupId: topicTable.groupId })
        .from(topicTable)
        .where(and(eq(topicTable.id, id), isNull(topicTable.deletedAt)))
        .limit(1)
      if (!target) throw DataApiErrorFactory.notFound('Topic', id)

      await applyMoves(tx, topicTable, [{ id, anchor }], {
        pkColumn: topicTable.id,
        scope: topicScopePredicate(target.groupId)
      })
    })
  }

  /** Cross-scope (mixed `groupId`) batches are rejected with VALIDATION_ERROR. */
  async reorderBatch(moves: Array<{ id: string; anchor: OrderRequest }>): Promise<void> {
    if (moves.length === 0) return

    const db = application.get('DbService').getDb()
    await db.transaction(async (tx) => {
      const ids = moves.map((m) => m.id)
      const targets = await tx
        .select({ id: topicTable.id, groupId: topicTable.groupId })
        .from(topicTable)
        .where(and(inArray(topicTable.id, ids), isNull(topicTable.deletedAt)))

      if (targets.length !== ids.length) {
        const found = new Set(targets.map((t) => t.id))
        const missing = ids.find((id) => !found.has(id)) ?? ids[0]
        throw DataApiErrorFactory.notFound('Topic', missing)
      }

      const scopeValues = new Set(targets.map((t) => t.groupId))
      if (scopeValues.size > 1) {
        const scopeList = [...scopeValues].map((s) => (s === null ? '<null>' : s)).join(', ')
        const message = `reorderBatch: batch spans multiple groupId scopes (${scopeList})`
        throw DataApiErrorFactory.validation({ _root: [message] }, message)
      }

      const [scopeValue] = [...scopeValues]
      await applyMoves(tx, topicTable, moves, {
        pkColumn: topicTable.id,
        scope: topicScopePredicate(scopeValue ?? null)
      })
    })
  }

  async deleteByAssistantId(assistantId: string): Promise<DeleteTopicsResult> {
    const dbService = application.get('DbService')
    const deletedIds = await dbService.withWriteTx(async (tx) => {
      const [assistant] = await tx
        .select({ id: assistantTable.id })
        .from(assistantTable)
        .where(and(eq(assistantTable.id, assistantId), isNull(assistantTable.deletedAt)))
        .limit(1)
      if (!assistant) throw DataApiErrorFactory.notFound('Assistant', assistantId)

      const rows = await tx
        .select({ id: topicTable.id })
        .from(topicTable)
        .where(and(eq(topicTable.assistantId, assistantId), isNull(topicTable.deletedAt)))

      return await this.deleteManyByIdsTx(
        tx,
        rows.map((row) => row.id)
      )
    })

    logger.info('Deleted assistant topics', { assistantId, count: deletedIds.length })

    return { deletedIds, deletedCount: deletedIds.length }
  }
}

export const topicService = new TopicService()

registerDataService('TopicService', topicService)
