'use client';

import { create } from 'zustand';

/**
 * 工作台跨栏同步 — 详情页(右)动作改了 plan 状态后,通知左栏选患者列原地更新。
 * 极简事件总线:seq 自增触发订阅;消费方按 planId patch 对应行(不重拉、不丢滚动)。
 */
interface PlanSyncState {
  seq: number;
  planId: string | null;
  /** 新状态(submitExecution 回的 planStatus 等);null = 只提示"该行变了"(消费方自行 refresh) */
  status: string | null;
  notify: (planId: string, status?: string) => void;
  /** 当前工作台正在看的患者(详情页 ready 时发布;右下角助手按它出场景化开场建议) */
  current: { planId: string; patientName: string } | null;
  setCurrent: (current: { planId: string; patientName: string } | null) => void;
}

export const usePlanSyncStore = create<PlanSyncState>((set) => ({
  seq: 0,
  planId: null,
  status: null,
  notify: (planId, status) =>
    set((s) => ({ seq: s.seq + 1, planId, status: status ?? null })),
  current: null,
  setCurrent: (current) => set({ current }),
}));
