package com.kym.miniapp.controller;

import com.kym.common.R;
import com.kym.entity.miniapp.queryParams.ApplyInvoiceParams;
import com.kym.service.miniapp.InvoiceService;
import com.kym.service.wechat.WxPayService;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 发票记录表 前端控制器
 * </p>
 *
 * @author skyline
 * @since 2023-09-15
 */
@RestController
@RequestMapping("/invoice")
public class InvoiceController {

    private final InvoiceService invoiceService;

    private final WxPayService wxPayService;

    public InvoiceController(InvoiceService invoiceService, WxPayService wxPayService) {
        this.invoiceService = invoiceService;
        this.wxPayService = wxPayService;
    }

    /**
     * 用户发票抬头填写完成通知
     *
     * @param request
     * @return
     */
    @PostMapping("/titleWriteNotice")
    R<?> titleWriteNotice(HttpServletRequest request) {
        return R.success();
    }

    /**
     * 申请发票
     *
     * @param params
     * @return 用户抬头填写URL
     */
    @PostMapping("/applyInvoice")
    R<?> applyInvoice(@RequestBody ApplyInvoiceParams params) {
        return R.success(invoiceService.applyInvoice(params));
    }

    /**
     * 发票列表
     *
     * @param status
     * @return 发票列表
     */
    @GetMapping("/list")
    R<?> listInvoiceForApp(@RequestParam(value = "status", required = false) Integer status) {
        return R.success(invoiceService.listInvoiceForApp(status));
    }


    /**
     * 接收发票相关通知
     *
     * @param request
     * @return
     */
    @PostMapping("/notify")
    R<?> invoiceNotify(HttpServletRequest request) {
        wxPayService.invoiceNotify(request);
        return R.success();
    }

    /**
     * 下载发票
     *
     * @param invoiceId
     * @return
     */
    @GetMapping("/downloadInvoice/{invoiceId}")
    R<?> downloadInvoice(@PathVariable("invoiceId") String invoiceId) {
        return R.success(wxPayService.downloadInvoice(invoiceId));
    }

}
