import { getSupabaseClient } from "../../shared/supabase/client"; import { logger } from "../../shared/utils/logger"; // Week number → region mapping const ROTATION_MAP: Record = { 1: "US", 2: "UK", 3: "AU", 4: "UAE", }; export interface RotationInfo { weekNumber: number; region: string; rotationId: string; } /** * Gets the current rotation region and advances the week counter. * Rotation cycles: US → UK → AU → UAE → US → ... */ export async function getCurrentRotation(): Promise { const db = getSupabaseClient(); // Get the latest rotation record const { data: latest } = await db .from("rotation_state") .select("*") .order("started_at", { ascending: false }) .limit(1) .single(); const currentWeek = latest?.week_number ?? 1; const region = ROTATION_MAP[currentWeek] ?? "US"; logger.info({ currentWeek, region }, "Rotation: current region"); return { weekNumber: currentWeek, region, rotationId: latest?.id ?? "unknown", }; } /** * Creates a new rotation record for the next week. * Call this at the END of a successful run. */ export async function advanceRotation(currentWeek: number): Promise { const db = getSupabaseClient(); const nextWeek = currentWeek >= 4 ? 1 : currentWeek + 1; const nextRegion = ROTATION_MAP[nextWeek]; const { error } = await db.from("rotation_state").insert({ week_number: nextWeek, region: nextRegion, }); if (error) { logger.error({ error }, "Failed to advance rotation"); } else { logger.info({ nextWeek, nextRegion }, "Rotation: advanced to next region"); } } /** * Marks the current rotation run as completed with stats. */ export async function completeRotation( rotationId: string, companiesFound: number, leadsQualified: number ): Promise { const db = getSupabaseClient(); await db .from("rotation_state") .update({ completed_at: new Date().toISOString(), companies_found: companiesFound, leads_qualified: leadsQualified }) .eq("id", rotationId); logger.info({ rotationId, companiesFound, leadsQualified }, "Rotation: completed"); } /** * Converts a region code to ICP geography + search labels. */ export function getRegionConfig(region: string): { countryCode: string; searchLabel: string; industries: string[]; } { const configs: Record = { US: { countryCode: "US", searchLabel: "United States", industries: ["technology", "manufacturing", "logistics", "healthcare", "finance", "retail_tech"], }, UK: { countryCode: "GB", searchLabel: "United Kingdom", industries: ["technology", "finance", "logistics", "professional_services", "manufacturing"], }, AU: { countryCode: "AU", searchLabel: "Australia", industries: ["technology", "mining_tech", "agri_tech", "finance", "healthcare"], }, UAE: { countryCode: "AE", searchLabel: "Dubai UAE", industries: ["technology", "logistics", "real_estate_tech", "finance", "retail"], }, }; return configs[region] ?? configs["US"]; }