package com.backendsys.modules.system.controller;

import com.backendsys.modules.common.config.security.annotations.Anonymous;
import com.backendsys.modules.common.utils.Result;
import com.backendsys.modules.system.entity.SysAgreement;
import com.backendsys.modules.system.service.SysAgreementService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@Validated
@RestController
@Tag(name = "协议管理")
public class SysAgreementController {

    @Autowired
    private SysAgreementService sysAgreementService;

    @Anonymous
    @Operation(summary = "获得协议详情 (HTML)")
    @GetMapping("/api/system/getAgreementToHtml")
    public String getAgreementHtml(HttpServletResponse response, @RequestParam(name = "tag", required = true) String tag) {

        SysAgreement sysAgreement = sysAgreementService.selectAgreementByTag(tag);
        if (sysAgreement == null) return "找不到内容";

        // 设置响应的类型为 text/html，这样浏览器就知道接收到的是 HTML 内容
        response.setContentType("text/html;charset=UTF-8");
        return "<html>" +
                "<head><title>" + sysAgreement.getTitle() + "</title></head>" +
                "<body>" + sysAgreement.getContent() + "</body>" +
                "</html>";
    }

    @Anonymous
    @Operation(summary = "获得协议列表")
    @GetMapping("/api/system/getAgreementList")
    public Result getAgreementList(SysAgreement sysAgreement) {
        return Result.success().put("data", sysAgreementService.selectAgreementList(sysAgreement));
    }

    @Anonymous
    @Operation(summary = "获得协议详情")
    @GetMapping("/api/system/getAgreementByTag")
    public Result getAgreementByTag(String tag) {
        return Result.success().put("data", sysAgreementService.selectAgreementByTag(tag));
    }

}
