import { useViewData } from '@byted-doubao-apps/framework';
import { navigateTo } from '@byted-doubao-apps/framework/api';
import { ContentCard, type ContentCardItem } from '@byted-doubao-apps/template';
import { saveShoppingPlan, type ShoppingPlanData } from '../../shared/shopping-plan';
import { formatShoppingPlanSummary } from '../../shared/shopping-plan-summary';

export default function ShoppingPlanCard() {
  const plan = useViewData<ShoppingPlanData>() ?? {};
  const items: ContentCardItem[] = [
    {
      key: plan.plan_id || 'shopping-plan',
      title: plan.title || '购物方案',
      subtitle: formatShoppingPlanSummary(plan)
    },
    ...(plan.groups ?? []).slice(0, 2).map((group, index) => ({
      key: group.name || `group-${index}`,
      title: group.name || '其他',
      subtitle: `${group.item_count || 0} 件 · ¥${group.subtotal || 0}`
    }))
  ];

  const openDetail = () => {
    saveShoppingPlan(plan);
    navigateTo({ url: 'pages/home/index' });
  };

  return (
    <ContentCard items={items} footer={{ primaryActionButton: { text: '查看方案', onClick: openDetail } }} />
  );
}
