import type { IAIMonitoringProvider, AIMonitoringFilters } from './IAIMonitoringProvider'
import type { AIOutput } from '../domain/aiOutput'
import type { ReviewStatus } from '../domain/enums'
import { mockAIOutputs } from '../mock-data/aiOutputs'

const store: AIOutput[] = [...mockAIOutputs]

export const MockupAIMonitoringProvider: IAIMonitoringProvider = {
  async getOutputs(filters?: AIMonitoringFilters) {
    let results = [...store]
    if (filters?.type) results = results.filter(o => o.type === filters.type)
    if (filters?.reviewStatus) results = results.filter(o => o.reviewStatus === filters.reviewStatus)
    if (filters?.hasError === true) results = results.filter(o => !!o.error)
    if (filters?.hasError === false) results = results.filter(o => !o.error)
    if (filters?.model) results = results.filter(o => o.model === filters.model)
    return results.sort((a, b) => b.createdAt.localeCompare(a.createdAt))
  },

  async getOutput(id: string) {
    return store.find(o => o.id === id) ?? null
  },

  async updateReviewStatus(id: string, status: ReviewStatus): Promise<AIOutput> {
    const idx = store.findIndex(o => o.id === id)
    if (idx === -1) throw new Error(`AIOutput ${id} not found`)
    store[idx] = { ...store[idx], reviewStatus: status }
    return store[idx]
  },
}
