// app/src/types/crmTypes.ts
// Canonical CRM TypeScript types — aligned to Doc 10 (TypeScript Module Spec)
// and Doc 09 (Database Schema and Root API Spec)

// ── Identity ──────────────────────────────────────────────────────────────────

export interface CrmContactListItem {
  person_id: string;
  full_name: string;
  primary_email: string | null;
  primary_phone: string | null;
  buyer_type: string | null;
  lead_id: string | null;
  legacy_li_id: string | null;
  lead_status: string | null;
  budget_band: string | null;
  urgency: string | null;
  primary_interest: string | null;
  intent_score: number;
  engagement_score: number;
  urgency_score: number;
  interaction_count: number;
  last_interaction_at: string | null;
  pending_tasks: number;
  created_at: string | null;
}

export interface CrmPerson {
  person_id: string;
  full_name: string;
  primary_email: string | null;
  primary_phone: string | null;
  buyer_type: string | null;
  persona_labels: string[];
  source_confidence: number;
  created_at: string | null;
}

// ── Lead ─────────────────────────────────────────────────────────────────────

export type CrmLeadStatus =
  | 'new' | 'contacted' | 'qualified' | 'site_visit_scheduled'
  | 'site_visited' | 'negotiation' | 'booking_initiated'
  | 'booked' | 'lost' | 'dormant';

export interface CrmLead {
  lead_id: string;
  status: CrmLeadStatus;
  budget_band: string | null;
  urgency: string | null;
  financing_posture: string | null;
  timeline_to_decision: string | null;
  objections: string[];
  motivations: string[];
  created_at: string | null;
}

// ── Opportunity ───────────────────────────────────────────────────────────────

export type CrmOpportunityStage =
  | 'prospect' | 'qualified' | 'proposal' | 'site_visit'
  | 'negotiation' | 'booking' | 'agreement'
  | 'closed_won' | 'closed_lost';

export interface CrmOpportunityCard {
  opportunity_id: string;
  stage: CrmOpportunityStage;
  value: number | null;
  probability: number | null;
  expected_close_date: string | null;
  next_action: string | null;
  notes?: string | null;
  project_id: string | null;
  unit_id: string | null;
  // When fetched from list endpoint, person-level fields are included
  person_id?: string;
  client_name?: string;
  client_phone?: string;
  project_name?: string;
}

// ── Interaction ───────────────────────────────────────────────────────────────

export type IntelChannel =
  | 'whatsapp' | 'phone' | 'email' | 'site_visit'
  | 'office_meeting' | 'video_call' | 'cctv'
  | 'perception_session' | 'system';

export interface InteractionTimelineItem {
  interaction_id: string;
  channel: IntelChannel;
  interaction_type: string;
  happened_at: string | null;
  summary: string | null;
}

// ── Task / Reminder ───────────────────────────────────────────────────────────

export interface CrmTask {
  reminder_id: string;
  reminder_type: string;
  title: string;
  notes: string | null;
  due_at: string | null;
  status: 'pending' | 'done' | 'snoozed' | 'cancelled';
  priority: 'low' | 'normal' | 'high' | 'urgent';
  // Populated in list view
  person_id?: string;
  client_name?: string;
  client_phone?: string;
}

export interface CrmLeadStageUpdate {
  lead_id: string;
  person_id: string;
  status: CrmLeadStatus;
  budget_band: string | null;
  urgency: string | null;
  client_name?: string;
  client_phone?: string;
}

// ── Property Interest ─────────────────────────────────────────────────────────

export interface PropertyInterest {
  interest_id: string;
  project_name: string;
  unit_preference: string | null;
  configuration: string | null;
  budget_min: number | null;
  budget_max: number | null;
  priority: number;
}

// ── QD Score ──────────────────────────────────────────────────────────────────

export interface QdScoreEntry {
  score_type: string;
  current_value: number;
  computed_at: string | null;
  reasoning: string | null;
}

export interface QdOverview {
  [score_type: string]: QdScoreEntry;
}

// ── Account ───────────────────────────────────────────────────────────────────

export interface AccountLink {
  account_id: string;
  account_name: string;
  account_type: string;
  industry: string | null;
}

// ── Client 360 Snapshot ────────────────────────────────────────────────────────

export interface Client360Snapshot {
  client_ref: string;
  snapshot_type: 'client_360';
  identity: CrmPerson;
  account_links: AccountLink[];
  current_lead: CrmLead | null;
  active_opportunities: CrmOpportunityCard[];
  recent_interactions: InteractionTimelineItem[];
  property_interests: PropertyInterest[];
  tasks: CrmTask[];
  qd_overview: QdOverview;
  risk_flags: string[];
  recommended_next_actions: string[];
  note: string;
}

// ── Import Batch ────────────────────────────────────────────────────────────

export type ImportLifecycle =
  | 'uploaded' | 'parsed' | 'mapped' | 'proposed'
  | 'approved' | 'committed' | 'failed';

export interface ImportBatchSummary {
  batch_id: string;
  source_system: string;
  filename: string;
  row_count: number;
  mapped_count: number;
  unresolved_count: number;
  lifecycle: ImportLifecycle;
  created_at: string | null;
}

export interface ImportProposal {
  proposal_id: string;
  payload: {
    row_number: number;
    canonical_payload: Record<string, string>;
    raw_row: Record<string, string>;
    unresolved_fields: string[];
    missing_required: string[];
    confidence: number;
    review_required: boolean;
  };
  confidence: number;
  status: string;
  review_required: boolean;
}

export type ImportReviewDecision = 'approved' | 'rejected' | 'needs_more_info';

// ── Kanban Board ──────────────────────────────────────────────────────────────

export interface KanbanColumn {
  status: CrmLeadStatus | string;
  label: string;
  count: number;
  items: KanbanCard[];
}

export interface KanbanCard {
  lead_id: string;
  person_id: string;
  client_name: string;
  client_phone: string | null;
  buyer_type: string | null;
  budget_band: string | null;
  urgency: string | null;
  intent_score: number;
}

export interface OracleClientDataListItem {
  person_id: string;
  full_name: string;
  primary_email: string | null;
  primary_phone: string | null;
  buyer_type: string | null;
  broker_name: string | null;
  communication_preference: string | null;
  best_contact_time: string | null;
  lead_id: string | null;
  lead_status: string | null;
  budget_band: string | null;
  urgency: string | null;
  qd_score: number;
  last_contact_at: string | null;
  last_channel: string | null;
  days_since_contact: number | null;
  next_best_action: string | null;
  next_action_priority: string | null;
  projects: string;
  interest_count: number;
}

export interface OracleClientTimelineItem {
  id: string;
  type: string;
  title: string | null;
  summary: string | null;
  date: string | null;
  actor: string | null;
}

export interface OracleClientDataDetail {
  profile: Record<string, unknown>;
  property_interests: Array<Record<string, unknown>>;
  opportunities: Array<Record<string, unknown>>;
  extracted_facts: Array<Record<string, unknown>>;
  qd_scores: Array<Record<string, unknown>>;
  timeline: OracleClientTimelineItem[];
}
