import { MockupFutureSignalProvider } from '../provider/MockupFutureSignalProvider'
import type { FutureSignalFilters } from '../provider/IFutureSignalProvider'
import type { FutureSignal } from '../domain/futureSignal'
import type { ReviewStatus } from '../domain/enums'
import type { FutureSignalSummary } from '../domain/dashboard'
import type { ListResponse, ItemResponse } from './types'

const provider = MockupFutureSignalProvider

export const futureSignalService = {
  async getAll(filters?: FutureSignalFilters): Promise<ListResponse<FutureSignal>> {
    const data = await provider.getAll(filters)
    return { data, meta: { total: data.length, page: 1, pageSize: data.length, hasMore: false } }
  },
  async getById(id: string): Promise<ItemResponse<FutureSignal | null>> {
    const data = await provider.getById(id)
    return { data }
  },
  async getByProperty(propertyId: string): Promise<ListResponse<FutureSignal>> {
    const data = await provider.getByProperty(propertyId)
    return { data, meta: { total: data.length, page: 1, pageSize: data.length, hasMore: false } }
  },
  async verify(id: string, verifiedBy: string): Promise<ItemResponse<FutureSignal>> {
    const data = await provider.verify(id, verifiedBy)
    return { data }
  },
  async updateReviewStatus(id: string, status: ReviewStatus): Promise<ItemResponse<FutureSignal>> {
    const data = await provider.updateReviewStatus(id, status)
    return { data }
  },

  async getSignalsForProperty(propertyId: string): Promise<ListResponse<FutureSignal>> {
    const data = await provider.getByProperty(propertyId)
    return { data, meta: { total: data.length, page: 1, pageSize: data.length, hasMore: false } }
  },

  async getSignalSummary(): Promise<FutureSignalSummary> {
    const signals = await provider.getAll()
    const RESTRICTED: readonly string[] = ['CONFIDENTIAL', 'INTERNAL']
    return {
      total: signals.length,
      highConfidence: signals.filter(s => s.confidenceScore >= 0.75).length,
      restricted: signals.filter(s => RESTRICTED.includes(s.sensitivityLevel)).length,
      needsReview: signals.filter(s => !s.isVerified).length,
      avgTimeHorizonMonths:
        signals.length > 0
          ? Math.round(signals.reduce((sum, s) => sum + (s.timeHorizonMonths ?? 0), 0) / signals.length)
          : 0,
      timeHorizonDistribution: {
        short: signals.filter(s => (s.timeHorizonMonths ?? 0) <= 6).length,
        medium: signals.filter(s => { const m = s.timeHorizonMonths ?? 0; return m > 6 && m <= 12 }).length,
        long: signals.filter(s => (s.timeHorizonMonths ?? 0) > 12).length,
      },
    }
  },
}
