import { create } from 'zustand';

/**
 * UI state for the listing inquiry modal.
 *
 * Lives in a Zustand store so that:
 *   - any component (e.g. floating CTAs, sticky "Nhắn tin" bars) can open the
 *     modal without prop drilling through the listing detail tree
 *   - tests and devtools can inspect / drive modal state directly
 */
export interface InquiryModalTarget {
  listingId: string;
  listingTitle: string;
  sellerName: string;
}

export interface InquiryModalState {
  /** Whether the inquiry modal is currently open. */
  isOpen: boolean;
  /** The listing being inquired about (null when the modal is closed). */
  target: InquiryModalTarget | null;

  /** Open the modal for a given listing. */
  openInquiry: (target: InquiryModalTarget) => void;
  /** Close the modal and clear the active target. */
  closeInquiry: () => void;
  /** Update open state directly (used by Radix onOpenChange). */
  setOpen: (open: boolean) => void;
}

export const useInquiryStore = create<InquiryModalState>((set) => ({
  isOpen: false,
  target: null,
  openInquiry: (target) => set({ isOpen: true, target }),
  closeInquiry: () => set({ isOpen: false, target: null }),
  setOpen: (open) =>
    set((state) => ({
      isOpen: open,
      target: open ? state.target : null,
    })),
}));
