package com.xindazhou.business.controller.admin.agreement;

import com.xindazhou.framework.common.pojo.CommonResult;
import com.xindazhou.framework.common.pojo.PageResult;
import com.xindazhou.business.controller.admin.agreement.vo.AgreementPageReqVO;
import com.xindazhou.business.controller.admin.agreement.vo.AgreementRespVO;
import com.xindazhou.business.controller.admin.agreement.vo.AgreementSaveReqVO;
import com.xindazhou.business.convert.agreement.AgreementConvert;
import com.xindazhou.business.dal.dataobject.agreement.AgreementDO;
import com.xindazhou.business.service.agreement.AgreementService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import static com.xindazhou.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - APP协议管理")
@RestController
@RequestMapping("/admin/system")
@Validated
public class AgreementController {

    @Resource
    private AgreementService agreementService;

    @PostMapping("/agreement")
    @Operation(summary = "创建协议")
    @PreAuthorize("@ss.hasPermission('system:agreement:create')")
    public CommonResult<Long> createAgreement(@Valid @RequestBody AgreementSaveReqVO createReqVO) {
        return success(agreementService.createAgreement(createReqVO));
    }

    @PutMapping("/agreement/{agreementId}")
    @Operation(summary = "修改协议")
    @PreAuthorize("@ss.hasPermission('system:agreement:update')")
    public CommonResult<Boolean> updateAgreement(@PathVariable("agreementId") String agreementId,
                                                 @Valid @RequestBody AgreementSaveReqVO updateReqVO) {
        // 如果前端传递的是agreementId字符串，需要转换为Long id
        // 这里假设前端传递的是id，不是agreementId
        agreementService.updateAgreement(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/agreement/{agreementId}")
    @Operation(summary = "删除协议")
    @Parameter(name = "agreementId", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system:agreement:delete')")
    public CommonResult<Boolean> deleteAgreement(@PathVariable("agreementId") String agreementId) {
        // 前端传递的是agreementId字符串，需要转换为Long id
        // 这里需要根据agreementId查询到id，或者修改Service支持agreementId
        Long id = Long.parseLong(agreementId);
        agreementService.deleteAgreement(id);
        return success(true);
    }

    @GetMapping("/agreement/{agreementId}")
    @Operation(summary = "获得协议")
    @Parameter(name = "agreementId", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system:agreement:query')")
    public CommonResult<AgreementRespVO> getAgreement(@PathVariable("agreementId") String agreementId) {
        Long id = Long.parseLong(agreementId);
        AgreementDO agreement = agreementService.getAgreement(id);
        return success(AgreementConvert.INSTANCE.convert(agreement));
    }

    // 为了兼容前端调用的 /system/agreements 路径
    @GetMapping("/agreements")
    @Operation(summary = "获得协议分页")
    @PreAuthorize("@ss.hasPermission('system:agreement:query')")
    public CommonResult<PageResult<AgreementRespVO>> getAgreements(@Valid AgreementPageReqVO pageReqVO) {
        PageResult<AgreementDO> page = agreementService.getAgreementPage(pageReqVO);
        return success(AgreementConvert.INSTANCE.convertPage(page));
    }

}

