import { readFileSync } from 'fs'
import { join } from 'path'
import { fetchAllTodos } from '@/lib/notion-pm'
import DashboardClient from './components/DashboardClient'

export const revalidate = 300

export const metadata = {
  title: 'ATHENA PM Dashboard | Market Compass',
}

export interface KritischEintrag {
  typ: 'deadline' | 'meilenstein' | 'kapazitaet' | 'ueberfallig'
  projekt: string
  text: string
  farbe: 'rot' | 'gelb'
}

export interface KritischData {
  stand: string
  kritisch: KritischEintrag[]
  allesOk: boolean
}

function loadKritischData(): KritischData {
  try {
    const filePath = join(process.cwd(), 'public', 'pm-kritisch.json')
    const raw = readFileSync(filePath, 'utf-8')
    return JSON.parse(raw) as KritischData
  } catch {
    return { stand: new Date().toISOString(), kritisch: [], allesOk: true }
  }
}

export default async function DashboardPage() {
  const [todos, kritischData] = await Promise.all([
    fetchAllTodos(),
    Promise.resolve(loadKritischData()),
  ])
  const timestamp = new Date().toISOString()
  return <DashboardClient todos={todos} fetchedAt={timestamp} kritischData={kritischData} />
}
