/*
 * F-B6 라이브 공지 조직자 API 계약 — 인증 필요(행사 접근권 · ORGANIZER·HALL_MANAGER·ADMIN).
 * 공용 클라이언트(../../api/client)만 사용. 공개 조회는 publicApi.listLiveNotices.
 * 정본: 백엔드 컨트롤러
 *   - GET  /api/events/{eventId}/live-notices?status=&limit=      → LiveNoticeDto[]
 *   - POST /api/events/{eventId}/live-notices                     → LiveNoticeDto(신규 게시)
 *   - POST /api/events/{eventId}/live-notices/{id}/archive        → LiveNoticeDto(보관)
 *   - POST /api/events/{eventId}/live-notices/{id}/pin  {pinned}  → LiveNoticeDto(고정 토글)
 */
import { api } from '../../api/client';
import type { LiveNoticeCategory, LiveNoticeDto } from '../public/publicApi';

export type { LiveNoticeCategory, LiveNoticeDto };

export interface LiveNoticeCreateRequest {
  category: LiveNoticeCategory;
  title: string;
  body?: string | null;
  pinned?: boolean;
}

function qs(params: Record<string, string | undefined>): string {
  const sp = new URLSearchParams();
  Object.entries(params).forEach(([k, v]) => {
    if (v != null && v !== '') sp.set(k, v);
  });
  const s = sp.toString();
  return s ? `?${s}` : '';
}

export const liveNoticeApi = {
  /** 전체 상태 목록(status 필터 선택: published|archived). */
  list: (eventId: string, status?: string, limit = 100) =>
    api.get<LiveNoticeDto[]>(
      `/api/events/${encodeURIComponent(eventId)}/live-notices${qs({ status, limit: String(limit) })}`,
    ),
  create: (eventId: string, body: LiveNoticeCreateRequest) =>
    api.post<LiveNoticeDto>(`/api/events/${encodeURIComponent(eventId)}/live-notices`, body),
  archive: (eventId: string, id: string) =>
    api.post<LiveNoticeDto>(
      `/api/events/${encodeURIComponent(eventId)}/live-notices/${encodeURIComponent(id)}/archive`,
    ),
  pin: (eventId: string, id: string, pinned: boolean) =>
    api.post<LiveNoticeDto>(
      `/api/events/${encodeURIComponent(eventId)}/live-notices/${encodeURIComponent(id)}/pin`,
      { pinned },
    ),
};
