'use server';


import { executeServerAction, type ActionResult } from '@/lib/api/execute-server-action';
import { buildApiUrl } from '@/lib/api/query-params';
import type {
  PostApiData,
  PostPaginationResponse,
  PostFilters,
  CommentApiData,
  CommentsApiResponse,
} from '@/components/customer-center/shared/types';

// ===== 게시글 API =====

export async function getDynamicBoardPosts(
  boardCode: string, filters?: PostFilters
): Promise<ActionResult<PostPaginationResponse>> {
  return executeServerAction<PostPaginationResponse>({
    url: buildApiUrl(`/api/v1/boards/${boardCode}/posts`, {
      search: filters?.search,
      is_notice: filters?.is_notice,
      status: filters?.status,
      per_page: filters?.per_page,
      page: filters?.page,
    }),
    errorMessage: '게시글 목록 조회에 실패했습니다.',
  });
}

export async function getDynamicBoardPost(
  boardCode: string, postId: number | string
): Promise<ActionResult<PostApiData>> {
  return executeServerAction<PostApiData>({
    url: buildApiUrl(`/api/v1/boards/${boardCode}/posts/${postId}`),
    errorMessage: '게시글을 찾을 수 없습니다.',
  });
}

export async function createDynamicBoardPost(
  boardCode: string,
  data: { title: string; content: string; is_secret?: boolean; is_notice?: boolean; custom_fields?: Record<string, string> }
): Promise<ActionResult<PostApiData>> {
  return executeServerAction<PostApiData>({
    url: buildApiUrl(`/api/v1/boards/${boardCode}/posts`),
    method: 'POST',
    body: data,
    errorMessage: '게시글 등록에 실패했습니다.',
  });
}

export async function updateDynamicBoardPost(
  boardCode: string, postId: number | string,
  data: { title?: string; content?: string; is_secret?: boolean; is_notice?: boolean; custom_fields?: Record<string, string> }
): Promise<ActionResult<PostApiData>> {
  return executeServerAction<PostApiData>({
    url: buildApiUrl(`/api/v1/boards/${boardCode}/posts/${postId}`),
    method: 'PUT',
    body: data,
    errorMessage: '게시글 수정에 실패했습니다.',
  });
}

export async function deleteDynamicBoardPost(
  boardCode: string, postId: number | string
): Promise<ActionResult> {
  return executeServerAction({
    url: buildApiUrl(`/api/v1/boards/${boardCode}/posts/${postId}`),
    method: 'DELETE',
    errorMessage: '게시글 삭제에 실패했습니다.',
  });
}

// ===== 댓글 API =====

export async function getDynamicBoardComments(
  boardCode: string, postId: number | string
): Promise<ActionResult<CommentsApiResponse>> {
  return executeServerAction<CommentsApiResponse>({
    url: buildApiUrl(`/api/v1/boards/${boardCode}/posts/${postId}/comments`),
    errorMessage: '댓글 목록 조회에 실패했습니다.',
  });
}

export async function createDynamicBoardComment(
  boardCode: string, postId: number | string, content: string
): Promise<ActionResult<CommentApiData>> {
  return executeServerAction<CommentApiData>({
    url: buildApiUrl(`/api/v1/boards/${boardCode}/posts/${postId}/comments`),
    method: 'POST',
    body: { content },
    errorMessage: '댓글 등록에 실패했습니다.',
  });
}

export async function updateDynamicBoardComment(
  boardCode: string, postId: number | string, commentId: number | string, content: string
): Promise<ActionResult<CommentApiData>> {
  return executeServerAction<CommentApiData>({
    url: buildApiUrl(`/api/v1/boards/${boardCode}/posts/${postId}/comments/${commentId}`),
    method: 'PUT',
    body: { content },
    errorMessage: '댓글 수정에 실패했습니다.',
  });
}

export async function deleteDynamicBoardComment(
  boardCode: string, postId: number | string, commentId: number | string
): Promise<ActionResult> {
  return executeServerAction({
    url: buildApiUrl(`/api/v1/boards/${boardCode}/posts/${postId}/comments/${commentId}`),
    method: 'DELETE',
    errorMessage: '댓글 삭제에 실패했습니다.',
  });
}
