import { api } from "@/src/lib/api/client"

export type DashboardPeriod = "7d" | "30d" | "90d" | "12m"

export type DashboardKpis = {
  total_tenants: number
  prev_tenants:  number
  total_users:   number
  prev_users:    number
  total_cases:   number
  total_mrr:     number
}

export type GrowthPoint = {
  name:    string
  tenants: number
  users:   number
  cases:   number
}

export type PlanDistPoint = {
  name:  string
  value: number
}

export type TenantHealthRow = {
  id:     number
  name:   string
  cases:  number
  users:  number
  plan:   string
  status: "active" | "suspended" | string
  mrr:    number
}

export type AuditLogEntry = {
  action:       string
  actor:        string
  tenant:       string | null
  entity_type:  string | null
  entity_label: string | null
  metadata:     any
  created_at:   string
}

export type AdminDashboardStats = {
  kpis:              DashboardKpis
  growth:            GrowthPoint[]
  plan_distribution: PlanDistPoint[]
  tenant_health:     TenantHealthRow[]
  audit_log:         AuditLogEntry[]
}

export async function getAdminDashboardStats(
  period: DashboardPeriod = "30d",
): Promise<AdminDashboardStats> {
  return api.request<AdminDashboardStats>(
    `/admin-dashboard/?period=${period}`,
    { method: "GET" },
  )
}
