package jnpf.certificate.controller.app;

import jnpf.base.ActionResult;
import jnpf.certificate.service.CertificateInstanceService;
import jnpf.certificate.service.CertificateManageService;
import jnpf.model.certificate.req.app.CertificateAppBusinessLicenseUpdateReq;
import jnpf.model.certificate.req.app.CertificateAppHealthCertificateUpdateReq;
import jnpf.model.certificate.req.app.CertificateAppHygieneLicenseUpdateReq;
import jnpf.model.certificate.req.app.CertificateAppStoreCustomUpdateReq;
import jnpf.model.certificate.vo.app.CertificateAppCertificateDetailVO;
import jnpf.model.certificate.vo.app.HealthCertificateDetailVO;
import jnpf.model.certificate.vo.app.HealthCertificateVO;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.util.Optional;

/**
 * App端证照管理控制器。
 */
@RestController
@Validated
@RequestMapping("/app/certificate-manage")
public class AppCertificateManageController {

    /**
     * App端证照管理服务。
     */
    @Autowired
    private CertificateManageService certificateManageService;
    @Autowired
    private CertificateInstanceService certificateInstanceService;

    /**
     * 根据证照实例ID查询证照详情。
     * 返回结果中包含certificateType，并按类型返回不同明细对象。
     *
     * @param certificateInstanceId 证照实例ID
     * @return 证照详情
     */
    @GetMapping("/query-info")
    public ActionResult<CertificateAppCertificateDetailVO> queryInfo(@RequestParam("certificateInstanceId")
                                                                      @NotBlank(message = "证照实例ID不能为空")
                                                                      String certificateInstanceId) {
        return ActionResult.success(certificateManageService.queryInfo(certificateInstanceId));
    }

    /**
     * 更新健康证。
     *
     * @param req 更新参数
     * @return 操作结果
     */
    @PutMapping("/update-health")
    public ActionResult<Void> updateHealth(@Validated @RequestBody CertificateAppHealthCertificateUpdateReq req) {
        certificateManageService.updateHealthCertificate(req);
        return ActionResult.success();
    }

    /**
     * 查询某个人的健康证信息
     *
     * @return 健康证信息
     */
    @GetMapping("/query-health-certificate/{userId}")
    public ActionResult<HealthCertificateVO> getHealthCertificateDetail(@PathVariable("userId") String userId) {
        Optional<HealthCertificateDetailVO> healthCertificateDetailVOOptional =  certificateInstanceService.getHealthCertificateDetail(userId);
        return healthCertificateDetailVOOptional.map(h->ActionResult.success(HealthCertificateVO.of(h)))
                .orElseGet(() -> ActionResult.fail(404, "未查询到健康证信息"));
    }

    /**
     * 更新营业执照。
     *
     * @param req 更新参数
     * @return 操作结果
     */
    @PutMapping("/update-business-license")
    public ActionResult<Void> updateBusinessLicense(@Validated @RequestBody CertificateAppBusinessLicenseUpdateReq req) {
        certificateManageService.updateBusinessLicense(req);
        return ActionResult.success();
    }

    /**
     * 更新食品经营许可证。
     *
     * @param req 更新参数
     * @return 操作结果
     */
    @PutMapping("/update-hygiene-license")
    public ActionResult<Void> updateHygieneLicense(@Validated @RequestBody CertificateAppHygieneLicenseUpdateReq req) {
        certificateManageService.updateHygieneLicense(req);
        return ActionResult.success();
    }

    /**
     * 更新门店自定义证照。
     *
     * @param req 更新参数
     * @return 操作结果
     */
    @PutMapping("/update-store-custom")
    public ActionResult<Void> updateStoreCustom(@Valid @RequestBody CertificateAppStoreCustomUpdateReq req) {
        certificateManageService.updateStoreCustomCertificate(req);
        return ActionResult.success();
    }
}
