/**
 * Shared type definitions matching the cultguard PostgreSQL schema.
 */

// ─── Core DB types ──────────────────────────────────────────────────────────

export interface Investigation {
  id: string;
  name: string;
  description?: string;
  target_url?: string;
  status?: "active" | "closed" | "archived";
  started_at?: Date;
  closed_at?: Date;
  metadata?: Record<string, unknown>;
}

export type EntityType = "profile" | "page" | "phone" | "domain" | "ip" | "org";
export type Platform = "facebook" | "google" | "instagram" | "telegram" | "twitter";

export interface Entity {
  id: string;
  investigation_id: string;
  type: EntityType;
  name?: string;
  url?: string;
  platform: Platform;
  country?: string;
  created_at?: Date;
  collected_at?: Date;
  confidence: number;
  is_sock: boolean;
  notes?: string;
  raw_json?: Record<string, unknown>;
}

export type ContentType = "post" | "comment" | "ad" | "bio" | "caption";
export type TextLang = "ar" | "en";
export type TextDialect = "lebanese" | "palestinian" | "gulf" | "msa";

export interface Content {
  id: string;
  investigation_id: string;
  entity_id?: string;
  type: ContentType;
  text?: string;
  text_lang?: TextLang;
  text_dialect?: TextDialect;
  published_at?: Date;
  collected_at?: Date;
  url?: string;
  likes?: number;
  shares?: number;
  comments?: number;
  reactions?: Record<string, number>;
  // Ad-specific fields
  ad_spend_min?: number;
  ad_spend_max?: number;
  ad_impressions?: number;
  ad_region_dist?: Record<string, number>;
  ad_start_at?: Date;
  ad_end_at?: Date;
  raw_json?: Record<string, unknown>;
}

export type MediaType = "image" | "video" | "screenshot" | "thumbnail" | "audio";

export interface Media {
  id: string;
  investigation_id: string;
  entity_id?: string;
  content_id?: string;
  type: MediaType;
  file_path?: string;
  url_original?: string;
  sha256?: string;
  width?: number;
  height?: number;
  duration_secs?: number;
  exif_data?: Record<string, unknown>;
  is_ai_generated?: boolean;
  ai_score?: number;
  reverse_search?: Record<string, unknown>;
  collected_at?: Date;
  notes?: string;
}

export type RelationshipType =
  | "follows"
  | "commented_on"
  | "admin_of"
  | "employer"
  | "linked_to"
  | "same_operator"
  | "reposts";

export interface Relationship {
  id?: number;
  investigation_id: string;
  source_id: string;
  target_id: string;
  type: RelationshipType;
  strength: number;
  evidence?: string;
  collected_at?: Date;
}

export type IdentifierType =
  | "phone"
  | "email"
  | "username"
  | "ip"
  | "domain"
  | "wallet"
  | "gaia_id";

export interface Identifier {
  id?: number;
  investigation_id: string;
  entity_id: string;
  type: IdentifierType;
  value: string;
  country_code?: string;
  carrier?: string;
  lookup_result?: Record<string, unknown>;
  collected_at?: Date;
}

export interface Annotation {
  id?: number;
  investigation_id: string;
  ref_type: "entity" | "content" | "media" | "relationship" | "claim";
  ref_id: string;
  analyst: "human" | "llm" | "subagent";
  note: string;
  confidence?: number;
  created_at?: Date;
}

export interface HttpLog {
  id?: number;
  investigation_id: string;
  url: string;
  method: string;
  request_headers?: Record<string, string>;
  request_body?: string;
  response_status?: number;
  response_headers?: Record<string, string>;
  response_body?: string;
  file_path?: string;
  collected_at?: Date;
  session_id?: string;
  notes?: string;
}

// ─── Facebook-specific types ────────────────────────────────────────────────

export interface FbPageData {
  page_id: string;
  name: string;
  category?: string;
  sub_category?: string;
  about?: string;
  description?: string;
  follower_count?: number;
  like_count?: number;
  checkin_count?: number;
  verified?: boolean;
  website?: string;
  phone?: string;
  email?: string;
  address?: string;
  hours?: Record<string, string>;
  price_range?: string;
  profile_photo_url?: string;
  cover_photo_url?: string;
  creation_date?: string;
  transparency?: FbTransparencyData;
  raw?: Record<string, unknown>;
}

export interface FbTransparencyData {
  manager_countries?: string[];
  name_history?: Array<{ name: string; changed_at?: string }>;
  created_date?: string;
  runs_political_ads?: boolean;
  linked_instagram?: string[];
  merge_history?: string[];
  raw?: Record<string, unknown>;
}

export interface FbPostData {
  post_id: string;
  author_id?: string;
  author_name?: string;
  text?: string;
  post_type?: "photo" | "video" | "link" | "text" | "event" | "share";
  published_at?: string;
  media_urls?: string[];
  video_urls?: string[];
  link_url?: string;
  reaction_counts?: Record<string, number>; // { like: N, love: N, ... }
  comment_count?: number;
  share_count?: number;
  raw?: Record<string, unknown>;
}

export interface FbCommentData {
  comment_id: string;
  post_id: string;
  author_id: string;
  author_name: string;
  author_url?: string;
  author_photo_url?: string;
  text: string;
  published_at?: string;
  reaction_count?: number;
  replies?: FbCommentData[];
  raw?: Record<string, unknown>;
}

export interface FbReactionData {
  post_id: string;
  user_id: string;
  user_name: string;
  user_url?: string;
  reaction_type: "like" | "love" | "haha" | "wow" | "sad" | "angry" | "care";
}

export interface FbAdData {
  ad_id: string;
  page_id: string;
  is_active: boolean;
  creative_text?: string;
  creative_image_url?: string;
  creative_video_url?: string;
  start_date?: string;
  end_date?: string;
  spend_min?: number;
  spend_max?: number;
  impressions_min?: number;
  impressions_max?: number;
  region_distribution?: Record<string, number>;
  raw?: Record<string, unknown>;
}

// ─── Embedding types ────────────────────────────────────────────────────────

export interface TextEmbeddingRow {
  ref_type: "entity" | "content" | "annotation" | "claim";
  ref_id: string;
  chunk_idx?: number;
  text: string;
  embedding: number[];
}

export interface ImageEmbeddingRow {
  media_id: string;
  embedding: number[];
}
