// ───────────────────────────────────────────────
// Central Type Definitions
// ───────────────────────────────────────────────
// All shared TypeScript types used across the CRM.
// Grouped by domain: auth, leads, notes, dashboard,
// notifications, chats, and calendar.
// ───────────────────────────────────────────────

// ── Lead Pipeline ──

/** Possible states for a lead in the sales pipeline. */
export type LeadStatus =
  | "open"
  | "contacted"
  | "pending"
  | "closed"
  | "ignored";

/** Access roles assigned to users. */
export type UserRole = "super_admin" | "admin" | "sales" | "dev";

/** A registered user of the CRM. */
export interface User {
  id: string;
  name: string;
  email: string;
  phone?: string;
  role: UserRole;
  active: boolean;
  avatar: string;
  createdAt: string;
}

/** A sales lead record with contact info and pipeline status. */
export interface Lead {
  id: string;
  companyName: string;
  contactName: string;
  email: string;
  phone: string;
  source: string;
  description: string;
  status: LeadStatus;
  assignedUserId: string | null;
  assignedUser: User | null;
  createdAt: string;
  updatedAt: string;
}

/** A note attached to a lead (timeline-style). */
export interface Note {
  id: string;
  leadId: string;
  userId: string;
  authorName: string;
  authorAvatar: string;
  authorRole: UserRole;
  note: string;
  createdAt: string;
  updatedAt: string;
}

// ── Dashboard ──

/** Aggregate statistics returned by the dashboard API. */
export interface DashboardStats {
  totalLeads: number;
  openLeads: number;
  contactedLeads: number;
  pendingLeads: number;
  closedLeads: number;
  ignoredLeads: number;
  conversionRate: number;
  /** Per-month breakdown for sparkline / chart rendering. */
  monthlyBreakdown: { label: string; total: number; open: number; contacted: number; pending: number; closed: number; ignored: number }[];
  leadsPerMonth: { label: string; leads: number; closed: number }[];
  recentLeads: Lead[];
  statusDistribution: { name: string; value: number; color: string }[];
  periodLabel: string;
  /** Trend percentages + direction (up/down) vs prior period. */
  trends: {
    totalLeads: { pct: number; up: boolean };
    openLeads: { pct: number; up: boolean };
    contactedLeads: { pct: number; up: boolean };
    pendingLeads: { pct: number; up: boolean };
    closedLeads: { pct: number; up: boolean };
    ignoredLeads: { pct: number; up: boolean };
    conversionRate: { pct: number; up: boolean };
  };
}

/** A generic column filter used by data tables. */
export interface ColumnFilter {
  id: string;
  value: unknown;
}

// ── Notifications ──

/** Categories of in-app notifications. */
export type NotificationType =
  | "lead_created"
  | "lead_status_changed"
  | "lead_assigned"
  | "chat_message"
  | "note_added"
  | "event_scheduled";

/** A single in-app notification displayed in the bell dropdown. */
export interface Notification {
  id: string;
  type: NotificationType;
  title: string;
  description: string;
  timestamp: string;
  read: boolean;
  link?: string;
}

// ── Chat / Messaging ──

/** A single message within a conversation. */
export interface ChatMessage {
  id: string;
  conversationId: string;
  senderId: string;
  senderName: string;
  senderAvatar: string;
  content: string;
  timestamp: string;
}

/** A conversation thread between two (or more) users. */
export interface Conversation {
  id: string;
  participants: { id: string; name: string; avatar: string; role: string }[];
  lastMessage: string;
  lastMessageTime: string;
  unread: number;
  messages: ChatMessage[];
}

// ── Calendar / Events ──

/** Types of calendar events. */
export type EventType = "call" | "follow_up" | "website_creation";
/** Lifecycle status for a calendar event. */
export type EventStatus = "scheduled" | "completed" | "cancelled" | "rescheduled";

/** A full calendar event with related entities (lead, participant, developer). */
export interface CalendarEvent {
  id: string;
  userId: string;
  participantId: string | null;
  developerId: string | null;
  leadId: string | null;
  conversationId: string | null;
  title: string;
  description: string | null;
  participantNotes: string | null;
  eventType: EventType;
  startTime: string;
  endTime: string | null;
  durationMinutes: number | null;
  status: EventStatus;
  creator: { id: string; name: string; email?: string; role?: string; avatar?: string } | null;
  participant: { id: string; name: string; email?: string; role?: string; avatar?: string } | null;
  developer: { id: string; name: string; email?: string; role?: string; avatar?: string } | null;
  lead: { id: string; companyName: string; contactName: string } | null;
  clientName: string | null;
  clientEmail: string | null;
  clientPhone: string | null;
  createdAt: string;
}
