/** CRM TypeScript types — mirrors backend DTOs (Phase 6.0–6.3). */

export type CrmEntityType = "lead" | "contact" | "organization";
export type CollaborationEntityType =
  | "lead"
  | "contact"
  | "organization"
  | "opportunity"
  | "activity"
  | "task"
  | "meeting";

export type LeadStatus = "new" | "contacted" | "qualified" | "unqualified" | "converted" | "lost";
export type LeadPriority = "low" | "medium" | "high" | "urgent";
export type ContactKind = "business" | "personal";
export type ContactStatus = "active" | "inactive" | "archived";
export type OrganizationKind = "company" | "account" | "partner" | "vendor";
export type OrganizationStatus = "active" | "inactive" | "prospect" | "archived";
export type OpportunityStatus = "open" | "won" | "lost" | "on_hold";
export type QuoteStatus = "draft" | "sent" | "accepted" | "rejected" | "expired";
export type ActivityType = "call" | "email" | "meeting" | "task" | "note" | "other";
export type ActivityStatus = "planned" | "in_progress" | "completed" | "cancelled";
export type ActivityPriority = "low" | "medium" | "high" | "urgent";

export interface Timestamps {
  created_at: string;
  updated_at: string;
}

export interface Lead {
  id: string;
  tenant_id: string;
  lead_number: string;
  first_name: string;
  last_name: string;
  full_name: string;
  title: string | null;
  company: string | null;
  job_title: string | null;
  email: string | null;
  mobile: string | null;
  phone: string | null;
  website: string | null;
  lead_source_id: string | null;
  lead_status_id: string | null;
  status: LeadStatus;
  source: string | null;
  assigned_user_id: string | null;
  priority: LeadPriority;
  estimated_value: string;
  expected_close_date: string | null;
  description: string | null;
  owner_user_id: string;
  contact_id: string | null;
  organization_id: string | null;
  is_deleted: boolean;
  created_by: string | null;
  updated_by: string | null;
  created_at: string;
  updated_at: string;
}

export interface Contact {
  id: string;
  tenant_id: string;
  first_name: string;
  last_name: string;
  full_name: string;
  kind: ContactKind;
  contact_type_id: string | null;
  email: string | null;
  phone: string | null;
  emails: string[] | null;
  phones: string[] | null;
  default_contact_method: string | null;
  social_links: Record<string, string> | null;
  birthday: string | null;
  job_title: string | null;
  job_position: string | null;
  status: ContactStatus;
  organization_id: string | null;
  owner_user_id: string | null;
  notes_summary: string | null;
  is_deleted: boolean;
  created_by: string | null;
  updated_by: string | null;
  created_at: string;
  updated_at: string;
}

export interface Organization {
  id: string;
  tenant_id: string;
  name: string;
  kind: OrganizationKind;
  legal_name: string | null;
  organization_number: string;
  organization_type_id: string | null;
  industry_id: string | null;
  email: string | null;
  phone: string | null;
  website: string | null;
  status: OrganizationStatus;
  owner_user_id: string | null;
  description: string | null;
  is_deleted: boolean;
  created_by: string | null;
  updated_by: string | null;
  created_at: string;
  updated_at: string;
}

export interface Pipeline {
  id: string;
  tenant_id: string;
  name: string;
  code: string;
  description: string | null;
  sort_order: number;
  is_default: boolean;
  is_active: boolean;
  status: string;
  created_at: string;
  updated_at: string;
}

export interface PipelineStage {
  id: string;
  tenant_id: string;
  pipeline_id: string;
  name: string;
  code: string;
  position: number;
  win_probability: number;
  color: string | null;
  is_start_stage: boolean;
  is_won_stage: boolean;
  is_lost_stage: boolean;
  is_closed_stage: boolean;
  required_fields: string[] | null;
  exit_validation: Record<string, unknown> | null;
  is_active: boolean;
  created_at: string;
  updated_at: string;
}

export interface Opportunity {
  id: string;
  tenant_id: string;
  opportunity_number: string;
  name: string | null;
  title: string | null;
  pipeline_id: string;
  stage_id: string;
  amount: string;
  expected_revenue: string | null;
  currency_code: string;
  probability: number;
  priority: string;
  status: OpportunityStatus;
  expected_close_date: string | null;
  organization_id: string | null;
  contact_id: string | null;
  lead_id: string | null;
  owner_user_id: string | null;
  description: string | null;
  is_deleted: boolean;
  created_at: string;
  updated_at: string;
}

export interface SalesActivity {
  id: string;
  tenant_id: string;
  subject: string;
  activity_type: ActivityType;
  status: ActivityStatus;
  priority: ActivityPriority;
  related_type: string | null;
  related_id: string | null;
  lead_id: string | null;
  contact_id: string | null;
  organization_id: string | null;
  opportunity_id: string | null;
  due_at: string | null;
  completed_at: string | null;
  owner_user_id: string | null;
  description: string | null;
  custom_type_label: string | null;
  created_at: string;
  updated_at: string;
}

export interface Quote {
  id: string;
  tenant_id: string;
  quote_number: string;
  title: string | null;
  status: QuoteStatus;
  opportunity_id: string | null;
  contact_id: string | null;
  organization_id: string | null;
  total_amount: string;
  currency_code: string;
  valid_until: string | null;
  owner_user_id: string | null;
  notes: string | null;
  created_at: string;
  updated_at: string;
}

export interface Tag {
  id: string;
  tenant_id: string;
  name: string;
  color: string | null;
  scope: string;
  created_at: string;
}

export interface LookupRead {
  id: string;
  tenant_id: string;
  code: string;
  name: string;
  is_active: boolean;
  created_at: string;
}

export interface Note {
  id: string;
  tenant_id: string;
  entity_type: CrmEntityType;
  entity_id: string;
  body: string;
  version: number;
  created_by: string | null;
  created_at: string;
  updated_at: string;
}

export interface Attachment {
  id: string;
  tenant_id: string;
  entity_type: CrmEntityType;
  entity_id: string;
  file_storage_id: string;
  kind: string;
  title: string | null;
  created_by: string | null;
  created_at: string;
}

export interface CustomField {
  id: string;
  tenant_id: string;
  entity_type: CrmEntityType;
  code: string;
  label: string;
  field_type: string;
  is_required: boolean;
  options: unknown;
  created_at: string;
}

export interface AuditEntry {
  id: string;
  tenant_id: string;
  entity_type: string;
  entity_id: string;
  action: string;
  actor_user_id: string | null;
  changes: Record<string, unknown> | null;
  created_at: string;
}

export interface Task {
  id: string;
  tenant_id: string;
  title: string;
  description: string | null;
  owner_user_id: string | null;
  assignee_user_id: string | null;
  priority: string;
  status: string;
  due_at: string | null;
  completed_at: string | null;
  opportunity_id: string | null;
  lead_id: string | null;
  contact_id: string | null;
  organization_id: string | null;
  activity_id: string | null;
  created_at: string;
  updated_at: string;
}

export interface Meeting {
  id: string;
  tenant_id: string;
  subject: string;
  status: string;
  starts_at: string;
  ends_at: string | null;
  location: string | null;
  outcome: string | null;
  opportunity_id: string | null;
  owner_user_id: string | null;
  created_at: string;
  updated_at: string;
}

export interface CallLog {
  id: string;
  tenant_id: string;
  subject: string;
  direction: string;
  duration_seconds: number | null;
  outcome: string | null;
  opportunity_id: string | null;
  contact_id: string | null;
  owner_user_id: string | null;
  called_at: string;
  created_at: string;
}

export interface TimelineEvent {
  id: string;
  tenant_id: string;
  event_type: string;
  title: string;
  summary: string | null;
  actor_user_id: string | null;
  occurred_at: string;
  created_at: string;
}

export interface Comment {
  id: string;
  tenant_id: string;
  entity_type: CollaborationEntityType;
  entity_id: string;
  body: string;
  parent_id: string | null;
  created_by: string | null;
  created_at: string;
  updated_at: string;
}

export interface Mention {
  id: string;
  tenant_id: string;
  comment_id: string;
  mentioned_user_id: string;
  is_read: boolean;
  created_at: string;
}

export interface TeamMember {
  id: string;
  tenant_id: string;
  user_id: string;
  team_role_id: string | null;
  display_name: string | null;
  is_active: boolean;
  created_at: string;
}

export interface Goal {
  id: string;
  tenant_id: string;
  name: string;
  metric: string;
  scope: string;
  target_value: string;
  achieved_value: string;
  achievement_pct: number;
  period_start: string | null;
  period_end: string | null;
  created_at: string;
}

export interface Target {
  id: string;
  tenant_id: string;
  name: string;
  metric: string;
  target_value: string;
  achieved_value: string;
  period: string;
  owner_user_id: string | null;
  created_at: string;
}

export interface Forecast {
  id: string;
  tenant_id: string;
  name: string;
  forecast_kind: string;
  period: string;
  pipeline_amount: string;
  weighted_amount: string;
  committed_amount: string;
  computed_at: string;
  created_at: string;
}

export interface Playbook {
  id: string;
  tenant_id: string;
  name: string;
  code: string;
  version: number;
  is_active: boolean;
  created_at: string;
}

export interface HealthResponse {
  status: string;
  service: string;
  version: string;
}

export type PaginationParams = {
  page?: number;
  page_size?: number;
};

export type ListParams = PaginationParams & Record<string, string | number | undefined>;
