'use server';

import { fetchJson } from '@/lib/utils/server';
import { ResultDto } from '@/types/response/common';
import {
	UserProfileDto,
	UserPostsResponse,
	UserCommentsResponse
} from '@/types/account/profile';
import { FeedUserPageResponse } from '@/types/feed/card';
import { FollowListResponse } from '@/types/account/follow';

export async function fetchUserProfile(memberSID: string): Promise<ResultDto<UserProfileDto>> {
	return await fetchJson<UserProfileDto>(`/api/member/${encodeURIComponent(memberSID)}`, {
		method: 'GET',
		headers: { 'Accept': 'application/json' }
	});
}

export async function fetchUserPosts(memberSID: string, page = 1, perPage = 20): Promise<ResultDto<UserPostsResponse>> {
	return await fetchJson<UserPostsResponse>(`/api/member/${encodeURIComponent(memberSID)}/posts?page=${page}&perPage=${perPage}`, {
		method: 'GET',
		headers: { 'Accept': 'application/json' }
	});
}

export async function fetchUserComments(memberSID: string, page = 1, perPage = 20): Promise<ResultDto<UserCommentsResponse>> {
	return await fetchJson<UserCommentsResponse>(`/api/member/${encodeURIComponent(memberSID)}/comments?page=${page}&perPage=${perPage}`, {
		method: 'GET',
		headers: { 'Accept': 'application/json' }
	});
}

export async function fetchUserFeed(memberSID: string, page = 1, perPage = 20): Promise<ResultDto<FeedUserPageResponse>> {
	return await fetchJson<FeedUserPageResponse>(`/api/member/${encodeURIComponent(memberSID)}/feed?page=${page}&perPage=${perPage}`, {
		method: 'GET',
		headers: { 'Accept': 'application/json' }
	});
}

export async function fetchUserFollowers(memberSID: string, page = 1, perPage = 20): Promise<ResultDto<FollowListResponse>> {
	return await fetchJson<FollowListResponse>(`/api/member/${encodeURIComponent(memberSID)}/followers?page=${page}&perPage=${perPage}`, {
		method: 'GET',
		headers: { 'Accept': 'application/json' }
	});
}

export async function fetchUserFollowing(memberSID: string, page = 1, perPage = 20): Promise<ResultDto<FollowListResponse>> {
	return await fetchJson<FollowListResponse>(`/api/member/${encodeURIComponent(memberSID)}/following?page=${page}&perPage=${perPage}`, {
		method: 'GET',
		headers: { 'Accept': 'application/json' }
	});
}