import { api } from "@/lib/api-client";
import type { FeesPlan, StudentFeesDetail, FeesTerms } from "@/types/fees";
import type { PaginatedResponse, PaginationParams } from "@/types";

export interface FeesPlanFilters extends PaginationParams {
  q?: string;
  state?: string;
  payment_state?: string;
  student_id?: number;
}

export interface RegisterPaymentBody {
  amount?: number;
  journal_id?: number;
  payment_date?: string;
  memo?: string;
}

export const feesService = {
  async listFeesPlans(params?: FeesPlanFilters): Promise<PaginatedResponse<FeesPlan>> {
    return api.get<PaginatedResponse<FeesPlan>>(
      "/fees-plans",
      params as Record<string, string | number | boolean | undefined>,
    );
  },

  async getFeesPlan(id: number): Promise<FeesPlan> {
    return api.get<FeesPlan>(`/fees-plans/${id}`);
  },

  async createInvoice(id: number): Promise<FeesPlan> {
    return api.post<FeesPlan>(`/fees-plans/${id}/create-invoice`, {});
  },

  async registerPayment(id: number, body: RegisterPaymentBody = {}): Promise<FeesPlan> {
    return api.post<FeesPlan>(`/fees-plans/${id}/register-payment`, body);
  },

  async listStudentFees(params?: FeesPlanFilters): Promise<PaginatedResponse<StudentFeesDetail>> {
    return api.get<PaginatedResponse<StudentFeesDetail>>(
      "/student-fees",
      params as Record<string, string | number | boolean | undefined>,
    );
  },

  async listFeesTerms(params?: PaginationParams): Promise<PaginatedResponse<FeesTerms>> {
    return api.get<PaginatedResponse<FeesTerms>>(
      "/fees-terms",
      params as Record<string, string | number | boolean | undefined>,
    );
  },
};
