import type { AdminDrawFinanceSummaryData } from "@/types/api/admin-draw-finance";
import type { AdminRiskPoolRow } from "@/types/api/admin-risk";
import type { DrawCurrentSnapshot } from "@/types/api/public-draw";

export type AdminDashboardWarning = {
  code: string;
  message: string;
};

export type AdminDashboardResolvedDraw = {
  id: number;
  draw_no: string;
};

export type AdminDashboardDrawPanel = {
  id: number;
  draw_no: string;
  business_date: string;
  sequence_no: number;
  status: string;
  hall_preview_status: string;
  result_batch_counts: {
    total: number;
    pending_review: number;
    published: number;
  };
};

export type AdminDashboardSoldOutBuckets = {
  d4: number;
  d3: number;
  d2: number;
  special: number;
  other: number;
};

export type AdminDashboardRiskSnapshot = {
  draw_id: number;
  draw_no: string;
  locked_amount: number;
  cap_amount: number;
  usage_percent: number;
  hot_pool_rows: AdminRiskPoolRow[];
  sold_out_buckets: AdminDashboardSoldOutBuckets;
};

export type AdminDashboardCapabilities = {
  draw_finance_risk: boolean;
  wallet_transfer_view: boolean;
};

export type AdminDashboardSiteFinanceOverview = {
  admin_site_id: number;
  site_code: string;
  site_name: string;
  wallet_player_count: number;
  credit_player_count: number;
  pending_confirm_bill_count: number;
  payable_bill_count: number;
  payable_unpaid_minor: number;
  pending_bill_count: number;
  pending_unpaid_minor: number;
  abnormal_transfer_count: number;
  currency_code: string | null;
};

export type AdminDashboardSiteCsOverview = {
  admin_site_id: number;
  site_code: string;
  site_name: string;
  player_count: number;
  ticket_order_count_today: number;
  active_player_count_today: number;
  latest_ticket_at: string | null;
};

/** 站点管理员首页摘要（`GET /api/v1/admin/dashboard` → `site_overview`） */
export type AdminDashboardSiteOverview = {
  admin_site_id: number;
  site_code: string;
  site_name: string;
  agent_count: number;
  player_count: number;
  active_player_count_today: number;
  bet_order_count_today: number;
  today_bet_minor: number;
  today_payout_minor: number;
  today_profit_minor: number;
  seven_day_bet_minor: number;
  seven_day_payout_minor: number;
  seven_day_profit_minor: number;
  profit_scope?: "house_gross";
  currency_code: string | null;
  pending_bill_count: number;
  pending_unpaid_minor: number;
  latest_bet_at: string | null;
  top_agent_today: {
    agent_node_id: number;
    agent_code: string;
    agent_name: string;
    total_bet_minor: number;
    total_payout_minor: number;
    approx_house_gross_minor: number;
  } | null;
};

/** 代理经营账号首页摘要（`GET /api/v1/admin/dashboard` → `agent_overview`） */
export type AdminDashboardAgentOverview = {
  agent_node_id: number;
  agent_code: string;
  agent_name: string;
  depth: number;
  credit_limit: number;
  allocated_credit: number;
  used_credit: number;
  available_credit: number;
  total_share_rate: number;
  can_create_child_agent: boolean;
  can_create_player: boolean;
  direct_child_count: number;
  subtree_agent_count: number;
  direct_player_count: number;
  team_player_count: number;
  active_player_count_today: number;
  bet_order_count_today: number;
  today_bet_minor: number;
  today_payout_minor: number;
  today_profit_minor: number;
  seven_day_bet_minor: number;
  seven_day_payout_minor: number;
  seven_day_profit_minor: number;
  /** 代理视角盈亏口径：本级占成 */
  profit_scope?: "share_profit";
  currency_code: string | null;
  pending_bill_count: number;
  pending_unpaid_minor: number;
  latest_bet_at: string | null;
  top_agent_today: {
    agent_node_id: number;
    agent_code: string;
    agent_name: string;
    total_bet_minor: number;
    total_payout_minor: number;
    approx_house_gross_minor: number;
  } | null;
};

/** 全站待审核开奖批次队列（不限于大厅当前期） */
export type AdminDashboardResultBatchQueue = {
  pending_review_total: number;
  pending_draw_count: number;
  published_total: number;
  batch_total: number;
  first_pending_draw_id: number | null;
  first_pending_batch_id: number | null;
};

/** 全站风险池占用汇总 */
export type AdminDashboardPlatformRisk = {
  locked_amount: number;
  cap_amount: number;
  usage_percent: number;
};

/** 按业务日汇总的今日投注/派彩/盈亏（与报表 daily-profit 口径一致） */
export type AdminDashboardTodayFinance = {
  business_date: string;
  currency_code: string | null;
  total_bet_minor: number;
  total_payout_minor: number;
  approx_house_gross_minor: number;
};

/** 全平台历史累计投注/派彩/盈亏 */
export type AdminDashboardLifetimeFinance = {
  currency_code: string | null;
  total_bet_minor: number;
  total_win_minor: number;
  total_jackpot_minor: number;
  total_payout_minor: number;
  approx_house_gross_minor: number;
  order_count: number;
  ticket_item_count: number;
  draw_count: number;
  business_day_count: number;
  date_from: string | null;
  date_to: string | null;
};

/** `GET /api/v1/admin/dashboard` */
export type AdminDashboardData = {
  hall: DrawCurrentSnapshot | null;
  resolved_draw: AdminDashboardResolvedDraw | null;
  today_finance: AdminDashboardTodayFinance | null;
  lifetime_finance: AdminDashboardLifetimeFinance | null;
  finance: AdminDrawFinanceSummaryData | null;
  draw: AdminDashboardDrawPanel | null;
  risk: AdminDashboardRiskSnapshot | null;
  platform_risk: AdminDashboardPlatformRisk | null;
  result_batch_queue: AdminDashboardResultBatchQueue | null;
  abnormal_transfer_total: number | null;
  warnings: AdminDashboardWarning[];
  capabilities: AdminDashboardCapabilities;
  agent_overview: AdminDashboardAgentOverview | null;
  site_overview: AdminDashboardSiteOverview | null;
  site_finance_overview: AdminDashboardSiteFinanceOverview | null;
  site_cs_overview: AdminDashboardSiteCsOverview | null;
};
