package com.cf.imes.module.system.controller.admin.funds.invoice;

import com.cf.imes.framework.common.pojo.CommonResult;
import com.cf.imes.framework.common.pojo.PageResult;
import com.cf.imes.module.system.controller.admin.funds.invoice.vo.InvoiceAmountAndAvailablePageReqVO;
import com.cf.imes.module.system.controller.admin.funds.invoice.vo.InvoiceAmountRespVO;
import com.cf.imes.module.system.controller.admin.funds.invoice.vo.InvoiceConfirmReqVO;
import com.cf.imes.module.system.controller.admin.funds.invoice.vo.InvoiceDetailsPageReqVO;
import com.cf.imes.module.system.controller.admin.funds.invoice.vo.InvoiceDetailsRespVO;
import com.cf.imes.module.system.controller.admin.funds.invoice.vo.InvoiceRecordsReqVO;
import com.cf.imes.module.system.controller.admin.funds.invoice.vo.InvoiceRecordsRespVO;
import com.cf.imes.module.system.controller.admin.funds.invoice.vo.InvoiceTitleSaveReqVO;
import com.cf.imes.module.system.dal.dataobject.funds.purchase.PurchaseRecordDO;
import com.cf.imes.module.system.service.funds.invoice.InvoiceService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
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.RequestPart;
import org.springframework.web.bind.annotation.RestController;

import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

import static com.cf.imes.framework.common.pojo.CommonResult.success;


/**
 * @author 发票管理
 */
@Tag(name = "管理后台 - 发票管理")
@RestController
@RequestMapping("/system/invoice")
@Validated
public class InvoiceController {

    @Resource
    private InvoiceService invoiceService;


    @PostMapping("/title")
    @Operation(summary = "新增/修改发票抬头信息")
    public CommonResult<Boolean> insetInvoiceTitle(@Valid @RequestBody InvoiceTitleSaveReqVO reqVO) {
        invoiceService.insetInvoiceTitle(reqVO);
        return success(Boolean.TRUE);
    }

    @GetMapping("/org/amount")
    @Operation(summary = "获取组织的开票金额信息")
    public CommonResult<InvoiceAmountRespVO> getInvoiceAmount() {
        return success(invoiceService.getInvoiceAmount());
    }

    @GetMapping("/available")
    @Operation(summary = "获取组织可开票购买记录")
    public CommonResult<PageResult<PurchaseRecordDO>> getAvailablePage(@Valid InvoiceAmountAndAvailablePageReqVO pageReqVO) {
        return success(invoiceService.getAvailablePage(pageReqVO));
    }

    @GetMapping("/manage/amount")
    @Operation(summary = "获取管理端开票金额信息")
    public CommonResult<InvoiceAmountRespVO> getManageEndPointInvoiceAmount() {
        return success(invoiceService.getManageInvoiceAmount());
    }

    @PostMapping("")
    @Operation(summary = "组织申请开票")
    public CommonResult<Boolean> applyInvoiceRecords(@Valid @RequestBody InvoiceRecordsReqVO reqVO) {
        invoiceService.applyInvoice(reqVO);
        return success(Boolean.TRUE);
    }

    @GetMapping("/records")
    @Operation(summary = "获取组织的开票记录")
    public CommonResult<PageResult<InvoiceRecordsRespVO>> getInvoiceRecords(@Valid InvoiceDetailsPageReqVO pageReqVO) {
        return success(invoiceService.getInvoiceRecords(pageReqVO));
    }

    @GetMapping("/record/{recordId}")
    @Parameter(name = "recordId", description = "发票申请记录ID", required = true)
    @Operation(summary = "获取开票记录对应的购买记录和抬头信息")
    public CommonResult<InvoiceDetailsRespVO> getInvoiceRecordDetail(@PathVariable("recordId") Long recordId) {
        return success(invoiceService.getInvoiceRecordDetail(recordId));
    }

    @PostMapping("/confirm")
    @Operation(summary = "确认发票")
    public CommonResult<Boolean> confirmInvoice(@Valid @RequestPart("invoiceConfirmInfo") InvoiceConfirmReqVO reqVO,
                                                @RequestPart(value = "file", required = false) MultipartFile file) throws IOException {
        invoiceService.confirmInvoice(reqVO, file);
        return success(Boolean.TRUE);
    }
}
