package com.hotwj.platform.reportStatistics.verify.controller;

import cn.dev33.satoken.annotation.SaIgnore;
import com.hotwj.platform.reportStatistics.verify.domain.HotReportVerifyTicket;
import com.hotwj.platform.reportStatistics.verify.service.ReportVerifyTicketService;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.exception.ServiceException;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@SaIgnore
@RestController
@RequiredArgsConstructor
@RequestMapping("/public/verify/training")
public class ReportVerifyController {

    private final ReportVerifyTicketService reportVerifyTicketService;

    @GetMapping(value = "/monthly", produces = MediaType.TEXT_HTML_VALUE + ";charset=UTF-8")
    public String verifyTrainingMonthly(@RequestParam("ticket") Long ticket) {
        try {
            HotReportVerifyTicket t = reportVerifyTicketService.loadAndValidate(ticket, "TP_MONTHLY");
            return renderHtml(t);
        } catch (ServiceException e) {
            return renderError(e.getMessage());
        }
    }

    @GetMapping(value = "/studyRecord", produces = MediaType.TEXT_HTML_VALUE + ";charset=UTF-8")
    public String verifyStudyRecord(@RequestParam("ticket") Long ticket) {
        try {
            HotReportVerifyTicket t = reportVerifyTicketService.loadAndValidate(ticket, "STUDY_RECORD");
            return renderHtml(t);
        } catch (ServiceException e) {
            return renderError(e.getMessage());
        }
    }

    @GetMapping(value = "/studyDetail", produces = MediaType.TEXT_HTML_VALUE + ";charset=UTF-8")
    public String verifyStudyDetail(@RequestParam("ticket") Long ticket) {
        try {
            HotReportVerifyTicket t = reportVerifyTicketService.loadAndValidate(ticket, "STUDY_DETAIL");
            return renderHtml(t);
        } catch (ServiceException e) {
            return renderError(e.getMessage());
        }
    }

    private String renderHtml(HotReportVerifyTicket ticket) {
        if (ticket == null) {
            throw new ServiceException("验真票据不存在");
        }
        String html = ticket.getSnapshotContent();
        return reportVerifyTicketService.normalizeSnapshotHtmlForMobile(html);
    }

    private String renderError(String message) {
        String msg = message == null ? "验真失败" : message;
        String html = "<!DOCTYPE html><html><head><meta charset=\"UTF-8\"><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">"
                + "<title>验真失败</title></head><body style=\"font-family:Arial,Helvetica,sans-serif;padding:16px;\">"
                + "<div style=\"max-width:720px;margin:0 auto;\">"
                + "<h2 style=\"margin:0 0 12px 0;\">验真失败</h2>"
                + "<div style=\"padding:12px;border:1px solid #ddd;border-radius:6px;background:#fafafa;\">"
                + msg
                + "</div></div></body></html>";
        return reportVerifyTicketService.normalizeSnapshotHtmlForMobile(html);
    }
}
