import { requestClient } from '#/api/request';

const prefix = 'store-input/';

export type DashboardPeriod =
  | 'today'
  | 'last7days'
  | 'this_month'
  | 'last30days'
  | 'this_year'
  | 'last365days'
  | 'last_year'
  | 'custom';

export type SalespersonDashboardTrendItem = {
  label: string;
  input_total: number;
  clinic_count: number;
  pharmacy_count: number;
  valid_amount: string;
};

export type SalespersonDashboardRegionItem = {
  region_id: number;
  region_name: string;
  input_count: number;
  clinic_count: number;
  pharmacy_count: number;
};

export type SalespersonDashboardRankingItem = {
  id: number;
  nick_name: string;
  avatar: string;
  code: string;
  count?: number;
  amount?: string;
};

export type SalespersonDashboardStoreAmountItem = {
  store_id: number;
  name: string;
  type: number;
  type_label: string;
  amount: string;
  promoter_nick_name: string;
};

export type SalespersonDashboardInactiveStoreItem = {
  store_id: number;
  name: string;
  type: number;
  type_label: string;
  input_at: number;
  promoter_nick_name: string;
};

export type SalespersonDashboardResult = {
  period: DashboardPeriod;
  start_at: number;
  end_at: number;
  prev_start_at: number;
  prev_end_at: number;
  summary: {
    input_total: number;
    clinic_input_count: number;
    pharmacy_input_count: number;
    pending_count: number;
    valid_amount_total: string;
    prev_input_total: number;
    prev_valid_amount_total: string;
    input_mom_rate: number | null;
    amount_mom_rate: number | null;
  };
  trend: SalespersonDashboardTrendItem[];
  compare_trend: SalespersonDashboardTrendItem[];
  region_stats: SalespersonDashboardRegionItem[];
  input_ranking: SalespersonDashboardRankingItem[];
  amount_ranking: SalespersonDashboardRankingItem[];
  store_amount_ranking: SalespersonDashboardStoreAmountItem[];
  inactive_store_warnings: SalespersonDashboardInactiveStoreItem[];
};

export type SalespersonDashboardParams = {
  period: DashboardPeriod;
  start_at?: number;
  end_at?: number;
};

export async function getSalespersonDashboardApi(params: SalespersonDashboardParams) {
  return requestClient.get<SalespersonDashboardResult>(`${prefix}dashboard`, { params });
}
