export interface TicketCategory {
  id: number;
  label: string;
  label_en: string;
}

export interface TicketStatusOption {
  value: string;
  label: string;
  label_en: string;
}

export interface TicketPriorityOption {
  value: string;
  label: string;
  label_en: string;
}

export interface TicketStatus {
  label: string;
  label_en: string;
  key: string;
}

export interface TicketPriority {
  label: string;
  label_en: string;
  key: string;
  color?: string;
}

export interface TicketAssignee {
  id: number;
  name: string;
  email: string;
  initials: string;
  avatar_url?: string | null;
  /** Dadycar team member (vs. the client who wrote the message). */
  from_dadycar?: boolean;
}

export interface TicketAssignableUser extends TicketAssignee {
  active_tickets_count: number;
}

export interface Ticket {
  id: number;
  title: string;
  category: TicketCategory | null;
  priority: TicketPriority;
  status: TicketStatus;
  description: string;
  company: {
    id: number;
    name: string;
  };
  created_by: {
    id: number;
    name: string;
    email: string;
    phone?: string | null;
    initials?: string;
    avatar_url?: string | null;
  };
  assignee: TicketAssignee | null;
  assigned_at: string | null;
  created_at: string;
  last_message_at: string | null;
  last_message_by: "client" | "dadycar" | null;
  last_client_message_at: string | null;
  last_dadycar_message_at: string | null;
  /** Last word in the client conversation is the client's and the ticket is still active. */
  awaiting_reply: boolean;
  /** Client messages the current agent has not read yet. */
  unread_count: number | null;
  last_message: TicketLastMessage | null;
  /** Marked as answered: the client is asked to confirm. */
  resolved_at: string | null;
  /** Confirmed by the client (or closed): nobody replies any more. */
  closed_at: string | null;
}

export interface TicketLastMessage {
  id: number;
  kind: TicketMessageKind;
  body: string | null;
  created_at: string;
  from_dadycar: boolean;
  author_id: number | null;
  author_name: string | null;
  attachments_count: number;
  attachment_type: "image" | "audio" | "file" | null;
}

/** Support inbox views (a conversation is a ticket). */
export type SupportInboxView = "mine" | "unassigned" | "open" | "resolved";

export type SupportInboxCounts = Record<SupportInboxView, number> & {
  /** Conversations (mine or unassigned) holding unread client messages. */
  unread: number;
};

/** Real-time push from the `admin.support` channel. */
export interface SupportActivityEvent {
  ticket_id: number;
  reason: "message" | "assignment" | "status" | "deleted";
  message: TicketMessage | null;
}

export interface TicketDocument {
  id: number;
  file_path: string;
  uploaded_file_name: string;
  file_size: number;
  created_at: string;
}

export interface TicketDetail extends Ticket {
  updated_at: string;
  documents: TicketDocument[];
  client_conversations_count: number;
}

export interface TicketStats {
  total: number;
  open: number;
  in_progress: number;
  resolved: number;
  closed: number;
  critical: number;
  high: number;
  new_last_30_days: number;
  awaiting_reply: number;
}

export interface UpdateTicketPayload {
  status?: string;
  priority?: string;
  category_id?: number;
  title?: string;
  description?: string;
  assigned_to?: number | null;
}

// PBI 2 — Messages, drafts, mentions

export type TicketMessageKind = "client" | "internal";

export interface TicketMessageAttachment {
  id: number;
  url: string;
  mime_type: string;
  is_image: boolean;
  is_audio?: boolean;
  uploaded_file_name: string;
  file_size: number;
  visibility: "internal" | "public";
}

export interface TicketMessageMention {
  user_id: number;
  name: string;
}

export interface TicketMessage {
  id: number;
  kind: TicketMessageKind;
  body: string;
  edited_at: string | null;
  created_at: string;
  author: TicketAssignee;
  mentions: TicketMessageMention[];
  attachments: TicketMessageAttachment[];
}

export interface TicketDraft {
  kind: TicketMessageKind;
  body: string;
  updated_at: string;
}

export interface CreateTicketMessagePayload {
  kind: TicketMessageKind;
  body?: string;
  attachments?: File[];
  /** User IDs selected in the mention picker (backend notifies them). */
  mentions?: number[];
}

// PBI 3 — Audit log

export type TicketHistoryEventType =
  | "create"
  | "deleted"
  | "status_change"
  | "priority_change"
  | "category_change"
  | "title_change"
  | "description_change"
  | "assign"
  | "unassign"
  | "message_client"
  | "message_internal"
  | "message_edited"
  | "message_deleted"
  | "attachment_added"
  | "attachment_removed";

export interface TicketHistoryEntry {
  id: number;
  event_type: TicketHistoryEventType;
  field: string | null;
  before: string | null;
  after: string | null;
  metadata: Record<string, unknown> | null;
  actor: {
    id: number | null;
    name: string;
    initials: string;
  };
  description_fr: string;
  created_at: string;
}

export interface TicketHistoryPage {
  items: TicketHistoryEntry[];
  next_cursor: number | null;
  has_more: boolean;
}
