import { useQuery } from '@tanstack/react-query'
import api from '@/lib/api'
import { FilterableTable } from '@/components/FilterableTable'
import { LoadingSpinner } from '@/components/LoadingSpinner'
import { CollapsibleSection } from '@/components/CollapsibleSection'
import { MetricCard } from '@/components/MetricCard'
import { formatCurrency, formatPercent } from '@/lib/utils'
import { useState, useMemo } from 'react'
import { XAxis, YAxis, CartesianGrid, Tooltip, ResponsiveContainer, ScatterChart, Scatter, ReferenceLine } from 'recharts'
import { MonthPicker } from '@/components/MonthPicker'

export function CostPage() {
  const [month, setMonth] = useState('2026-04')

  const { data: costData, isLoading: costLoading } = useQuery({
    queryKey: ['cost/comparison', month],
    queryFn: () => api.get('/cost/comparison', { params: { month } }),
  })

  const [invOpen, setInvOpen] = useState(false)
  const [catOpen, setCatOpen] = useState(false)

  const { data: invData, isLoading: invLoading } = useQuery({
    queryKey: ['cost/inventory', month],
    queryFn: () => api.get('/cost/inventory', { params: { month } }),
    enabled: invOpen,
  })

  const { data: catData, isLoading: catLoading } = useQuery({
    queryKey: ['cost/category-benchmark', month],
    queryFn: () => api.get('/cost/category-benchmark', { params: { month } }),
    enabled: catOpen,
  })

  const costRows = (costData as any)?.data || []
  const invRows = (invData as any)?.data || []
  const catRows = (catData as any)?.data || []

  const pageLoading = costLoading

  const validCostRows = costRows.filter((r: any) => !r.variance_level?.includes('口径异常'))
  const abnormalRows = costRows.filter((r: any) => r.variance_level?.includes('口径异常'))

  const avgTheoreticalRate = validCostRows.length > 0
    ? validCostRows.reduce((s: number, r: any) => s + Number(r.theoretical_cost_rate_pct || 0), 0) / validCostRows.length
    : 0
  const avgActualRate = validCostRows.length > 0
    ? validCostRows.reduce((s: number, r: any) => s + Number(r.actual_food_cost_rate_pct || 0), 0) / validCostRows.length
    : 0
  const highVarianceCount = validCostRows.filter((r: any) => Number(r.variance_to_theoretical_pct || 0) > 20).length

  const scatterData = validCostRows.map((r: any) => ({
    name: r.store_name,
    theoretical: Number(r.theoretical_cost_rate_pct || 0),
    actual: Number(r.actual_food_cost_rate_pct || 0),
    variance: Number(r.variance_to_theoretical_pct || 0),
  }))

  const scatterDomain = useMemo(() => {
    if (scatterData.length === 0) return { x: [0, 40], y: [0, 50] }
    const xs = scatterData.map((d: any) => d.theoretical)
    const ys = scatterData.map((d: any) => d.actual)
    const xMin = Math.floor(Math.min(...xs) - 1)
    const xMax = Math.ceil(Math.max(...xs) + 1)
    const yMin = Math.floor(Math.min(...ys) - 1)
    const yMax = Math.ceil(Math.max(...ys) + 1)
    return { x: [xMin, xMax], y: [yMin, yMax] }
  }, [scatterData])

  if (pageLoading) {
    return <LoadingSpinner text="加载成本数据..." />
  }

  return (
    <div className="space-y-4">
      <div className="flex items-center justify-between">
        <div>
          <h1 className="text-xl font-bold">成本与库存管理</h1>
          <p className="mt-0.5 text-xs text-muted-foreground">理论vs实际成本 · 原料分类差异 · 库存效率 · {month}</p>
        </div>
        <MonthPicker month={month} onChange={setMonth} />
      </div>

      {/* 概览指标 */}
      <CollapsibleSection title="成本概览" subtitle="全门店成本效率汇总">
        <div className="grid grid-cols-2 gap-3 md:grid-cols-4">
          <MetricCard title="平均理论成本率" value={avgTheoreticalRate} format="percent" description="按标准BOM计算的理论食材成本占实收比例（排除口径异常门店）" />
          <MetricCard title="平均实际成本率" value={avgActualRate} format="percent" description="盘点倒挤的实际食材成本占实收比例（排除口径异常门店）" />
          <MetricCard title="高偏差门店(>20%)" value={highVarianceCount} format="number" description="实际成本率超理论20%以上的门店数（排除口径异常）" />
          <MetricCard title="口径异常门店" value={abnormalRows.length} format="number" description="理论成本数据缺失或口径异常，不纳入成本率统计" />
        </div>
        {abnormalRows.length > 0 && (
          <div className="mt-3 rounded-md border border-gray-200 bg-gray-50/50 p-3">
            <p className="text-xs text-muted-foreground">
              <span className="font-medium text-gray-700">口径异常门店（{abnormalRows.length}家）：</span>
              {abnormalRows.map((r: any) => r.store_name).join('、')}
              — 理论成本数据缺失或口径不一致，需先修复数据再纳入对比
            </p>
          </div>
        )}
      </CollapsibleSection>

      {/* 成本散点图 */}
      <CollapsibleSection title="理论 vs 实际成本率" subtitle="每个点代表一家门店，偏离对角线越多成本偏差越大">
        <ResponsiveContainer width="100%" height={300}>
          <ScatterChart margin={{ top: 20, right: 20, bottom: 20, left: 20 }}>
            <CartesianGrid strokeDasharray="3 3" />
            <XAxis type="number" dataKey="theoretical" name="理论成本率" unit="%" tick={{ fontSize: 10 }} domain={scatterDomain.x} />
            <YAxis type="number" dataKey="actual" name="实际成本率" unit="%" tick={{ fontSize: 10 }} domain={scatterDomain.y} />
            <ReferenceLine segment={[{ x: scatterDomain.x[0], y: scatterDomain.y[0] }, { x: scatterDomain.x[1], y: scatterDomain.y[1] }]} stroke="#94a3b8" strokeDasharray="4 4" />
            <Tooltip
              cursor={{ strokeDasharray: '3 3' }}
              content={({ active, payload }: any) => {
                if (active && payload && payload.length) {
                  const d = payload[0].payload
                  return (
                    <div className="rounded border bg-card p-2 text-xs shadow-sm">
                      <p className="font-medium">{d.name}</p>
                      <p>理论: {d.theoretical.toFixed(1)}%</p>
                      <p>实际: {d.actual.toFixed(1)}%</p>
                      <p>偏差: {d.variance > 0 ? '+' : ''}{d.variance.toFixed(1)}%</p>
                    </div>
                  )
                }
                return null
              }}
            />
            <Scatter data={scatterData} fill="#3b82f6" />
          </ScatterChart>
        </ResponsiveContainer>
      </CollapsibleSection>

      {/* 成本对比表 */}
      <CollapsibleSection title={`门店成本对比 (${costRows.length})`} subtitle="理论成本 vs 实际倒挤成本，按偏差降序">
        <FilterableTable
          data={costRows}
          sortOptions={[
            { key: 'variance_to_theoretical_pct', label: '偏差' },
            { key: 'theoretical_cost_rate_pct', label: '理论成本率' },
            { key: 'actual_food_cost_rate_pct', label: '实际成本率' },
            { key: 'actual_food_cost', label: '实际食材成本' },
            { key: 'sales_received', label: '销售额' },
          ]}
          defaultSort="variance_to_theoretical_pct"
          defaultOrder="desc"
          filterKey="variance_level"
          filterLabel="全部等级"
          columns={[
            { key: 'store_name', label: '门店' },
            { key: 'theoretical_cost_rate_pct', label: '理论成本率', align: 'right', render: (r) => formatPercent(r.theoretical_cost_rate_pct) },
            { key: 'actual_food_cost_rate_pct', label: '实际成本率', align: 'right', render: (r) => formatPercent(r.actual_food_cost_rate_pct) },
            { key: 'variance_to_theoretical_pct', label: '偏差', align: 'right', render: (r) => {
              const v = Number(r.variance_to_theoretical_pct || 0)
              return <span className={v > 20 ? 'font-medium text-red-600' : v > 10 ? 'text-yellow-600' : 'text-green-600'}>{v > 0 ? '+' : ''}{v.toFixed(1)}%</span>
            }},
            { key: 'variance_level', label: '偏差等级', render: (r) => (
              <span className={`rounded px-2 py-0.5 text-xs font-medium ${
                r.variance_level === '高偏差' ? 'bg-red-100 text-red-700' :
                r.variance_level === '中偏差' ? 'bg-yellow-100 text-yellow-700' :
                'bg-green-100 text-green-700'
              }`}>{r.variance_level || '-'}</span>
            )},
            { key: 'actual_food_cost', label: '实际食材成本', align: 'right', render: (r) => formatCurrency(r.actual_food_cost) },
          ]}
        />
      </CollapsibleSection>

      {/* 库存效率 */}
      <CollapsibleSection title="库存效率" subtitle="库存天数、负耗用、异常货品" defaultOpen={false} onToggle={setInvOpen}>
        {invLoading ? <LoadingSpinner text="加载库存数据..." /> : <>
        <FilterableTable
          data={invRows}
          sortOptions={[
            { key: 'ending_inventory_amount', label: '期末库存' },
            { key: 'negative_item_lines', label: '负耗用行数' },
            { key: 'store_name', label: '门店' },
          ]}
          defaultSort="ending_inventory_amount"
          defaultOrder="desc"
          filterKey="business_type"
          filterLabel="全部业态"
          columns={[
            { key: 'store_name', label: '门店' },
            { key: 'business_type', label: '业态' },
            { key: 'scale_tier', label: '规模' },
            { key: 'estimated_inventory_days', label: '库存天数', align: 'right', render: (r) => {
              const d = Number(r.estimated_inventory_days || 0)
              return <span className={d > 7 ? 'font-medium text-red-600' : d > 4 ? 'text-yellow-600' : 'text-green-600'}>{d.toFixed(1)}天</span>
            }},
            { key: 'ending_inventory_amount', label: '期末库存', align: 'right', render: (r) => formatCurrency(r.ending_inventory_amount) },
            { key: 'negative_item_lines', label: '负耗用行数', align: 'center', render: (r) => {
              const n = Number(r.negative_item_lines || 0)
              return n > 0 ? <span className="font-medium text-red-600">{n}</span> : <span className="text-muted-foreground">0</span>
            }},
          ]}
        />
        </>}
      </CollapsibleSection>

      {/* 原料分类差异 */}
      <CollapsibleSection title="原料分类成本基准" subtitle="按财务分类对比同业基准" defaultOpen={false} onToggle={setCatOpen}>
        {catLoading ? <LoadingSpinner text="加载分类数据..." /> : <>
        <FilterableTable
          data={catRows}
          sortOptions={[
            { key: 'actual_category_cost', label: '实际成本' },
            { key: 'cost_per_10k_sales', label: '每万元成本' },
            { key: 'excess_vs_peer_per_10k', label: '超出同业' },
            { key: 'store_name', label: '门店' },
          ]}
          defaultSort="excess_vs_peer_per_10k"
          defaultOrder="desc"
          filterKey="finance_category"
          filterLabel="全部分类"
          columns={[
            { key: 'store_name', label: '门店' },
            { key: 'finance_category', label: '财务分类' },
            { key: 'actual_category_cost', label: '实际成本', align: 'right', render: (r) => formatCurrency(r.actual_category_cost) },
            { key: 'cost_per_10k_sales', label: '每万元成本', align: 'right', render: (r) => formatCurrency(r.cost_per_10k_sales) },
            { key: 'peer_median_cost_per_10k', label: '同业中位', align: 'right', render: (r) => formatCurrency(r.peer_median_cost_per_10k) },
            { key: 'excess_vs_peer_per_10k', label: '超出同业', align: 'right', render: (r) => {
              const v = Number(r.excess_vs_peer_per_10k || 0)
              return v > 0 ? <span className="text-red-600">+{formatCurrency(v)}</span> : <span className="text-green-600">{formatCurrency(v)}</span>
            }},
          ]}
        />
        </>}
      </CollapsibleSection>
    </div>
  )
}
