// Import types from drizzle schema
import type { FeedViewType } from "@folo-services/constants"
import type { lists, subscriptions } from "@folo-services/drizzle"
import type { InboxOpenApiSchema, ListOpenApiSchema } from "@folo-services/shared"
import type { InferInsertModel, InferSelectModel } from "drizzle-orm"

import type {
  SerializedInsertModel,
  SerializedModel,
  SuccessResponse,
} from "../../types"
import type { UserModel } from "../entries/types"
// Import types from other modules
import type { FeedModel } from "../feeds/types"

// Re-export database types specific to subscriptions
export type SubscriptionModel = SerializedModel<
  InferSelectModel<typeof subscriptions>
>
export type SubscriptionInsert = SerializedInsertModel<
  InferInsertModel<typeof subscriptions>
>
type ListModel = SerializedModel<InferSelectModel<typeof lists>>

// Enhanced subscription types based on server implementation
export interface SubscriptionWithFeed extends SubscriptionModel {
  feeds: FeedModel & {
    owner?: Omit<UserModel, "email">
  }
  boost?: {
    boosters: UserModel[]
  }
}

export interface ListSubscriptionResponse {
  lists: ListOpenApiSchema
  category?: string
  feedId: string
  listId: string
  title: string | null
  userId: string
  view: FeedViewType
  isPrivate: boolean
  hideFromTimeline: boolean | null
  createdAt: string
}

export interface InboxSubscriptionResponse {
  inboxes: InboxOpenApiSchema
  feedId: string
  title: string | null
  userId: string
  inboxId: string
  view: FeedViewType
  category: string | null
  isPrivate: boolean
  hideFromTimeline: boolean | null
  createdAt: string
}

// API request/response types
export interface SubscriptionGetQuery {
  view?: FeedViewType
  userId?: string
}

export type SubscriptionGetResponse = SuccessResponse<
  Array<
    SubscriptionWithFeed | ListSubscriptionResponse | InboxSubscriptionResponse
  >
>

export interface SubscriptionCreateRequest {
  url?: string
  listId?: string
  view?: FeedViewType
  category?: string | null
  isPrivate?: boolean
  title?: string | null
  // Two-factor authentication fields
  code?: string
  type?: string
}

export type SubscriptionCreateResponse = {
  code: 0
  feed: FeedModel | null
  list: ListModel | null
  unread: Record<string, number>
}

export interface SubscriptionUpdateRequest {
  view?: number
  category?: string | null
  isPrivate?: boolean
  hideFromTimeline?: boolean | null
  title?: string | null
  feedId?: string
  listId?: string
}

export type SubscriptionUpdateResponse = SuccessResponse<SubscriptionModel>

export interface SubscriptionDeleteRequest {
  feedIdList?: string[]
  listId?: string
  url?: string
  feedId?: string
}

export type SubscriptionDeleteResponse = SuccessResponse<null>

export interface SubscriptionBatchRequest {
  feedIds: string[]
  view?: number
  category?: string | null
  isPrivate?: boolean
  title?: string | null
}

export type SubscriptionBatchResponse = SuccessResponse<null>

// OPML-related types
export interface ParsedSubscription {
  userId: string
  url: string
  view: FeedViewType
  category: string | null
  title: string | null
}

export type SubscriptionParseOpmlResponse = SuccessResponse<{
  subscriptions: ParsedSubscription[]
  remaining: number
}>

export interface ImportSuccessItem {
  id?: string
  url?: string
  title?: string | null
}

export interface ImportConflictItem {
  id: string
  url: string
  title?: string | null
}

export interface ImportErrorItem {
  id?: string
  url: string
  title?: string | null
}

export type SubscriptionImportResponse = SuccessResponse<{
  successfulItems: ImportSuccessItem[]
  conflictItems: ImportConflictItem[]
  parsedErrorItems: ImportErrorItem[]
}>

export interface SubscriptionExportQuery {
  format?: "opml" | "json"
  categoryId?: string
  listId?: string
}

export interface SubscriptionExportResponse {
  content: string
  contentType: string
  filename: string
}
