// Auto-generated types for ai module
import type { languageSchema } from "@folo-services/drizzle"
import type { z } from "zod"

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

type LanguageSchema = z.infer<typeof languageSchema>

// Chat module types
export interface ChatMessage {
  role: string
  content: string
}

export interface ChatContext {
  mainEntryId?: string
  referEntryIds?: string[]
  referFeedIds?: string[]
}

export interface ChatRequest {
  messages: ChatMessage[]
  model?: "openai/gpt-5-mini" | (string & {})
  context?: ChatContext
}

// Chat response is a streaming Response object for text/event-stream
export type ChatResponse = Response

// Summary module types
export interface SummaryRequest {
  id: string
  language?: LanguageSchema
  target?: "content" | "readabilityContent"
}

export type SummaryResponse = StructuredSuccessResponse<string | null>

// Translation module types
export type EntryStringKey =
  | "title" |
  "description" |
  "content" |
  "readabilityContent"

export interface TranslationRequest {
  id: string
  language: LanguageSchema
  fields: string
  part?: string
}

export interface TranslationData {
  title?: string
  description?: string
  content?: string
  readabilityContent?: string
}

export type TranslationResponse =
  StructuredSuccessResponse<TranslationData | null>
export interface TranslationBatchRequest {
  ids: string[]
  language: LanguageSchema
  fields: string
  part?: string
  mode?: "bilingual" | "translation-only"
}

export interface TranslationBatchStreamChunk {
  id: string
  data: TranslationData
}

// Streaming NDJSON response where each line is a TranslationBatchStreamChunk
type StreamingJsonResponse<T> = Response & {
  body: ReadableStream<T>
}
export type TranslationBatchResponse =
  StreamingJsonResponse<TranslationBatchStreamChunk>

// Text-to-speech request types
export type TextToSpeechSource =
  | "auto" |
  "content" |
  "readabilityContent" |
  "summary"

export interface TextToSpeechRequest {
  entryId?: string
  text?: string
  voice?: string
  source?: TextToSpeechSource
}

// The TTS endpoint returns a streaming audio Response
export type TextToSpeechResponse = Response

// Title generation module types
export interface TitleRequest {
  chatId?: string
  messages: ChatMessage[]
}

export interface TitleResponse {
  title: string
  remainingTokens?: number
}

export interface TitleErrorResponse {
  error: string
}

// Daily summary module types
export interface DailyRequest {
  startDate: string
  view: "0" | "1"
}

export type DailyResponse = StructuredSuccessResponse<string>

// eslint-disable-next-line @typescript-eslint/no-empty-object-type
export interface UsageRequest {}

export type UsageResponse = StructuredSuccessResponse<{
  total: number
  used: number
  remaining: number
  resetAt: Date
}>

// Config module types
export interface RateLimit {
  maxTokens: number
  currentTokens: number
  remainingTokens: number
  windowDuration: number
  windowResetTime: number
  usageRate?: number
  projectedLimitTime?: number | null
  warningLevel?: string
}

export interface AttachmentLimits {
  maxFiles: number
  remainingFiles: number
  windowDuration: number
  windowResetTime: number
}

export interface TokenUsage {
  total: number
  used: number
  remaining: number
  resetAt: Date
}

export interface FreeQuota {
  shouldCheckDailyLimit: boolean
  remainingRequests: number
  remainingMonthlyRequests: number
  role: string
  dailyLimit: number
  monthlyLimit: number
}

export interface TokenUsageHistory {
  id: string
  createdAt: Date
  changes: number
  comment: string | null
}

export interface ConfigResponse {
  defaultModel: string
  availableModels: string[]
  availableModelsMenu: {
    label: string
    value?: string
    paidLevel?: string
  }[]
  rateLimit: RateLimit
  attachmentLimits: AttachmentLimits
  usage: TokenUsage
  freeQuota: FreeQuota
}

export interface AIMemoryRecord {
  id: string
  userId: string
  memory: string
  importance: number
  tags: string[] | null
  source: string | null
  metadata: Record<string, unknown> | null
  createdAt: Date
  updatedAt: Date
  lastAccessedAt: Date | null
  expiresAt: Date | null
}

export interface AIMemoryListResponse {
  data: AIMemoryRecord[]
  nextBefore?: Date
}

export interface AIMemoryCreateRequest {
  memory: string
  importance?: number
  tags?: string[]
  source?: string
  metadata?: Record<string, unknown>
  expiresAt?: Date
}

export interface AIMemoryUpdateRequest {
  memoryId: string
  memory?: string
  importance?: number
  tags?: string[] | null
  source?: string | null
  metadata?: Record<string, unknown> | null
  expiresAt?: Date | null
}

export interface AIMemoryListQuery {
  limit?: number
  before?: Date
  importanceGte?: number
  importanceLte?: number
}
