import { api } from "@/lib/api-client";
import type {
  AcademicYear, AcademicTerm, Department,
  AcademicYearCreateRequest, AcademicTermCreateRequest, DepartmentCreateRequest,
} from "@/types/academic";
import type { PaginatedResponse, PaginationParams, ApiSuccessResponse } from "@/types";

export const academicService = {
  async listAcademicYears(params?: PaginationParams): Promise<PaginatedResponse<AcademicYear>> {
    return api.get<PaginatedResponse<AcademicYear>>("/academic-years", params as Record<string, string | number | boolean | undefined>);
  },

  async getAcademicYear(id: number): Promise<AcademicYear> {
    return api.get<AcademicYear>(`/academic-years/${id}`);
  },

  async createAcademicYear(data: AcademicYearCreateRequest): Promise<AcademicYear> {
    return api.post<AcademicYear>("/academic-years", data);
  },

  async updateAcademicYear(id: number, data: Partial<AcademicYearCreateRequest>): Promise<AcademicYear> {
    return api.patch<AcademicYear>(`/academic-years/${id}`, data);
  },

  async deleteAcademicYear(id: number): Promise<ApiSuccessResponse> {
    return api.delete<ApiSuccessResponse>(`/academic-years/${id}`);
  },

  async generateTerms(yearId: number): Promise<AcademicTerm[]> {
    return api.post<AcademicTerm[]>(`/academic-years/${yearId}/generate-terms`);
  },

  async listTerms(params?: PaginationParams & { year_id?: number }): Promise<PaginatedResponse<AcademicTerm>> {
    return api.get<PaginatedResponse<AcademicTerm>>("/academic-terms", params as Record<string, string | number | boolean | undefined>);
  },

  async createTerm(data: AcademicTermCreateRequest): Promise<AcademicTerm> {
    return api.post<AcademicTerm>("/academic-terms", data);
  },

  async updateTerm(id: number, data: Partial<AcademicTermCreateRequest>): Promise<AcademicTerm> {
    return api.patch<AcademicTerm>(`/academic-terms/${id}`, data);
  },

  async deleteTerm(id: number): Promise<ApiSuccessResponse> {
    return api.delete<ApiSuccessResponse>(`/academic-terms/${id}`);
  },

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

  async createDepartment(data: DepartmentCreateRequest): Promise<Department> {
    return api.post<Department>("/departments", data);
  },

  async updateDepartment(id: number, data: Partial<DepartmentCreateRequest>): Promise<Department> {
    return api.patch<Department>(`/departments/${id}`, data);
  },

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