import { buildVelocityHeaders } from '@/lib/velocitySession';

const rawApiBase = import.meta.env.VITE_API_URL?.trim();
const DEPLOYED_BACKEND_ORIGIN = 'https://velocity.desineuron.in';

function getBrowserOrigin() {
  if (typeof window !== 'undefined' && window.location?.origin) {
    return window.location.origin;
  }
  return '';
}

export const API_URL = (
  rawApiBase && rawApiBase.length > 0
    ? rawApiBase
    : import.meta.env.DEV
      ? getBrowserOrigin()
      : DEPLOYED_BACKEND_ORIGIN || getBrowserOrigin()
).replace(/\/$/, '');

export const WS_URL = API_URL.replace(/^http/, 'ws');

export interface ScatterDataPoint {
  id: string;
  name: string;
  sentiment_score: number;
  response_time_ms: number;
  score: number;
  qualification: string;
  kanban_status: string;
}

export interface LeadRecord {
  id: string;
  name: string;
  email?: string | null;
  phone?: string | null;
  source: string;
  notes: string;
  qualification: string;
  score: number;
  kanban_status: string;
  stage: string;
  budget: string;
  unit_interest: string;
  metadata: Record<string, unknown>;
  created_at?: string | null;
  updated_at?: string | null;
}

export interface LeadDemographics {
  by_source: Array<{ source: string; lead_count: number; avg_score: number }>;
  by_qualification: Array<{ qualification: string; lead_count: number }>;
}

export interface ChatLogRecord {
  id: string;
  lead_id: string;
  sender: string;
  channel: string;
  content: string;
  metadata: Record<string, unknown>;
  created_at: string | null;
}

export interface MarketingCampaignSummary {
  id: string;
  name: string;
  platform: 'meta' | 'google';
  status: 'active' | 'paused' | 'completed';
  budget: number;
  spent: number;
  impressions: number;
  clicks: number;
  conversions: number;
}

async function requestJson<T>(path: string): Promise<T> {
  const response = await fetch(`${API_URL}${path}`, {
    headers: buildVelocityHeaders(undefined, false),
  });
  if (!response.ok) {
    const body = await response.json().catch(() => ({}));
    throw new Error(
      typeof body?.detail === 'string'
        ? body.detail
        : typeof body?.message === 'string'
          ? body.message
          : `Request failed: ${response.status}`,
    );
  }
  return response.json() as Promise<T>;
}

async function requestWrappedData<T>(path: string): Promise<T> {
  const payload = await requestJson<{ data: T }>(path);
  return payload.data;
}

export async function getSentimentScatter(): Promise<ScatterDataPoint[]> {
  return requestJson<ScatterDataPoint[]>('/api/analytics/sentiment-scatter');
}

export async function getCatalystCampaigns(): Promise<MarketingCampaignSummary[]> {
  return requestWrappedData<MarketingCampaignSummary[]>('/api/catalyst/campaigns');
}

export async function getLeads(): Promise<LeadRecord[]> {
  const payload = await requestJson<{ data: LeadRecord[] }>('/api/leads');
  return payload.data;
}

export async function getLead(leadId: string): Promise<LeadRecord> {
  return requestWrappedData<LeadRecord>(`/api/leads/${leadId}`);
}

export async function getKanbanBoard() {
  return requestWrappedData<Array<{ status: string; stage: string; count: number; items: LeadRecord[] }>>('/api/kanban/board');
}

export async function getChatLogs(leadId?: string): Promise<ChatLogRecord[]> {
  const suffix = leadId ? `?lead_id=${encodeURIComponent(leadId)}` : '';
  return requestWrappedData<ChatLogRecord[]>(`/api/chat-logs${suffix}`);
}

export async function getLeadDemographics(): Promise<LeadDemographics> {
  return requestWrappedData<LeadDemographics>('/api/leads/demographics');
}
