import { propertyService } from './propertyService'
import { matchService } from './matchService'
import { futureSignalService } from './futureSignalService'
import { dataQualityService } from './dataQualityService'
import { reviewService } from './reviewService'
import type { DashboardData } from '../domain/dashboard'

export const dashboardService = {
  async getDashboardData(): Promise<DashboardData> {
    const [propRes, matchRes, signalRes, qualityRes, reviewRes] = await Promise.allSettled([
      propertyService.getDashboardPropertiesSummary(),
      matchService.getStrongMatches(),
      futureSignalService.getSignalSummary(),
      dataQualityService.getPortfolioQualitySummary(),
      reviewService.getDashboardTasks(),
    ])

    const propSummary = propRes.status === 'fulfilled' ? propRes.value : null
    const strongMatches = matchRes.status === 'fulfilled' ? matchRes.value : null
    const signals = signalRes.status === 'fulfilled' ? signalRes.value : null
    const quality = qualityRes.status === 'fulfilled' ? qualityRes.value : null
    const tasks = reviewRes.status === 'fulfilled' ? reviewRes.value : null

    return {
      totalProperties: propSummary?.total ?? 0,
      activeProperties: propSummary?.active ?? 0,
      strongMatchCount: strongMatches?.length ?? 0,
      avgDataQuality: quality?.avgScore ?? 0,
      futureSignals: signals,
      dataQuality: quality,
      reviewTasks: tasks,
      strongMatches,
      lastUpdated: new Date().toISOString(),
    }
  },
}
