package com.qqchen.deploy.backend.notification.api;

import com.qqchen.deploy.backend.framework.api.Response;
import com.qqchen.deploy.backend.framework.controller.BaseController;
import com.qqchen.deploy.backend.notification.dto.NotificationChannelDTO;
import com.qqchen.deploy.backend.notification.dto.NotificationChannelQuery;
import com.qqchen.deploy.backend.notification.entity.NotificationChannel;
import com.qqchen.deploy.backend.notification.enums.NotificationChannelTypeEnum;
import com.qqchen.deploy.backend.notification.service.INotificationChannelService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 通知渠道API控制器
 *
 * @author qqchen
 * @since 2025-10-22
 */
@Slf4j
@RestController
@RequestMapping("/api/v1/notification-channel")
@Tag(name = "通知渠道管理", description = "通知渠道管理相关接口")
public class NotificationChannelApiController 
    extends BaseController<NotificationChannel, NotificationChannelDTO, Long, NotificationChannelQuery> {
    
    @Resource
    private INotificationChannelService notificationChannelService;
    
    @Operation(summary = "获取所有渠道类型")
    @GetMapping("/types")
    public Response<List<Map<String, Object>>> getChannelTypes() {
        List<Map<String, Object>> types = Arrays.stream(NotificationChannelTypeEnum.values())
            .map(type -> {
                Map<String, Object> map = new HashMap<>();
                map.put("code", type.name());
                map.put("label", type.getLabel());
                map.put("description", type.getDescription());
                return map;
            })
            .collect(Collectors.toList());
        
        return Response.success(types);
    }
    
    @Operation(summary = "测试通知渠道连接")
    @PostMapping("/{id}/test")
    public Response<Boolean> testConnection(
        @Parameter(description = "渠道ID", required = true) @PathVariable Long id
    ) {
        boolean result = notificationChannelService.testConnection(id);
        return Response.success(result);
    }
    
    @Operation(summary = "启用通知渠道")
    @PostMapping("/{id}/enable")
    public Response<Void> enable(
        @Parameter(description = "渠道ID", required = true) @PathVariable Long id
    ) {
        notificationChannelService.enable(id);
        return Response.success();
    }
    
    @Operation(summary = "禁用通知渠道")
    @PostMapping("/{id}/disable")
    public Response<Void> disable(
        @Parameter(description = "渠道ID", required = true) @PathVariable Long id
    ) {
        notificationChannelService.disable(id);
        return Response.success();
    }
    
    @Override
    protected void exportData(HttpServletResponse response, List<NotificationChannelDTO> data) {
        // TODO: 实现导出功能
    }
}

