import type { BackendPaginatedData } from "@/shared/types";
/** Donna (the AI product) — usage and monthly gifts, amounts in EUR. `price` = client price (cost × markup). */

export interface DonnaGift {
  id: number;
  amount_eur: number;
  vehicles_count: number;
  price_per_vehicle: number;
  /** The amount was typed by an admin instead of computed. */
  custom: boolean;
  note?: string | null;
  revoked: boolean;
}

/** A company that is not on the list yet. */
export interface DonnaCandidate {
  id: number;
  name: string;
}

export interface DonnaUsageClientRow {
  company: { id: number; name: string };
  price_per_vehicle: number | null;
  gift_enabled: boolean;
  gift: DonnaGift | null;
  donna: { calls: number; price_eur: number; cost_eur: number; gift_used_eur: number; overage_eur: number };
  platform: { calls: number; price_eur: number; cost_eur: number };
  net_eur: number;
}

export interface DonnaUsageTotals {
  gift_eur: number;
  donna_price_eur: number;
  donna_cost_eur: number;
  overage_eur: number;
  platform_price_eur: number;
  platform_cost_eur: number;
  unassigned_cost_eur: number;
  net_eur: number;
}

export interface DonnaUsageMonth {
  period: string;
  currency: string;
  markup: number;
  gift_rate: number;
  gift_enabled: boolean;
  totals: DonnaUsageTotals;
  clients: DonnaUsageClientRow[];
}

export interface DonnaUsageLine {
  feature: string;
  group: "chat" | "worker" | "platform";
  label: string;
  calls: number;
  price_eur: number;
  cost_eur: number;
}

export interface DonnaWorker {
  key: string;
  label: string;
  job: string | null;
  enabled: boolean | null;
  price_eur: number;
}

export interface DonnaClientDetail {
  period: string;
  company: { id: number; name: string };
  price_per_vehicle: number | null;
  gift_enabled: boolean;
  gift_quote: { vehicles_count: number; price_per_vehicle: number; amount_eur: number } | null;
  donna: {
    gift: (DonnaGift & { granted_at: string | null; seen: boolean }) | null;
    used_eur: number;
    remaining_eur: number;
    overage_eur: number;
    cost_eur: number;
    lines: DonnaUsageLine[];
    workers: DonnaWorker[];
  };
  platform: { offered: boolean; used_eur: number; cost_eur: number; lines: DonnaUsageLine[] };
  daily: { day: string; account: "donna" | "platform"; price_eur: number; cost_eur: number }[];
}

/** Donna's pricing, all managed here: provider prices are USD per million tokens. */
export interface DonnaModelPrice {
  model: string;
  input: number;
  output: number;
  cache_read: number | null;
  cache_write: number | null;
}

export interface DonnaSettings {
  markup: number;
  usd_to_eur: number;
  gift_enabled: boolean;
  gift_rate: number;
  /** Pre-filled when a prospect or a former client asks Dadycar for credit to try Donna. */
  credit_request_default_eur: number;
  ocr_page_usd: number;
  default_price: { input: number; output: number };
  models: DonnaModelPrice[];
}

export interface UpdateDonnaClientPayload {
  donna_price_per_vehicle?: number | null;
  donna_gift_enabled?: boolean;
}

export type DonnaMessageRatingFilter = "up" | "down" | "none";

/** A question to Donna, her answer and the client's rating. */
export interface DonnaMessage {
  id: number;
  created_at: string;
  question: string | null;
  answer: string | null;
  agents: string[];
  rating: "up" | "down" | null;
  comment: string | null;
  rated_at: string | null;
  conversation: { session_id: string | null; title: string | null };
  company: { id: number; name: string } | null;
  user: { name: string; email: string | null };
}

export interface DonnaMessagesPage extends BackendPaginatedData<DonnaMessage> {
  counts: { total: number; up: number; down: number; none: number };
}

export interface DonnaMessagesParams {
  page?: number;
  per_page?: number;
  search?: string;
  rating?: DonnaMessageRatingFilter;
  company_id?: number;
  from?: string;
  to?: string;
}

export type DonnaCorrectionStatus = "pending" | "confirmed" | "rejected";
export type DonnaCorrectionDecision = "confirm" | "reject" | "pending";

/** A client's correction of an agent's answer; once confirmed it guides that agent on similar requests. */
export interface DonnaCorrection {
  id: number;
  /** The agent whose answer was corrected (mcp-agent, executor-agent…): with id, what addresses the correction. */
  agent: string;
  timestamp: string;
  mistake_type: string;
  user_message_raw: string;
  agent_wrong_response: string;
  correction_summary: string;
  status: DonnaCorrectionStatus;
  notes: string;
  /** How many requests the rule applied to since it was confirmed, and when it last did. */
  fired?: number;
  last_fired_at?: string | null;
}

export interface DonnaCorrectionDetail extends DonnaCorrection {
  user_message_pattern: string;
  correction_followup_message: string;
  conversation_context: string;
}

export interface DonnaCorrectionCounts {
  pending: number;
  confirmed: number;
  rejected: number;
}

export interface DonnaCorrectionsOverview {
  /** Every agent, with its counts; an agent that could not be reached says so. */
  agents: { agent: string; reachable: boolean; counts: DonnaCorrectionCounts }[];
  items: DonnaCorrection[];
}

/** What the judge said of one of Donna's answers, after the fact; a verdict never changed an answer. */
export interface DonnaJudgement {
  at: string;
  session_id: string;
  request: string;
  answer: string;
  /** The parts of Donna that took part; none: Donna answered herself. */
  steps: string[];
  meets: boolean;
  score: number;
  issues: string[];
  reason: string;
}

export interface DonnaJudgementsOverview {
  summary: { judged: number; met: number; average_score: number | null };
  items: DonnaJudgement[];
}

export interface UpdateDonnaCorrectionPayload {
  decision?: DonnaCorrectionDecision;
  correction_summary?: string;
  notes?: string;
}

export type DonnaCreditRequestStatus = "pending" | "approved" | "refused";

/** A prospect's or a former client's request for Donna credit offered by Dadycar. */
export interface DonnaCreditRequest {
  id: number;
  status: DonnaCreditRequestStatus;
  company: { id: number; name: string | null; client_status: "client" | "prospect" | "dormant" | "former" | null };
  requested_by: { id: number; name: string; email: string | null } | null;
  message: string | null;
  amount_eur: number | null;
  note: string | null;
  requested_at: string | null;
  handled_at: string | null;
}

export interface DonnaCreditRequestsPage {
  default_amount_eur: number;
  pending_count: number;
  items: DonnaCreditRequest[];
}

export type DonnaBindingProposalStatus = "pending" | "confirmed" | "rejected";

/** A field the auditor proposes for a concept its binding lacks; applied once the admin confirms it. */
export interface DonnaBindingProposal {
  id: number;
  platform: string;
  catalog_version: string;
  concept: string;
  group: string;
  tool: string;
  field: string;
  rows_key: string | null;
  confidence: number;
  reason: string;
  status: DonnaBindingProposalStatus;
  created_at: string;
  decided_at: string | null;
}

export interface DonnaBindingOverview {
  platform: string;
  catalog_version: string | null;
  bound_groups: string[];
  unbound_groups: string[];
  to_bind: string[];
  validation: { problems: { group: string; concept: string | null; kind: string; detail: string }[]; unverified: string[]; unavailable: string[] };
  proposals: DonnaBindingProposal[];
  counts: Record<DonnaBindingProposalStatus, number>;
}

export interface DecideDonnaBindingPayload {
  decision: "confirm" | "reject" | "pending";
  tool?: string;
  field?: string;
  rows_key?: string;
}
