import { createAdminClient } from "@/lib/supabase/admin";

const BOT_REGEX = /bot|crawl|spider|googlebot|bingbot|slurp|baidu|yandex|duckduck|facebook|pinterest|whatsapp|twitter|skype|linkedin|preview|fetch|curl|wget|monitor|uptime/i;

export function isBot(userAgent: string | null | undefined): boolean {
  if (!userAgent) return true;
  return BOT_REGEX.test(userAgent);
}

export function clientIp(headers: Headers): string | null {
  const xff = headers.get("x-forwarded-for") || "";
  const first = xff.split(",")[0]?.trim();
  if (first) return first;
  return headers.get("x-real-ip") || null;
}

export function sessionIdFromCookies(cookieHeader: string | null): string | null {
  if (!cookieHeader) return null;
  const m = cookieHeader.match(/bb_sid=([^;]+)/);
  return m ? decodeURIComponent(m[1]) : null;
}

export interface ImpressionInput {
  campaignId: string;
  pageUrl?: string | null;
  pagePath?: string | null;
  referrer?: string | null;
  userAgent?: string | null;
  ipAddress?: string | null;
  isBot?: boolean;
  viewport?: boolean;
  sessionId?: string | null;
}

export interface ClickInput {
  campaignId: string;
  pageUrl?: string | null;
  referrer?: string | null;
  userAgent?: string | null;
  ipAddress?: string | null;
  isBot?: boolean;
  sessionId?: string | null;
}

export async function recordImpression(i: ImpressionInput): Promise<void> {
  const admin = createAdminClient();
  await admin.from("ad_impressions").insert({
    campaign_id: i.campaignId,
    page_url: i.pageUrl ?? null,
    page_path: i.pagePath ?? null,
    referrer: i.referrer ?? null,
    user_agent: i.userAgent ?? null,
    ip_address: i.ipAddress ?? null,
    is_bot: !!i.isBot,
    viewport: !!i.viewport,
    session_id: i.sessionId ?? null,
  });
}

export async function recordClick(c: ClickInput): Promise<void> {
  const admin = createAdminClient();
  await admin.from("ad_clicks").insert({
    campaign_id: c.campaignId,
    page_url: c.pageUrl ?? null,
    referrer: c.referrer ?? null,
    user_agent: c.userAgent ?? null,
    ip_address: c.ipAddress ?? null,
    is_bot: !!c.isBot,
    session_id: c.sessionId ?? null,
  });
}
