// Import types from drizzle schema
import type { FeedViewType } from "@folo-services/constants"
import type {
  collectionsOpenAPISchema,
  entries,
  entriesOpenAPISchema,
  inboxesEntriesOpenAPISchema,
  SettingsModel,
  users,
} from "@folo-services/drizzle"
import type {
  FeedOpenApiSchema as _FeedOpenApiSchema,
  InboxOpenApiSchema as _InboxOpenApiSchema,
} from "@folo-services/shared"
import type { InferInsertModel, InferSelectModel } from "drizzle-orm"
import type z from "zod"

import type { Prettify } from "@/types/utils"

import type {
  DateISOString,
  EntryIdRequest,
  IdRequest,
  SerializedInsertModel,
  SerializedModel,
  StructuredSuccessResponse,
} from "../../types"

type FeedOpenApiSchema = SerializedModel<_FeedOpenApiSchema>
type InboxOpenApiSchema = SerializedModel<_InboxOpenApiSchema>
// Re-export database types with proper date serialization
export type EntryModel = SerializedModel<InferSelectModel<typeof entries>>
export type EntryInsert = SerializedInsertModel<
  InferInsertModel<typeof entries>
>
export type UserModel = SerializedModel<InferSelectModel<typeof users>>

// Entry media types
export interface EntryMedia {
  url: string
  type: "photo" | "video"
  preview_image_url?: string
  width?: number
  height?: number
  blurhash?: string
}

export interface EntryAttachment {
  url: string
  title?: string
  duration_in_seconds?: number
  mime_type?: string
  size_in_bytes?: number
}

// Entry-related API request/response types
export interface EntryGetQuery extends IdRequest {}

export interface EntryTagSummary {
  schemaOrgCategory: string | null
  mediaTopics: string[]
}

export type EntryOpenAPISchema = Prettify<
  SerializedModel<z.infer<typeof entriesOpenAPISchema>> & {
    tags?: EntryTagSummary
  }
>
export type EntryGetByIdResponse = StructuredSuccessResponse<{
  feeds: FeedOpenApiSchema
  entries: Prettify<Omit<EntryOpenAPISchema, "feedId">>
  settings?: EntrySettings
} | null>

export interface EntryWithFeed extends EntryExtraResponse {
  read: boolean
  view: FeedViewType
  from: string[]
  feeds: FeedOpenApiSchema
  entries: Prettify<Omit<EntryOpenAPISchema, "content" | "feedId">> & {
    tags?: EntryTagSummary
  }
}

export type EntryListRequest = {
  view?: FeedViewType
  feedId?: string
  feedIdList?: string[]
  read?: boolean
  limit?: number
  publishedAfter?: DateISOString
  publishedBefore?: DateISOString
  isCollection?: boolean
  isArchived?: boolean
  withContent?: boolean
  excludePrivate?: boolean
  aiSort?: boolean
}

export type EntryListResponse = StructuredSuccessResponse<EntryWithFeed[]>

export interface EntryTagsQueryRequest {
  tags: {
    schemaOrgCategories?: string[]
    mediaTopics?: string[]
  }
  match?: "any" | "all"
  limit?: number
  cursor?: string
  feedId?: string
  withContent?: boolean
}

export type EntryTagsQueryResponse = StructuredSuccessResponse<EntryWithFeed[]>

export interface EntryPreviewRequest extends IdRequest {}

export type EntryPreviewResponse = StructuredSuccessResponse<EntryOpenAPISchema>

export interface EntryReadabilityRequest extends IdRequest {}

export type EntryReadabilityResponse = StructuredSuccessResponse<{
  content?: string
} | null>

export interface EntryTranscriptionRequest {
  url: string
}

export type EntryTranscriptionResponse = StructuredSuccessResponse<{
  srt: string
  duration: number
} | null>

export interface EntryStreamRequest {
  ids: string[]
}

export interface EntryStreamItem {
  id: string
  content: string
}

// Check new entries types
export interface CheckNewEntriesQuery {
  view?: number
  feedId?: string
  feedIdList?: string[]
  read?: boolean
  insertedAfter: number
}

export type CheckNewEntriesResponse = StructuredSuccessResponse<{
  has_new: boolean
  lastest_at?: string
  entry_id?: string
}>

// Read histories types
export interface ReadHistoriesQuery {
  page?: number
  size?: number
}

export type ReadHistoriesResponse = StructuredSuccessResponse<{
  users: Record<string, Pick<UserModel, "id" | "handle" | "name" | "image">>
  total: number
  entryReadHistories: {
    userIds: string[]
    readCount: number
  } | null
}>

// Inbox types
export interface InboxEntryGetQuery extends IdRequest {}

export interface InboxListEntryRequestInput {
  inboxId: string
  read?: boolean
  limit?: number
  publishedAfter?: string
  publishedBefore?: string
}
export type EntrySettings = SettingsModel

export interface EntryExtraResponse {
  collections?: Prettify<
    Pick<z.infer<typeof collectionsOpenAPISchema>, "createdAt">
  >
  settings?: EntrySettings
}

export interface InboxListEntry extends EntryExtraResponse {
  read: boolean
  feeds: InboxOpenApiSchema
  entries: Prettify<
    Omit<SerializedModel<z.infer<typeof inboxesEntriesOpenAPISchema>>, "content">
  >
}
export type InboxListEntryResponse = StructuredSuccessResponse<InboxListEntry[]>
export type InboxEntryGetResponse = StructuredSuccessResponse<{
  feeds: InboxOpenApiSchema
  entries: SerializedModel<z.infer<typeof inboxesEntriesOpenAPISchema>>
} | null>

export interface MarkReadInput extends IdRequest {}

export interface MarkUnreadInput extends IdRequest {}

export interface ReadHistoriesInput extends IdRequest {
  page?: number
  size?: number
}

export interface InboxRemoveInput extends EntryIdRequest {}
