import { researchBridge } from '../lib/researchBridge';
import type {
  ArchiveResearchNoteRequest,
  ArchiveResearchWorkspaceRequest,
  CaptureResearchNoteRequest,
  CreateResearchWorkspaceRequest,
  GetNoteAuditTrailRequest,
  GhostNote,
  ListResearchJobsRequest,
  NoteAuditTrail,
  NoteCaptureResult,
  PipelineJob,
  PreviewLinkRequest,
  PreviewLinkResult,
  PromoteNoteToThesisRequest,
  ResearchGhostUpdateEvent,
  ResearchJobUpdateEvent,
  ResearchNavigationIntent,
  ResearchNote,
  ResearchNoteUpdateEvent,
  ResearchWorkspace,
  ResearchWorkspaceUpdateEvent,
  RetryResearchJobsRequest,
  ReviewGhostNoteRequest,
  SearchResearchNotesRequest,
  UpdateResearchNoteRequest,
  UpdateResearchWorkspaceRequest,
  WorkspaceProjection,
} from '../types/research';

export interface SearchResearchNoteIdsRequest {
  workspaceId: string;
  query: string;
  includeArchived?: boolean;
}

export const normalizeResearchSearchQuery = (query: string) => query.trim();

export const researchApi = {
  listWorkspaces(): Promise<ResearchWorkspace[]> {
    return researchBridge.listResearchWorkspaces();
  },

  getWorkspaceSnapshot(workspaceId: string): Promise<WorkspaceProjection> {
    return researchBridge.getWorkspaceProjection({ workspaceId });
  },

  listJobs(request: ListResearchJobsRequest): Promise<PipelineJob[]> {
    return researchBridge.listResearchJobs(request);
  },

  createWorkspace(request: CreateResearchWorkspaceRequest): Promise<ResearchWorkspace> {
    return researchBridge.createResearchWorkspace(request);
  },

  updateWorkspace(request: UpdateResearchWorkspaceRequest): Promise<ResearchWorkspace> {
    return researchBridge.updateResearchWorkspace(request);
  },

  archiveWorkspace(request: ArchiveResearchWorkspaceRequest): Promise<ResearchWorkspace> {
    return researchBridge.archiveResearchWorkspace(request);
  },

  captureNote(request: CaptureResearchNoteRequest): Promise<NoteCaptureResult> {
    return researchBridge.captureResearchNote(request);
  },

  previewLink(request: PreviewLinkRequest): Promise<PreviewLinkResult> {
    return researchBridge.previewNoteLink(request);
  },

  updateNote(request: UpdateResearchNoteRequest): Promise<ResearchNote> {
    return researchBridge.updateResearchNote(request);
  },

  archiveNote(request: ArchiveResearchNoteRequest): Promise<ResearchNote> {
    return researchBridge.archiveResearchNote(request);
  },

  promoteNote(request: PromoteNoteToThesisRequest): Promise<ResearchNote> {
    return researchBridge.promoteNoteToThesis(request);
  },

  reviewGhost(request: ReviewGhostNoteRequest): Promise<GhostNote> {
    return researchBridge.reviewGhostNote(request);
  },

  retryJobs(request: RetryResearchJobsRequest): Promise<PipelineJob[]> {
    return researchBridge.retryResearchJobs(request);
  },

  getNoteAuditTrail(request: GetNoteAuditTrailRequest): Promise<NoteAuditTrail> {
    return researchBridge.getNoteAuditTrail(request);
  },

  async searchNoteIds(request: SearchResearchNoteIdsRequest): Promise<string[]> {
    const normalizedQuery = normalizeResearchSearchQuery(request.query);
    if (!normalizedQuery) {
      return [];
    }

    const results = await researchBridge.searchResearchNotes({
      workspaceId: request.workspaceId,
      query: normalizedQuery,
      includeArchived: request.includeArchived,
    } satisfies SearchResearchNotesRequest);

    return results.map((note) => note.id);
  },

  listenForWorkspaceUpdates(
    handler: (payload: ResearchWorkspaceUpdateEvent) => void,
  ): Promise<() => void> {
    return researchBridge.listenForWorkspaceUpdates(handler);
  },

  listenForNoteUpdates(
    handler: (payload: ResearchNoteUpdateEvent) => void,
  ): Promise<() => void> {
    return researchBridge.listenForNoteUpdates(handler);
  },

  listenForGhostUpdates(
    handler: (payload: ResearchGhostUpdateEvent) => void,
  ): Promise<() => void> {
    return researchBridge.listenForGhostUpdates(handler);
  },

  listenForJobUpdates(
    handler: (payload: ResearchJobUpdateEvent) => void,
  ): Promise<() => void> {
    return researchBridge.listenForJobUpdates(handler);
  },
};

export const isResearchIntentEmpty = (intent: ResearchNavigationIntent | null | undefined) =>
  !intent ||
  (!intent.ticker &&
    !intent.workspaceId &&
    !intent.preferredView &&
    !intent.seedNote &&
    !intent.terminalNoteSeed);
