package com.huimv.employment.controller.publicapi;

import com.huimv.employment.service.registration.RegistrationBatchService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
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.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 登记小程序码图片公开访问（路径写入 {@code fe_registration_batch.qr_code_url}）。
 */
@RestController
@RequestMapping("/api/v1/public/registration-qr")
@Tag(name = "登记小程序码", description = "公开访问登记批次小程序码 PNG")
public class RegistrationQrController {

    private final RegistrationBatchService registrationBatchService;

    public RegistrationQrController(RegistrationBatchService registrationBatchService) {
        this.registrationBatchService = registrationBatchService;
    }

    @GetMapping("/{scene}.png")
    @Operation(summary = "获取登记小程序码图片（scene=订单编号）")
    public ResponseEntity<byte[]> getQrPng(@PathVariable("scene") String scene) {
        byte[] png = registrationBatchService.readQrPng(scene);
        if (png == null || png.length == 0) {
            return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
        }
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.IMAGE_PNG);
        headers.setCacheControl("public, max-age=86400");
        return new ResponseEntity<>(png, headers, HttpStatus.OK);
    }
}
