import type { IFutureSignalProvider, FutureSignalFilters } from './IFutureSignalProvider'
import type { FutureSignal } from '../domain/futureSignal'
import type { ReviewStatus } from '../domain/enums'
import { mockFutureSignals } from '../mock-data/futureSignals'

const store: FutureSignal[] = [...mockFutureSignals]

export const MockupFutureSignalProvider: IFutureSignalProvider = {
  async getAll(filters?: FutureSignalFilters) {
    let results = [...store]
    if (filters?.signalType) results = results.filter(s => s.signalType === filters.signalType)
    if (filters?.minProbability !== undefined) results = results.filter(s => s.probability >= filters.minProbability!)
    if (filters?.organizationId) results = results.filter(s => s.organizationId === filters.organizationId)
    if (filters?.isVerified !== undefined) results = results.filter(s => s.isVerified === filters.isVerified)
    if (filters?.sensitivityLevel) results = results.filter(s => s.sensitivityLevel === filters.sensitivityLevel)
    if (filters?.reviewStatus) results = results.filter(s => (s.reviewStatus ?? 'UNREVIEWED') === filters.reviewStatus)
    if (filters?.maxTimeHorizonMonths !== undefined) results = results.filter(s => s.timeHorizonMonths <= filters.maxTimeHorizonMonths!)
    return results.sort((a, b) => b.probability - a.probability)
  },
  async getById(id) {
    return store.find(s => s.id === id) ?? null
  },
  async getByProperty(propertyId) {
    return store.filter(s => s.propertyId === propertyId)
  },
  async verify(id, verifiedBy) {
    const idx = store.findIndex(s => s.id === id)
    store[idx] = { ...store[idx], isVerified: true, verifiedBy, verifiedAt: new Date().toISOString(), updatedAt: new Date().toISOString() }
    return store[idx]
  },
  async updateReviewStatus(id, status: ReviewStatus) {
    const idx = store.findIndex(s => s.id === id)
    store[idx] = { ...store[idx], reviewStatus: status, updatedAt: new Date().toISOString() }
    return store[idx]
  },
}
