'use client'

import { useApp } from '@/components/providers/AppProvider'
import { AddToCartButton } from '@/components/commerce/AddToCartButton'
import type { AgentProduct, CourseAccessState } from '@/types'

interface AgentPricePanelProps {
  agent: AgentProduct
}

function centsToYuan(amount: number) {
  return Math.max(0, Math.round(amount / 100))
}

function normalizeAccessState(value: unknown): CourseAccessState {
  if (value === 'closed' || value === 'free' || value === 'paid') return value
  return 'paid'
}

function accessForLevel(product: AgentProduct, level: 0 | 1 | 2) {
  if (level === 2) return normalizeAccessState(product.seniorAccess)
  if (level === 1) return normalizeAccessState(product.juniorAccess)
  return normalizeAccessState(product.registeredAccess)
}

function requiredMembershipLevel(product: AgentProduct) {
  if (accessForLevel(product, 0) !== 'closed') return 0
  if (accessForLevel(product, 1) !== 'closed') return 1
  return 2
}

function displayPrice(product: AgentProduct, level: 0 | 1 | 2) {
  const access = accessForLevel(product, level)
  if (access === 'closed') return null
  if (access === 'free') return 0
  if (level === 2) return Math.max(0, Math.round(product.seniorPriceAmount || 0))
  if (level === 1) return Math.max(0, Math.round(product.juniorPriceAmount || 0))
  return Math.max(0, Math.round(product.registeredPriceAmount || 0))
}

function priceLabel(product: AgentProduct, level: 0 | 1 | 2) {
  const access = accessForLevel(product, level)
  if (access === 'closed') return '会员专享'
  if (access === 'free') return '免费'
  const amount = centsToYuan(displayPrice(product, level) || 0)
  return amount === 0 ? '免费' : `¥${amount}`
}

export function AgentPricePanel({ agent }: AgentPricePanelProps) {
  const { user } = useApp()
  const level: 0 | 1 | 2 = user?.membershipLevel || 0
  const access = accessForLevel(agent, level)
  const priceAmount = displayPrice(agent, level)
  const label = user ? priceLabel(agent, level) : priceLabel(agent, 0)
  const buttonLabel = user
    ? (priceAmount === 0 ? '免费' : `¥${centsToYuan(priceAmount || 0)}`)
    : priceLabel(agent, 0)
  const itemPrice = user
    ? centsToYuan(priceAmount || 0)
    : centsToYuan(agent.registeredPriceAmount || 0)

  return (
    <div className="rounded-[4px] border border-line bg-white p-6 shadow-[0_18px_44px_rgba(15,23,42,0.07)]">
      <div className="text-xs font-bold text-muted">购买价格</div>
      <div className="mt-2 text-4xl font-extrabold text-brand">{label}</div>
      <p className="mt-4 text-sm leading-7 text-muted">购买后进入智能体配置与交付流程，进度会保留在个人中心。</p>
      <AddToCartButton
        item={{
          id: agent.id,
          type: 'agent',
          title: agent.title,
          price: itemPrice,
          href: `/agents/${agent.slug}`,
          meta: `${label} · 购买后填写部署信息`,
        }}
        requiredMembershipLevel={requiredMembershipLevel(agent)}
        membershipAccess={{
          0: accessForLevel(agent, 0),
          1: accessForLevel(agent, 1),
          2: accessForLevel(agent, 2),
        }}
        membershipPrices={{
          0: centsToYuan(agent.registeredPriceAmount || 0),
          1: centsToYuan(agent.juniorPriceAmount || 0),
          2: centsToYuan(agent.seniorPriceAmount || 0),
        }}
        label={buttonLabel}
        directOrderOnFree
        className={
          access === 'closed'
            ? 'mt-6 h-11 w-full cursor-not-allowed border border-line bg-bg text-soft'
            : access === 'free'
              ? 'mt-6 h-11 w-full border border-line px-5 text-text-body hover:border-brand hover:text-brand'
              : 'mt-6 h-11 w-full bg-brand px-5 text-white hover:bg-brand-dark'
        }
      />
    </div>
  )
}
