package shop.alien.storeplatform.controller;

import io.swagger.annotations.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import shop.alien.entity.result.R;
import shop.alien.entity.store.StoreContract;
import shop.alien.entity.store.dto.StoreContractQueryDto;
import shop.alien.entity.store.vo.StoreContractVo;
import shop.alien.storeplatform.service.StoreContractService;
import com.baomidou.mybatisplus.core.metadata.IPage;

/**
 * 合同管理控制器
 *
 * @author alien
 * @since 2025-01-XX
 */
@Slf4j
@Api(tags = {"商家端-合同管理"})
@ApiSort(4)
@CrossOrigin
@RestController
@RequestMapping("/contract")
@RequiredArgsConstructor
public class StoreContractController {

    private final StoreContractService storeContractService;

    @ApiOperation("分页查询合同列表")
    @ApiOperationSupport(order = 1)
    @PostMapping("/list")
    public R<IPage<StoreContractVo>> getContractList(@RequestBody StoreContractQueryDto queryDto) {
        log.info("StoreContractController.getContractList?queryDto={}", queryDto);
        return storeContractService.getContractList(queryDto);
    }

    @ApiOperation("根据合同名称搜索合同列表（分页）")
    @ApiOperationSupport(order = 2)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码", dataType = "int", paramType = "query", required = false, defaultValue = "1"),
            @ApiImplicitParam(name = "pageSize", value = "每页数量", dataType = "int", paramType = "query", required = false, defaultValue = "10"),
            @ApiImplicitParam(name = "contractName", value = "合同名称（模糊查询）", dataType = "String", paramType = "query", required = false),
            @ApiImplicitParam(name = "status", value = "合同状态(0:未签署, 1:已签署)", dataType = "int", paramType = "query", required = false),
            @ApiImplicitParam(name = "storeId", value = "店铺ID", dataType = "int", paramType = "query", required = false)
    })
    @GetMapping("/list")
    public R<IPage<StoreContractVo>> getContractListByQuery(
            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
            @RequestParam(value = "contractName", required = false) String contractName,
            @RequestParam(value = "status", required = false) Integer status,
            @RequestParam(value = "storeId", required = false) Integer storeId) {
        log.info("StoreContractController.getContractListByQuery?pageNum={}, pageSize={}, contractName={}, status={}, storeId={}",
                pageNum, pageSize, contractName, status, storeId);

        StoreContractQueryDto queryDto = new StoreContractQueryDto();
        queryDto.setPageNum(pageNum);
        queryDto.setPageSize(pageSize);
        queryDto.setContractName(contractName);
        queryDto.setStatus(status);
        queryDto.setStoreId(storeId);

        return storeContractService.getContractList(queryDto);
    }

    @ApiOperation("查询合同详情")
    @ApiOperationSupport(order = 3)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "合同ID", dataType = "int", paramType = "query", required = true)
    })
    @GetMapping("/detail")
    public R<StoreContractVo> getContractDetail(@RequestParam("id") Integer id) {
        log.info("StoreContractController.getContractDetail?id={}", id);
        return storeContractService.getContractDetail(id);
    }

    @ApiOperation("新增合同")
    @ApiOperationSupport(order = 4)
    @PostMapping("/add")
    public R<StoreContract> addContract(@RequestBody StoreContract contract) {
        log.info("StoreContractController.addContract?contract={}", contract);
        return storeContractService.addContract(contract);
    }

    @ApiOperation("更新合同")
    @ApiOperationSupport(order = 5)
    @PostMapping("/update")
    public R<StoreContract> updateContract(@RequestBody StoreContract contract) {
        log.info("StoreContractController.updateContract?contract={}", contract);
        return storeContractService.updateContract(contract);
    }

    @ApiOperation("签署合同")
    @ApiOperationSupport(order = 6)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "合同ID", dataType = "long", paramType = "query", required = true)
    })
    @PostMapping("/sign")
    public R<Boolean> signContract(@RequestParam("id") Long id) {
        log.info("StoreContractController.signContract?id={}", id);
        return storeContractService.signContract(id);
    }

    @ApiOperation("删除合同")
    @ApiOperationSupport(order = 7)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "合同ID", dataType = "int", paramType = "query", required = true)
    })
    @PostMapping("/delete")
    public R<Boolean> deleteContract(@RequestParam("id") Integer id) {
        log.info("StoreContractController.deleteContract?id={}", id);
        return storeContractService.deleteContract(id);
    }

    @ApiOperation("获取合同签署状态（contract_bundle：subject_id=店铺ID，subject_type=store，返回 status）")
    @ApiOperationSupport(order = 8)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "store_id", value = "店铺ID", dataType = "int", paramType = "query", required = true)
    })
    @GetMapping("/signingStatus")
    public R<String> getSigningStatus(@RequestParam("store_id") Integer storeId) {
        log.info("StoreContractController.getSigningStatus?storeId={}", storeId);
        return storeContractService.getSigningStatus(storeId);
    }
}

