import { apiClient } from '../api/api-client';
import type { AlertRuleRepository } from '@/domain/repositories/alert-rule.repository';
import type { AlertRule, AlertEvent } from '@/domain/entities/alert-rule';

export const apiAlertRuleRepository: AlertRuleRepository = {
  async listRules() {
    return apiClient<AlertRule[]>('/api/v1/monitor/alerts/rules');
  },

  async getRuleById(id) {
    return apiClient<AlertRule>(`/api/v1/monitor/alerts/rules/${id}`);
  },

  async createRule(rule) {
    return apiClient<AlertRule>('/api/v1/monitor/alerts/rules', {
      method: 'POST',
      body: rule,
    });
  },

  async updateRule(id, data) {
    return apiClient<AlertRule>(`/api/v1/monitor/alerts/rules/${id}`, {
      method: 'PUT',
      body: data,
    });
  },

  async removeRule(id) {
    await apiClient(`/api/v1/monitor/alerts/rules/${id}`, {
      method: 'DELETE',
    });
  },

  async toggleRule(id, enabled) {
    await apiClient(`/api/v1/monitor/alerts/rules/${id}/toggle`, {
      method: 'PATCH',
      body: { enabled },
    });
  },

  async listEvents(params) {
    const query = params ? '?' + new URLSearchParams(params).toString() : '';
    return apiClient<AlertEvent[]>(`/api/v1/monitor/alerts/events${query}`);
  },

  async acknowledgeEvent(id) {
    await apiClient(`/api/v1/monitor/alerts/events/${id}/acknowledge`, {
      method: 'POST',
    });
  },
};
