import { apiClient } from "@/lib/api/client";

export interface SystemNotice {
  id: number;
  company_code: string;
  title: string;
  content: string;
  is_active: boolean;
  priority: number;
  created_by: string;
  created_at: string;
  updated_at: string;
}

export interface CreateSystemNoticePayload {
  title: string;
  content: string;
  is_active: boolean;
  priority: number;
}

export interface UpdateSystemNoticePayload {
  title?: string;
  content?: string;
  is_active?: boolean;
  priority?: number;
}

interface ApiResponse<T> {
  success: boolean;
  data?: T;
  message?: string;
}

// 공지사항 목록 조회
export async function getSystemNotices(): Promise<ApiResponse<SystemNotice[]>> {
  try {
    const response = await apiClient.get("/system-notices");
    return response.data;
  } catch (error: any) {
    return {
      success: false,
      message: error.response?.data?.message || error.message || "공지사항 목록 조회 실패",
    };
  }
}

// 공지사항 등록
export async function createSystemNotice(
  payload: CreateSystemNoticePayload
): Promise<ApiResponse<SystemNotice>> {
  try {
    const response = await apiClient.post("/system-notices", payload);
    return response.data;
  } catch (error: any) {
    return {
      success: false,
      message: error.response?.data?.message || error.message || "공지사항 등록 실패",
    };
  }
}

// 공지사항 수정
export async function updateSystemNotice(
  id: number,
  payload: UpdateSystemNoticePayload
): Promise<ApiResponse<SystemNotice>> {
  try {
    const response = await apiClient.put(`/system-notices/${id}`, payload);
    return response.data;
  } catch (error: any) {
    return {
      success: false,
      message: error.response?.data?.message || error.message || "공지사항 수정 실패",
    };
  }
}

// 공지사항 삭제
export async function deleteSystemNotice(id: number): Promise<ApiResponse<void>> {
  try {
    const response = await apiClient.delete(`/system-notices/${id}`);
    return response.data;
  } catch (error: any) {
    return {
      success: false,
      message: error.response?.data?.message || error.message || "공지사항 삭제 실패",
    };
  }
}
