import { api } from '@/utils/request'
import type {
  DashboardOverview,
  DashboardTrendPoint,
  DashboardBreakdownItem,
} from '@/types/api'

export const useDashboardStore = defineStore('dashboard', () => {
  // ─── Overview ───
  const overview = ref<DashboardOverview | null>(null)
  const overviewLoading = ref(false)
  const overviewError = ref('')

  // ─── Trend ───
  const trendData = ref<DashboardTrendPoint[]>([])
  const trendLoading = ref(false)
  const trendError = ref('')
  const trendGroupBy = ref<'day' | 'week' | 'month'>('day')
  const trendDataType = ref<string | undefined>(undefined)
  const trendFrom = ref<string | undefined>(undefined)
  const trendTo = ref<string | undefined>(undefined)

  // ─── Breakdown ───
  const breakdownData = ref<DashboardBreakdownItem[]>([])
  const breakdownLoading = ref(false)
  const breakdownError = ref('')
  const breakdownDimension = ref<'company' | 'platform' | 'store'>('company')
  const breakdownDataType = ref<string | undefined>(undefined)
  const breakdownFrom = ref<string | undefined>(undefined)
  const breakdownTo = ref<string | undefined>(undefined)

  async function fetchOverview() {
    overviewLoading.value = true
    overviewError.value = ''
    try {
      overview.value = await api.get<DashboardOverview>('/api/v1/dashboard/overview')
    } catch (err: unknown) {
      const msg = err instanceof Error ? err.message : '获取概览数据失败'
      overviewError.value = msg
      message.error(msg)
    } finally {
      overviewLoading.value = false
    }
  }

  async function fetchTrend() {
    trendLoading.value = true
    trendError.value = ''
    try {
      const params: Record<string, unknown> = {
        group_by: trendGroupBy.value,
        data_type: trendDataType.value,
        from: trendFrom.value,
        to: trendTo.value,
      }
      trendData.value = await api.get<DashboardTrendPoint[]>(
        '/api/v1/dashboard/trend',
        params,
      )
    } catch (err: unknown) {
      const msg = err instanceof Error ? err.message : '获取趋势数据失败'
      trendError.value = msg
      message.error(msg)
    } finally {
      trendLoading.value = false
    }
  }

  async function fetchBreakdown() {
    breakdownLoading.value = true
    breakdownError.value = ''
    try {
      const params: Record<string, unknown> = {
        dimension: breakdownDimension.value,
        data_type: breakdownDataType.value,
        from: breakdownFrom.value,
        to: breakdownTo.value,
      }
      breakdownData.value = await api.get<DashboardBreakdownItem[]>(
        '/api/v1/dashboard/breakdown',
        params,
      )
    } catch (err: unknown) {
      const msg = err instanceof Error ? err.message : '获取分维度数据失败'
      breakdownError.value = msg
      message.error(msg)
    } finally {
      breakdownLoading.value = false
    }
  }

  async function fetchAll() {
    await Promise.allSettled([fetchOverview(), fetchTrend(), fetchBreakdown()])
  }

  return {
    // Overview
    overview,
    overviewLoading,
    overviewError,
    // Trend
    trendData,
    trendLoading,
    trendError,
    trendGroupBy,
    trendDataType,
    trendFrom,
    trendTo,
    // Breakdown
    breakdownData,
    breakdownLoading,
    breakdownError,
    breakdownDimension,
    breakdownDataType,
    breakdownFrom,
    breakdownTo,
    // Actions
    fetchOverview,
    fetchTrend,
    fetchBreakdown,
    fetchAll,
  }
})
