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.NotificationTemplateDTO;
import com.qqchen.deploy.backend.notification.dto.template.NotificationTemplateQuery;
import com.qqchen.deploy.backend.notification.dto.template.TemplateRenderRequest;
import com.qqchen.deploy.backend.notification.entity.NotificationTemplate;
import com.qqchen.deploy.backend.notification.service.INotificationTemplateService;
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 jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 通知模板API控制器
 *
 * @author qqchen
 * @since 2025-11-12
 */
@Slf4j
@RestController
@RequestMapping("/api/v1/notification-template")
@Tag(name = "通知模板管理", description = "通知模板的增删改查、渲染预览、发送测试等功能")
@Validated
public class NotificationTemplateApiController extends BaseController<NotificationTemplate, NotificationTemplateDTO, Long, NotificationTemplateQuery> {

    @Resource
    private INotificationTemplateService notificationTemplateService;



    @Operation(summary = "根据编码获取模板")
    @GetMapping("/code/{code}")
    public Response<NotificationTemplateDTO> getByCode(@Parameter(description = "模板编码", required = true) @PathVariable String code) {
        NotificationTemplateDTO template = notificationTemplateService.getByCode(code);
        return Response.success(template);
    }

    @Operation(summary = "渲染模板预览")
    @PostMapping("/render")
    public Response<String> renderTemplate(@Valid @RequestBody TemplateRenderRequest request) {
        String result = notificationTemplateService.renderTemplate(request.getTemplateContext(), request.getParams());
        return Response.success(result);
    }

    @Operation(summary = "启用通知模板")
    @PutMapping("/{id}/enable")
    public Response<Void> enable(@Parameter(description = "模板ID", required = true) @PathVariable Long id) {
        notificationTemplateService.enable(id);
        return Response.success();
    }

    @Operation(summary = "禁用通知模板")
    @PutMapping("/{id}/disable")
    public Response<Void> disable(@Parameter(description = "模板ID", required = true) @PathVariable Long id) {
        notificationTemplateService.disable(id);
        return Response.success();
    }

    @Override
    protected void exportData(HttpServletResponse response, List<NotificationTemplateDTO> data) {
        // TODO: 实现导出功能
    }
}
