import { api } from "@/lib/api-client";
import type {
  DiscussionBoard,
  DiscussionPost,
  Announcement,
  Message,
  PostCreateRequest,
  AnnouncementCreateRequest,
  MessageCreateRequest,
} from "@/types/communication";
import type { ApiSuccessResponse, PaginatedResponse, PaginationParams } from "@/types";

export const communicationService = {
  async listDiscussionBoards(params?: { course_id?: number; batch_id?: number }): Promise<DiscussionBoard[]> {
    return api.get<DiscussionBoard[]>("/discussion-boards", params as Record<string, string | number | boolean | undefined>);
  },

  async createDiscussionBoard(data: Partial<DiscussionBoard> & Pick<DiscussionBoard, "name" | "course_id">): Promise<DiscussionBoard> {
    return api.post<DiscussionBoard>("/discussion-boards", data);
  },

  async updateDiscussionBoard(id: number, data: Partial<DiscussionBoard>): Promise<DiscussionBoard> {
    return api.patch<DiscussionBoard>(`/discussion-boards/${id}`, data);
  },

  async listPosts(boardId: number, params?: PaginationParams): Promise<PaginatedResponse<DiscussionPost>> {
    return api.get<PaginatedResponse<DiscussionPost>>(
      `/discussion-boards/${boardId}/posts`,
      params as Record<string, string | number | boolean | undefined>,
    );
  },

  async createPost(boardId: number, data: PostCreateRequest): Promise<DiscussionPost> {
    return api.post<DiscussionPost>(`/discussion-boards/${boardId}/posts`, data);
  },

  async updatePost(id: number, data: Partial<PostCreateRequest>): Promise<DiscussionPost> {
    return api.patch<DiscussionPost>(`/posts/${id}`, data);
  },

  async deletePost(id: number): Promise<ApiSuccessResponse> {
    return api.delete<ApiSuccessResponse>(`/posts/${id}`);
  },

  async pinPost(id: number, body?: { pinned?: boolean }): Promise<DiscussionPost> {
    return api.post<DiscussionPost>(`/posts/${id}/pin`, body);
  },

  async resolvePost(id: number): Promise<DiscussionPost> {
    return api.post<DiscussionPost>(`/posts/${id}/resolve`);
  },

  async listAnnouncements(params?: { course_id?: number; priority?: string }): Promise<Announcement[]> {
    return api.get<Announcement[]>("/announcements", params as Record<string, string | number | boolean | undefined>);
  },

  async createAnnouncement(data: AnnouncementCreateRequest): Promise<Announcement> {
    return api.post<Announcement>("/announcements", data);
  },

  async updateAnnouncement(id: number, data: Partial<AnnouncementCreateRequest>): Promise<Announcement> {
    return api.patch<Announcement>(`/announcements/${id}`, data);
  },

  async deleteAnnouncement(id: number): Promise<ApiSuccessResponse> {
    return api.delete<ApiSuccessResponse>(`/announcements/${id}`);
  },

  async publishAnnouncement(id: number): Promise<Announcement> {
    return api.post<Announcement>(`/announcements/${id}/publish`);
  },

  async listMessages(params?: { is_read?: boolean } & PaginationParams): Promise<PaginatedResponse<Message>> {
    return api.get<PaginatedResponse<Message>>("/messages", params as Record<string, string | number | boolean | undefined>);
  },

  async listSentMessages(params?: PaginationParams): Promise<PaginatedResponse<Message>> {
    return api.get<PaginatedResponse<Message>>("/messages/sent", params as Record<string, string | number | boolean | undefined>);
  },

  async sendMessage(data: MessageCreateRequest): Promise<Message> {
    return api.post<Message>("/messages", data);
  },

  async getMessage(id: number): Promise<Message> {
    return api.get<Message>(`/messages/${id}`);
  },

  async deleteMessage(id: number): Promise<ApiSuccessResponse> {
    return api.delete<ApiSuccessResponse>(`/messages/${id}`);
  },

  async getUnreadMessageCount(): Promise<{ count: number }> {
    return api.get<{ count: number }>("/messages/unread-count");
  },
};
