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.contracts.report.CreateExamSprintReportResponse;
import cn.yunzhixue.ability.center.examsprint.contracts.report.CreateExamSprintReportWithUrlResponse;
import cn.yunzhixue.ability.center.examsprint.contracts.report.ExamSprintReportDetailResponse;
import cn.yunzhixue.ability.center.kernel.BaseResponse;
import com.fasterxml.jackson.databind.JsonNode;
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")
public class ExamSprintReportController {

    private final ExamSprintReportApplicationService applicationService;

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

    @PostMapping("/outlook-reports")
    public ResponseEntity<BaseResponse<CreateExamSprintReportResponse>> createOutlookReport(
            @RequestBody JsonNode payload) {
        return ResponseEntity.accepted().body(BaseResponse.success(applicationService.createOutlookReport(payload)));
    }

    @PostMapping("/achievement-reports")
    public ResponseEntity<BaseResponse<CreateExamSprintReportResponse>> createAchievementReport(
            @RequestBody JsonNode payload) {
        return ResponseEntity.accepted().body(BaseResponse.success(applicationService.createAchievementReport(payload)));
    }

    @PostMapping("/outlook-reports/sync")
    public BaseResponse<CreateExamSprintReportWithUrlResponse> createOutlookReportSync(@RequestBody JsonNode payload) {
        return BaseResponse.success(applicationService.createOutlookReportSync(payload));
    }

    @PostMapping("/achievement-reports/sync")
    public BaseResponse<CreateExamSprintReportWithUrlResponse> createAchievementReportSync(@RequestBody JsonNode payload) {
        return BaseResponse.success(applicationService.createAchievementReportSync(payload));
    }

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

    @GetMapping("/reports/{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());
    }
}
