package com.mcwl.web.controller.system;

import com.mcwl.common.core.controller.BaseController;
import com.mcwl.system.domain.Policy;
import com.mcwl.system.service.PolicyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

/**
 * @Author：ChenYan
 * @Project：mcwl-ai
 * @Package：com.mcwl.web.controller.system
 * @Filename：PerController
 * @Description TODO
 * @Date：2025/2/14 10:11
 */
@Api(tags = "隐私政策/用户协议")
@RestController
@RequestMapping("Policy")
@RequiredArgsConstructor
public class PerController extends BaseController {
    @Autowired
    private PolicyService policyService;

    /**
     * 获取指定类型的政策内容
     *
     * @param type 政策类型
     * @return 政策内容
     */
    @ApiOperation(value = "获取指定类型的政策内容")
    @GetMapping("/{type}")
    public ResponseEntity<?> getPolicy(@PathVariable("type") String type) {
        Policy policy = policyService.getPolicyByType(type);
        if (policy == null) {
            return new ResponseEntity<>("Policy not found", HttpStatus.NOT_FOUND);
        }
        return new ResponseEntity<>(policy.getContent(), HttpStatus.OK);
    }
    /**
     * 更新指定类型的政策内容
     *
     * @param type   政策类型
     * @param content 政策内容
     * @return 更新结果
     */
    @ApiOperation(value = "更新指定类型的政策内容")
    @PutMapping("/{type}")
    public ResponseEntity<?> updatePolicy(@PathVariable("type") String type, @RequestBody String content) {
        try {
            policyService.updatePolicyContent(type, content);
            return new ResponseEntity<>("Policy updated successfully", HttpStatus.OK);
        } catch (RuntimeException e) {
            return new ResponseEntity<>(e.getMessage(), HttpStatus.BAD_REQUEST);
        }
    }
}
