import { api } from '@common/services/api';

// ============================================================
// 인터페이스
// ============================================================

export interface BoardPostItem {
  sn: number;
  categoryCd: string;
  title: string;
  authorId: string;
  authorName: string;
  viewCnt: number;
  pinnedYn: string;
  regDtm: string;
}

export interface BoardPostDetail extends BoardPostItem {
  content: string | null;
  mdfcnDtm: string | null;
}

export interface BoardListResponse {
  items: BoardPostItem[];
  totalCount: number;
  page: number;
  size: number;
}

export interface BoardListParams {
  categoryCd?: string;
  search?: string;
  page?: number;
  size?: number;
}

export interface CreateBoardPostInput {
  categoryCd: string;
  title: string;
  content?: string;
  pinnedYn?: string;
}

export interface UpdateBoardPostInput {
  title?: string;
  content?: string;
  pinnedYn?: string;
}

// ============================================================
// API 함수
// ============================================================

export async function fetchBoardPosts(params?: BoardListParams): Promise<BoardListResponse> {
  const response = await api.get<BoardListResponse>('/board', { params });
  return response.data;
}

export async function fetchBoardPost(sn: number): Promise<BoardPostDetail> {
  const response = await api.get<BoardPostDetail>(`/board/${sn}`);
  return response.data;
}

export async function createBoardPost(input: CreateBoardPostInput): Promise<{ sn: number }> {
  const response = await api.post<{ sn: number }>('/board', input);
  return response.data;
}

export async function updateBoardPost(sn: number, input: UpdateBoardPostInput): Promise<void> {
  await api.put(`/board/${sn}`, input);
}

export async function deleteBoardPost(sn: number): Promise<void> {
  await api.delete(`/board/${sn}`);
}

/** 관리자 전용 삭제 — 소유자 검증 없음 */
export async function adminDeleteBoardPost(sn: number): Promise<void> {
  await api.post('/board/admin-delete', { sn });
}

// ============================================================
// 매뉴얼 API
// ============================================================

export interface ManualItem {
  manualSn: number;
  catgNm: string;
  title: string;
  version: string | null;
  fileTp: string | null;
  fileSz: string | null;
  filePath: string | null;
  authorNm: string | null;
  dwnldCnt: number;
  regDtm: string;
}

export interface CreateManualInput {
  catgNm: string;
  title: string;
  version?: string;
  fileTp?: string;
  fileSz?: string;
  filePath?: string;
  authorNm?: string;
}

export interface UpdateManualInput {
  catgNm?: string;
  title?: string;
  version?: string;
  fileTp?: string;
  fileSz?: string;
  filePath?: string;
}

export async function fetchManuals(params?: {
  category?: string;
  search?: string;
}): Promise<ManualItem[]> {
  const response = await api.get<ManualItem[]>('/board/manual', { params });
  return response.data;
}

export async function createManual(input: CreateManualInput): Promise<{ manualSn: number }> {
  const response = await api.post<{ manualSn: number }>('/board/manual', input);
  return response.data;
}

export async function updateManual(sn: number, input: UpdateManualInput): Promise<void> {
  await api.put(`/board/manual/${sn}`, input);
}

export async function deleteManual(sn: number): Promise<void> {
  await api.delete(`/board/manual/${sn}`);
}

export async function incrementManualDownload(sn: number): Promise<void> {
  await api.post(`/board/manual/${sn}/download`);
}
