// Auto-generated types for discover module
import type {
  AttachmentsModel,
  ExtraModel,
  feedAnalytics,
  MediaModel,
} from "@folo-services/drizzle"
import type { InferSelectModel } from "drizzle-orm"

import type { SerializedModel, StructuredSuccessResponse } from "../../types"

// Discover feed/list request
export interface DiscoverRequest {
  keyword: string
  target?: "feeds" | "lists"
}

// Feed discovery result
export interface FeedDiscoveryResult {
  type: "feed"
  id: string
  url: string
  title: string | null
  description: string | null
  siteUrl: string | null
  image: string | null
  ownerUserId?: string
  owner?: {
    id: string
    name: string
    handle: string
    avatar: string | null
  }
}

// List discovery result
export interface ListDiscoveryResult {
  type: "list"
  id: string
  title: string
  description: string | null
  image: string | null
  view: number
  fee: number
  ownerUserId: string
}

// Entry data for feed preview
export interface EntryData {
  id: string
  title: string | null
  content: string | null
  description: string | null
  publishedAt: string
  url: string | null
  author: string | null
  feedId: string
  media?: MediaModel[] | null
  authorUrl: string | null
  authorAvatar: string | null
  insertedAt: string
  categories?: string[]
  attachments?: AttachmentsModel[]
  extra?: ExtraModel
  language?: string | null
}

type FeedAnalytics = SerializedModel<InferSelectModel<typeof feedAnalytics>>

// Discovery item result
export interface DiscoveryItem {
  feed?: FeedDiscoveryResult
  list?: ListDiscoveryResult
  docs?: string // Documentation URL for RSSHub routes
  entries?: EntryData[]
  subscriptionCount?: number
  updatesPerWeek?: number
  analytics?: FeedAnalytics
}

export type DiscoverResponse = StructuredSuccessResponse<DiscoveryItem[]>

// RSSHub namespaces query
export interface RSSHubQuery {
  category?: string
  namespace?: string
  lang?: string
  categories?: string
}

// RSSHub route metadata
export interface RSSHubRouteMetadata {
  path: string
  categories: string[]
  example: string
  parameters: Record<string, string>
  name: string
  maintainers: string[]
  location: string
  description: string
  view?: number
}

// RSSHub namespace data
export interface RSSHubNamespace {
  routes: Record<string, RSSHubRouteMetadata>
  name: string
  url: string
  description: string
  lang: string
}

// RSSHub response is nested namespaces
export type RSSHubResponse = StructuredSuccessResponse<
  Record<string, RSSHubNamespace>
>

// RSSHub specific route query
export interface RSSHubRouteQuery {
  route: string
}

// RSSHub route detail response
export interface RSSHubRouteData {
  route: RSSHubRouteMetadata
  prefix: string
  name: string
  description: string
  url: string
}

export type RSSHubRouteResponse = StructuredSuccessResponse<RSSHubRouteData>

// RSSHub analytics query
export type RSSHubAnalyticsQuery = Record<string, never>

// RSSHub analytics per route
export interface RSSHubRouteAnalytics {
  subscriptionCount: number
  topFeeds: FeedDiscoveryResult[]
}

// RSSHub analytics response - keyed by rsshub route
export type RSSHubAnalyticsResponse = StructuredSuccessResponse<
  Record<string, RSSHubRouteAnalytics>
>
