package cn.yunzhixue.ability.center.examsprint.adapter.http;

import cn.yunzhixue.ability.center.examsprint.application.report.ExamSprintReportApplicationService;
import cn.yunzhixue.ability.center.examsprint.application.report.ExamSprintReportApplicationService.ReportDownloadContent;
import cn.yunzhixue.ability.center.examsprint.application.report.ExamSprintReportApplicationService.ReportHtmlPreviewContent;
import cn.yunzhixue.ability.center.examsprint.contracts.report.CreateExamSprintReportRequest;
import cn.yunzhixue.ability.center.examsprint.contracts.report.CreateExamSprintReportResponse;
import cn.yunzhixue.ability.center.examsprint.contracts.report.ExamSprintReportDetailResponse;
import cn.yunzhixue.ability.center.kernel.BaseResponse;
import jakarta.validation.Valid;
import org.springframework.http.ContentDisposition;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.nio.charset.StandardCharsets;

@RestController
@RequestMapping("/api/exam-sprint/reports")
public class ExamSprintReportController {

    private final ExamSprintReportApplicationService applicationService;

    public ExamSprintReportController(ExamSprintReportApplicationService applicationService) {
        this.applicationService = applicationService;
    }

    @PostMapping
    public ResponseEntity<BaseResponse<CreateExamSprintReportResponse>> createReport(
            @Valid @RequestBody CreateExamSprintReportRequest request) {
        return ResponseEntity.accepted().body(BaseResponse.success(applicationService.createReport(request)));
    }

    @GetMapping("/{reportId}")
    public BaseResponse<ExamSprintReportDetailResponse> getReport(@PathVariable String reportId) {
        return BaseResponse.success(applicationService.getReport(reportId));
    }

    @GetMapping("/{reportId}/download")
    public ResponseEntity<byte[]> downloadReport(@PathVariable String reportId) {
        ReportDownloadContent content = applicationService.downloadReport(reportId);
        return ResponseEntity.ok()
                .contentType(MediaType.parseMediaType(content.contentType()))
                .header(HttpHeaders.CONTENT_DISPOSITION,
                        ContentDisposition.attachment()
                                .filename(content.fileName(), StandardCharsets.UTF_8)
                                .build()
                                .toString())
                .body(content.bytes());
    }

    @GetMapping(value = "/{reportId}/preview/html", produces = MediaType.TEXT_HTML_VALUE)
    public ResponseEntity<String> previewReportHtml(@PathVariable String reportId) {
        ReportHtmlPreviewContent content = applicationService.previewReportHtml(reportId);
        return ResponseEntity.ok()
                .contentType(MediaType.parseMediaType(content.contentType()))
                .body(content.html());
    }
}
