import apiRequest from './client' // * Types for PageSpeed Insights monitoring export interface PageSpeedConfig { site_id: string enabled: boolean frequency: 'daily' | 'weekly' | 'monthly' url: string | null next_check_at: string | null created_at: string updated_at: string } export interface AuditSummary { id: string title: string description: string score: number | null display_value?: string savings_ms?: number category: 'opportunity' | 'diagnostic' | 'passed' } export interface PageSpeedCheck { id: string site_id: string strategy: 'mobile' | 'desktop' performance_score: number | null accessibility_score: number | null best_practices_score: number | null seo_score: number | null lcp_ms: number | null cls: number | null tbt_ms: number | null fcp_ms: number | null si_ms: number | null tti_ms: number | null audits: AuditSummary[] | null triggered_by: 'scheduled' | 'manual' checked_at: string } export async function getPageSpeedConfig(siteId: string): Promise { return apiRequest(`/sites/${siteId}/pagespeed/config`) } export async function updatePageSpeedConfig( siteId: string, config: { enabled: boolean; frequency: string; url?: string } ): Promise { return apiRequest(`/sites/${siteId}/pagespeed/config`, { method: 'PUT', body: JSON.stringify(config), }) } export async function getPageSpeedLatest(siteId: string): Promise { const res = await apiRequest<{ checks: PageSpeedCheck[] }>(`/sites/${siteId}/pagespeed/latest`) return res?.checks ?? [] } export async function getPageSpeedHistory( siteId: string, strategy: 'mobile' | 'desktop' = 'mobile', days = 90 ): Promise { const res = await apiRequest<{ checks: PageSpeedCheck[] }>( `/sites/${siteId}/pagespeed/history?strategy=${strategy}&days=${days}` ) return res?.checks ?? [] } export async function getPageSpeedCheck(siteId: string, checkId: string): Promise { return apiRequest(`/sites/${siteId}/pagespeed/checks/${checkId}`) } export async function triggerPageSpeedCheck(siteId: string): Promise { // * PSI checks take 10-30s per strategy (mobile + desktop sequential = up to 60s) const controller = new AbortController() const timeoutId = setTimeout(() => controller.abort(), 120_000) try { const res = await apiRequest<{ checks: PageSpeedCheck[] }>(`/sites/${siteId}/pagespeed/check`, { method: 'POST', signal: controller.signal, }) return res?.checks ?? [] } finally { clearTimeout(timeoutId) } }