import api from "./api";
import type {
  ModuleDto,
  ModuleStatusDto,
  SubscriptionDto,
  EnableModuleRequest,
  UpdateSubscriptionRequest,
  RenewSubscriptionRequest,
} from "../types/module";

/**
 * Service per la gestione dei moduli applicativi
 */
export const moduleService = {
  /**
   * Ottiene tutti i moduli disponibili con stato subscription
   */
  getAll: async (): Promise<ModuleDto[]> => {
    const response = await api.get("/modules");
    return response.data;
  },

  /**
   * Ottiene solo i moduli attivi (per costruzione menu)
   */
  getActive: async (): Promise<ModuleDto[]> => {
    const response = await api.get("/modules/active");
    return response.data;
  },

  /**
   * Ottiene un modulo specifico per codice
   */
  getByCode: async (code: string): Promise<ModuleDto> => {
    const response = await api.get(`/modules/${code}`);
    return response.data;
  },

  /**
   * Verifica se un modulo è abilitato
   */
  isEnabled: async (code: string): Promise<ModuleStatusDto> => {
    const response = await api.get(`/modules/${code}/enabled`);
    return response.data;
  },

  /**
   * Attiva un modulo
   */
  enable: async (code: string, request: EnableModuleRequest): Promise<SubscriptionDto> => {
    const response = await api.put(`/modules/${code}/enable`, request);
    return response.data;
  },

  /**
   * Disattiva un modulo
   */
  disable: async (code: string): Promise<{ message: string }> => {
    const response = await api.put(`/modules/${code}/disable`);
    return response.data;
  },

  /**
   * Ottiene tutte le subscription
   */
  getAllSubscriptions: async (): Promise<SubscriptionDto[]> => {
    const response = await api.get("/modules/subscriptions");
    return response.data;
  },

  /**
   * Aggiorna la subscription di un modulo
   */
  updateSubscription: async (
    code: string,
    request: UpdateSubscriptionRequest
  ): Promise<SubscriptionDto> => {
    const response = await api.put(`/modules/${code}/subscription`, request);
    return response.data;
  },

  /**
   * Rinnova la subscription di un modulo
   */
  renewSubscription: async (
    code: string,
    request?: RenewSubscriptionRequest
  ): Promise<SubscriptionDto> => {
    const response = await api.post(`/modules/${code}/subscription/renew`, request || {});
    return response.data;
  },

  /**
   * Ottiene i moduli in scadenza
   */
  getExpiring: async (daysThreshold: number = 30): Promise<ModuleDto[]> => {
    const response = await api.get("/modules/expiring", {
      params: { daysThreshold },
    });
    return response.data;
  },

  /**
   * Inizializza i moduli di default (admin)
   */
  seedDefault: async (): Promise<{ message: string }> => {
    const response = await api.post("/modules/seed");
    return response.data;
  },

  /**
   * Forza il controllo delle subscription scadute (admin)
   */
  checkExpired: async (): Promise<{ message: string }> => {
    const response = await api.post("/modules/check-expired");
    return response.data;
  },

  /**
   * Invalida la cache dei moduli (admin)
   */
  invalidateCache: async (): Promise<{ message: string }> => {
    const response = await api.post("/modules/invalidate-cache");
    return response.data;
  },
};

export default moduleService;
