'use server';

import {
    PostResponse
} from '@/types/response/forum/post';

import { ResultDto } from '@/types/response/common';
import { fetchJson } from '@/lib/utils/server';

// 게시글 정보 조회
export async function fetchPostData(postID: number): Promise<ResultDto<PostResponse>> {
    const post = await fetchJson<PostResponse>(`/api/forum/posts/${postID}`, {
        method: 'GET',
        headers: {
            'Accept': 'application/json'
        }
    });

    if (post.success && post.data) {
        // 이미지 URL 변경
        post.data.content = post.data.content.replace(/<img\b[^>]*?\bsrc=["']?(\/[^"'\s>]*)["']?/gi, (match, src) => {
            return match.replace(src, process.env.API_URL + src);
        });
    }

    return post;
}