import { apiClient } from '@/shared/api/client'
import type { ApiResponse } from '@/shared/types/types'
import type { DisputeStatus, OrderStatus } from '@/shared/types/status'

export interface DashboardMetrics {
  total_users: number
  verified_users: number
  total_listings: number
  published_listings: number
  total_orders: number
  renting_orders: number
  today_orders: number
  today_ledger_amount: number
}

export interface DashboardPending {
  listing_reviews: number
  disputes: number
  pending_handoffs: number
  pending_return_confirms: number
}

export interface DashboardRecentOrder {
  id: number
  order_no: string
  title: string
  renter_id: number
  owner_id: number
  status: OrderStatus
  rent_amount: number
  deposit_amount: number
  created_at: string
}

export interface DashboardRecentDispute {
  id: number
  order_id: number
  order_no: string
  title: string
  type: string
  status: DisputeStatus
  initiator_id: number
  created_at: string
}

export interface AdminDashboard {
  metrics: DashboardMetrics
  pending: DashboardPending
  recent_orders: DashboardRecentOrder[]
  recent_disputes: DashboardRecentDispute[]
  generated_at: string
}

export async function fetchAdminDashboard() {
  const { data } = await apiClient.get<ApiResponse<AdminDashboard>>('/admin/dashboard')
  return {
    ...data.data,
    recent_orders: data.data.recent_orders ?? [],
    recent_disputes: data.data.recent_disputes ?? [],
  }
}
