import type { StructuredSuccessResponse } from "@/types"

import { defineModule, defineRoute } from "../../shared/define-module"
import type {
  CheckNewEntriesQuery,
  CheckNewEntriesResponse,
  EntryGetByIdResponse,
  EntryGetQuery,
  EntryListRequest,
  EntryListResponse,
  EntryPreviewRequest,
  EntryPreviewResponse,
  EntryReadabilityRequest,
  EntryReadabilityResponse,
  EntryStreamRequest,
  EntryTagsQueryRequest,
  EntryTagsQueryResponse,
  EntryTranscriptionRequest,
  EntryTranscriptionResponse,
  InboxEntryGetQuery,
  InboxEntryGetResponse,
  InboxListEntryRequestInput,
  InboxListEntryResponse,
  InboxRemoveInput,
  ReadHistoriesInput,
  ReadHistoriesResponse,
} from "./types"

/**
 * Entries module definition with nested routes
 */
export const entriesModule = defineModule({
  name: "entries",
  prefix: "/entries",
  routes: {
    // Basic entry operations
    get: defineRoute<EntryGetQuery, EntryGetByIdResponse>("GET", "/"),
    list: defineRoute<EntryListRequest, EntryListResponse>("POST", "/"),

    preview: defineRoute<EntryPreviewRequest, EntryPreviewResponse>(
      "POST",
      "/preview",
    ),
    readability: defineRoute<EntryReadabilityRequest, EntryReadabilityResponse>(
      "GET",
      "/readability",
    ),
    transcription: defineRoute<
      EntryTranscriptionRequest,
      EntryTranscriptionResponse
    >("GET", "/transcription"),

    stream: defineRoute<EntryStreamRequest, Response>("POST", "/stream", {
      asRaw: true,
    }),

    // Check for new entries
    checkNew: defineRoute<CheckNewEntriesQuery, CheckNewEntriesResponse>(
      "GET",
      "/check-new",
    ),

    tagsQuery: defineRoute<EntryTagsQueryRequest, EntryTagsQueryResponse>(
      "POST",
      "/tags/query",
    ),

    // Read histories
    readHistories: defineRoute<ReadHistoriesInput, ReadHistoriesResponse>(
      "GET",
      "/read-histories/{id}",
    ),

    // Inbox operations (nested)
    inbox: {
      get: defineRoute<InboxEntryGetQuery, InboxEntryGetResponse>(
        "GET",
        "/inbox",
      ),

      list: defineRoute<InboxListEntryRequestInput, InboxListEntryResponse>(
        "POST",
        "/inbox",
      ),

      delete: defineRoute<InboxRemoveInput, StructuredSuccessResponse>(
        "DELETE",
        "/inbox",
      ),
    },
  },
})

// Export the API type
export type EntriesAPI = typeof entriesModule.api
export type * from "./types"
