/*
 * 인증 관람객 개인화 API — 멤버십(F-C6)·최근본 피드(F-C7)·컨택트 지갑(F-C4)·저장 결제수단(F-B5)·
 * 세션 RSVP(F-B4/C5)·인앱 알림(F-C5). 공용 client(api)만 사용 — JWT 자동 첨부.
 * ★ PII 불변: 응답은 서버 마스킹 필드만(원문 없음).
 */
import { api } from '../../api/client';

// ── F-C6 멤버십 ──
export interface Membership {
  tier: 'GENERAL' | 'BUYER' | 'VIP';
  tierLabel: string;
  visitCount: number;
  buyerVerified: boolean;
  vipGrant: boolean;
  points: number;
  lastVisitAt: string | null;
  visitsToNextTier: number;
  benefits: string[];
}

// ── F-C7 피드 ──
export interface FeedItem {
  id: string;
  itemType: 'EVENT' | 'BOOTH';
  itemId: string;
  itemTitle: string;
  itemSub: string | null;
  viewedAt: string;
}

// ── F-C4 컨택트 ──
export interface MyCard {
  displayName: string;
  company: string | null;
  title: string | null;
  emailMasked: string | null;
  phoneMasked: string | null;
  shareToken: string;
  qrPayload: string;
}
export interface Contact {
  id: string;
  displayName: string;
  company: string | null;
  title: string | null;
  emailMasked: string | null;
  phoneMasked: string | null;
  memo: string | null;
  savedAt: string | null;
}

// ── F-B5 결제수단 ──
export interface PayMethod {
  id: string;
  kind: string;
  brand: string | null;
  last4: string | null;
  isDefault: boolean;
  label: string | null;
  createdAt: string | null;
}

// ── F-B4/C5 세션 ──
export interface SessionItem {
  id: string;
  title: string;
  speaker: string | null;
  room: string | null;
  track: string | null;
  startsAt: string | null;
  endsAt: string | null;
  capacity: number;
  seatTaken: number;
  remaining: number | null;
  full: boolean;
  status: string;
  myStatus: 'CONFIRMED' | 'WAITLIST' | null;
}
export interface RsvpResult {
  sessionId: string;
  status: 'CONFIRMED' | 'WAITLIST' | 'CANCELLED';
  waitlistPos: number | null;
  message: string;
}
export interface MyRsvp {
  sessionId: string;
  title: string;
  room: string | null;
  track: string | null;
  startsAt: string | null;
  status: string;
  waitlistPos: number | null;
}

// ── F-C5 알림 ──
export interface VisitorNotification {
  id: string;
  kind: string;
  title: string;
  body: string | null;
  refType: string | null;
  refId: string | null;
  read: boolean;
  createdAt: string;
}

export const accountApi = {
  // 멤버십
  membership: () => api.get<Membership>('/api/visitor/membership'),
  buyerVerify: (verified: boolean) =>
    api.post<Membership>(`/api/visitor/membership/buyer-verify?verified=${verified}`),

  // 피드
  feed: (limit = 20) => api.get<FeedItem[]>(`/api/visitor/feed/recent?limit=${limit}`),
  recordView: (payload: { itemType: string; itemId: string; itemTitle: string; itemSub?: string }) =>
    api.post<void>('/api/visitor/feed/view', payload),

  // 컨택트
  myCard: () => api.get<MyCard>('/api/visitor/contacts/me/card'),
  saveCard: (payload: { displayName: string; company?: string; title?: string; email?: string; phone?: string }) =>
    api.put<MyCard>('/api/visitor/contacts/me/card', payload),
  exchange: (qrPayload: string) => api.post<Contact>('/api/visitor/contacts/exchange', { qrPayload }),
  contacts: () => api.get<Contact[]>('/api/visitor/contacts'),
  deleteContact: (id: string) => api.del<void>(`/api/visitor/contacts/${encodeURIComponent(id)}`),

  // 결제수단
  payMethods: () => api.get<PayMethod[]>('/api/visitor/pay-methods'),
  registerPayMethod: (payload: { kind: string; brand?: string; cardNumber?: string; label?: string; makeDefault: boolean }) =>
    api.post<PayMethod>('/api/visitor/pay-methods', payload),
  setDefaultPayMethod: (id: string) => api.post<void>(`/api/visitor/pay-methods/${encodeURIComponent(id)}/default`),
  deletePayMethod: (id: string) => api.del<void>(`/api/visitor/pay-methods/${encodeURIComponent(id)}`),

  // 세션 RSVP
  sessions: (eventId: string) => api.get<SessionItem[]>(`/api/visitor/events/${encodeURIComponent(eventId)}/sessions`),
  rsvp: (sessionId: string) => api.post<RsvpResult>(`/api/visitor/sessions/${encodeURIComponent(sessionId)}/rsvp`),
  cancelRsvp: (sessionId: string) => api.del<RsvpResult>(`/api/visitor/sessions/${encodeURIComponent(sessionId)}/rsvp`),
  myRsvps: () => api.get<MyRsvp[]>('/api/visitor/sessions/mine'),

  // 알림
  notifications: () => api.get<VisitorNotification[]>('/api/visitor/notifications'),
  markNotificationRead: (id: string) => api.post<void>(`/api/visitor/notifications/${encodeURIComponent(id)}/read`),
  markAllNotificationsRead: () => api.post<void>('/api/visitor/notifications/read-all'),
};
