import type { IDashboardProvider, DashboardStats } from './IDashboardProvider'
import { mockProperties } from '../mock-data/properties'
import { mockMatches } from '../mock-data/matches'
import { mockNeeds } from '../mock-data/needs'
import { mockFutureSignals } from '../mock-data/futureSignals'
import { mockReviewQueue } from '../mock-data/reviewQueue'
import { mockDelay } from '../lib/mockUtils'

export const MockupDashboardProvider: IDashboardProvider = {
  async getStats(organizationId?) {
    await mockDelay()

    let props = mockProperties
    let matches = mockMatches
    let needs = mockNeeds
    let signals = mockFutureSignals
    let queue = mockReviewQueue

    if (organizationId) {
      props = props.filter(p => p.organizationId === organizationId)
      matches = matches.filter(m => m.organizationId === organizationId)
      needs = needs.filter(n => n.organizationId === organizationId)
      signals = signals.filter(s => s.organizationId === organizationId)
      queue = queue.filter(r => r.relatedOrganizationId === organizationId)
    }

    const avgScore =
      matches.length > 0
        ? matches.reduce((sum, m) => sum + m.matchScore, 0) / matches.length
        : 0

    const stats: DashboardStats = {
      totalProperties: props.length,
      verifiedProperties: props.filter(p => p.resultType === 'VERIFIED_PORTFOLIO').length,
      externalMarketProperties: props.filter(p => p.resultType === 'EXTERNAL_MARKET').length,
      futureSignalProperties: props.filter(p => p.resultType === 'FUTURE_AVAILABILITY').length,
      totalMatches: matches.length,
      pendingReviews: queue.filter(r => r.status === 'PENDING').length,
      approvedMatches: matches.filter(m => m.isApproved === true).length,
      activeNeeds: needs.length,
      totalSignals: signals.length,
      verifiedSignals: signals.filter(s => s.isVerified).length,
      averageMatchScore: Math.round(avgScore),
      highConfidenceMatches: matches.filter(m => m.confidenceLevel >= 0.75).length,
    }
    return stats
  },
}
