/**
 * 系统维护管理服务
 * 负责系统维护配置的API调用
 */

import apiClient from '@/infrastructure/api/client';
import { API_ENDPOINTS } from '@/infrastructure/api/endpoints';

/** 维护配置项 */
export interface MaintenanceItem {
  id: string;
  title: string;
  message: string;
  startTime: string;
  endTime: string;
  isActive: boolean;
  createdAt: string;
  updatedAt: string;
  createdBy: string;
  updatedBy: string | null;
}

/** 创建维护配置请求 */
export interface CreateMaintenanceRequest {
  title: string;
  message: string;
  startTime: string;
  endTime: string;
}

/** 更新维护配置请求 */
export interface UpdateMaintenanceRequest {
  title?: string;
  message?: string;
  startTime?: string;
  endTime?: string;
}

/** 维护列表响应 */
export interface MaintenanceListResponse {
  items: MaintenanceItem[];
  total: number;
}

/** 当前维护状态响应 */
export interface MaintenanceStatusResponse {
  isUnderMaintenance: boolean;
  maintenance: {
    title: string;
    message: string;
    startTime: string;
    endTime: string;
    remainingMinutes: number;
  } | null;
}

/**
 * 系统维护管理服务
 */
export const maintenanceService = {
  /**
   * 获取维护配置列表
   */
  async getMaintenanceList(): Promise<MaintenanceListResponse> {
    return apiClient.get(API_ENDPOINTS.MAINTENANCE.LIST);
  },

  /**
   * 获取维护配置详情
   */
  async getMaintenance(id: string): Promise<MaintenanceItem> {
    return apiClient.get(API_ENDPOINTS.MAINTENANCE.DETAIL(id));
  },

  /**
   * 创建维护配置
   */
  async createMaintenance(data: CreateMaintenanceRequest): Promise<MaintenanceItem> {
    return apiClient.post(API_ENDPOINTS.MAINTENANCE.CREATE, data);
  },

  /**
   * 更新维护配置
   */
  async updateMaintenance(id: string, data: UpdateMaintenanceRequest): Promise<MaintenanceItem> {
    return apiClient.put(API_ENDPOINTS.MAINTENANCE.UPDATE(id), data);
  },

  /**
   * 切换维护配置激活状态
   */
  async toggleMaintenance(id: string, isActive: boolean): Promise<MaintenanceItem> {
    return apiClient.put(API_ENDPOINTS.MAINTENANCE.TOGGLE(id), { isActive });
  },

  /**
   * 删除维护配置
   */
  async deleteMaintenance(id: string): Promise<void> {
    return apiClient.delete(API_ENDPOINTS.MAINTENANCE.DELETE(id));
  },

  /**
   * 获取当前维护状态
   */
  async getCurrentStatus(): Promise<MaintenanceStatusResponse> {
    return apiClient.get(API_ENDPOINTS.MAINTENANCE.CURRENT_STATUS);
  },
};

export default maintenanceService;
