// Import types from drizzle schema
import type { rsshub, rsshubUsage } from "@folo-services/drizzle"
import type { InferSelectModel } from "drizzle-orm"

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

// Re-export database types with proper date serialization
export type RSSHubModel = SerializedModel<InferSelectModel<typeof rsshub>>
export type RSSHubUsageModel = SerializedModel<InferSelectModel<typeof rsshubUsage>>

// User type for owner information
export interface RSSHubOwner {
  id: string
  handle: string | null
  name: string | null
  image: string | null
}

// Request types
export interface RSSHubCreateRequest {
  id?: string
  baseUrl: string
  accessKey?: string
}

export interface RSSHubDeleteRequest {
  id: string
}

export interface RSSHubUseRequest extends TwoFactorAuth {
  id: string | null
  durationInMonths?: number
}

export interface RSSHubGetQuery {
  id: string
}

// Response data types
export interface RSSHubListItem {
  id: string
  ownerUserId: string
  price: number
  userLimit: number | null
  description: string | null
  errorMessage: string | null
  errorAt: string | null
  userCount: number
  owner: RSSHubOwner | null
}

export interface RSSHubInstanceDetails extends Omit<RSSHubModel, "baseUrl" | "accessKey"> {
  baseUrl?: string | null
  accessKey?: string | null
}

export interface RSSHubPurchase {
  hash: string | null
  expiresAt: string
}

export interface RSSHubInstanceData {
  instance: RSSHubInstanceDetails
  purchase: RSSHubPurchase | null
}

export interface RSSHubStatusData {
  usage?: RSSHubUsageModel
  purchase: RSSHubPurchase | null
}

// Response types
export type RSSHubCreateResponse = StructuredSuccessResponse<null>
export type RSSHubListResponse = StructuredSuccessResponse<RSSHubListItem[]>
export type RSSHubDeleteResponse = StructuredSuccessResponse<null>
export type RSSHubUseResponse = StructuredSuccessResponse<null>
export type RSSHubGetResponse = StructuredSuccessResponse<RSSHubInstanceData>
export type RSSHubStatusResponse = StructuredSuccessResponse<RSSHubStatusData>
