// tdd-guard:skip — Next.js server page: Auth-Gate + Count-Queries + Komposition,
// keine eigenständig testbare Logik.
import { redirect } from'next/navigation'
import { createClient } from'@/lib/supabase/server'
import { getUserSession } from'@/lib/auth/permissions'
import MasterDataHomeClient from'@/components/repository/masterdata-home-client'

export default async function MasterDataPage() {
 // Authoritative role lookup — uses user_profiles + roles instead of the
 // planning-module consultants.role column. Masteradmins who are not also
 // listed in the consultants table were previously bounced out of this
 // page; that bug is fixed by switching to the same pattern as the rest
 // of the admin/owner area (see app/admin/users/page.tsx).
 const session = await getUserSession()
 if (!session || (session.role !=='admin'&& session.role !=='masteradmin')) {
 redirect('/repository')
 }

 const supabase = await createClient()

 const [
 { count: supplierCount, data: supplierMeta },
 { count: consultantCount, data: consultantMeta },
 { count: projektStammdatenCount, data: projektStammdatenMeta },
 { count: placeholderCount },
 { count: fieldHintCount, data: fieldHintMeta },
 { count: visibilityPairCount },
 ] = await Promise.all([
 supabase
 .from('supplier_master_data')
 .select('updated_at', { count:'exact'})
 .eq('is_active', true)
 .order('updated_at', { ascending: false })
 .limit(1),
 supabase
 .from('consultants')
 .select('updated_at', { count:'exact'})
 .eq('is_active', true)
 .order('updated_at', { ascending: false })
 .limit(1),
 supabase
 .from('master_data_values')
 .select('updated_at', { count:'exact'})
 .eq('is_active', true)
 .order('updated_at', { ascending: false })
 .limit(1),
 supabase
 .from('master_data_values')
 .select('master_data_types!inner(code)', { count:'exact', head: true })
 .eq('is_active', true)
 .eq('is_hidden', false)
 .eq('master_data_types.code','PLACEHOLDER_TEXTS'),
 supabase
 .from('lsc_tooltips')
 .select('updated_at', { count:'exact'})
 .order('updated_at', { ascending: false })
 .limit(1),
 supabase
 .from('department_visibility')
 .select('id', { count:'exact', head: true }),
 ])

 const supplierLastUpdated = (supplierMeta?.[0] as { updated_at?: string } | undefined)?.updated_at ?? null
 const consultantLastUpdated = (consultantMeta?.[0] as { updated_at?: string } | undefined)?.updated_at ?? null
 const projektStammdatenLastUpdated = (projektStammdatenMeta?.[0] as { updated_at?: string } | undefined)?.updated_at ?? null
 const fieldHintLastUpdated = (fieldHintMeta?.[0] as { updated_at?: string } | undefined)?.updated_at ?? null

 return (
 <MasterDataHomeClient
 supplierCount={supplierCount ?? 0}
 supplierLastUpdated={supplierLastUpdated}
 consultantCount={consultantCount ?? 0}
 consultantLastUpdated={consultantLastUpdated}
 projektStammdatenCount={projektStammdatenCount ?? 0}
 projektStammdatenLastUpdated={projektStammdatenLastUpdated}
 placeholderCount={placeholderCount ?? 0}
 fieldHintCount={fieldHintCount ?? 0}
 fieldHintLastUpdated={fieldHintLastUpdated}
 visibilityPairCount={visibilityPairCount ?? 0}
 />
 )
}
