File size: 3,203 Bytes
bd28470
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import { getSupabaseClient } from "../../shared/supabase/client";
import { logger } from "../../shared/utils/logger";

// Week number → region mapping
const ROTATION_MAP: Record<number, string> = {
  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<RotationInfo> {
  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<void> {
  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<void> {
  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<string, { countryCode: string; searchLabel: string; industries: string[] }> = {
    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"];
}