package com.jeesharp.business.modules.setting.controller;

import cn.hutool.core.util.StrUtil;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.jeesharp.business.modules.setting.application.PrivacyPolicyApplicationService;
import com.jeesharp.business.modules.setting.dto.PrivacyPolicySaveDTO;
import com.jeesharp.business.modules.setting.enums.PrivacyPolicyClientEnum;
import com.jeesharp.business.modules.setting.vo.PrivacyPolicyInfoVO;
import com.jeesharp.business.modules.setting.vo.PrivacyPolicyListVO;
import com.jeesharp.business.modules.setting.vo.PrivacyPolicyQueryVO;
import com.jeesharp.framework.pojo.AjaxResult;
import com.jeesharp.framework.pojo.Page;
import com.jeesharp.framework.pojo.query.PageRespVO;
import com.jeesharp.framework.web.controller.BaseController;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * 隐私政策/法律文档管理 REST 接口（后台管理端）。
 *
 * @author jeesharp
 * @since 2026-05-20
 */
@RestController
@RequestMapping(value = "${jeesharp.web.adminPath}/setting/privacyPolicy/v1")
@Tag(name = "站点设置-隐私政策", description = "隐私政策与服务协议等法律文档维护")
@AllArgsConstructor
public class PrivacyPolicyController extends BaseController {

    private final PrivacyPolicyApplicationService privacyPolicyApplicationService;

    @ApiOperationSupport(order = 1)
    @Operation(summary = "获取详情", description = "可选 client：PC/H5/MP，默认 PC（原文，用于编辑回填）")
    @PreAuthorize("@ss.hasPermission('setting:privacyPolicy:info')")
    @GetMapping("info")
    public AjaxResult<PrivacyPolicyInfoVO> info(
            @RequestParam("id") String id,
            @Parameter(description = "展示端：PC/H5/MP，默认 PC", example = "PC")
            @RequestParam(value = "client", required = false) String client) {
        PrivacyPolicyClientEnum target = PrivacyPolicyClientEnum.fromCode(
                client, PrivacyPolicyClientEnum.DEFAULT_FOR_ADMIN);
        return AjaxResult.okData(privacyPolicyApplicationService.get(id, target));
    }

    @ApiOperationSupport(order = 2)
    @Operation(summary = "分页列表")
    @PreAuthorize("@ss.hasPermission('setting:privacyPolicy:list')")
    @PostMapping("list")
    public AjaxResult<PageRespVO<PrivacyPolicyListVO>> list(@RequestBody PrivacyPolicyQueryVO query) {
        return AjaxResult.okData(privacyPolicyApplicationService.findPage(new Page<>(query), query));
    }

    @ApiOperationSupport(order = 3)
    @Operation(summary = "保存")
    @PreAuthorize("@ss.hasPermission('setting:privacyPolicy:save')")
    @PostMapping("save")
    public AjaxResult<Void> save(@RequestBody @Valid PrivacyPolicySaveDTO dto) {
        privacyPolicyApplicationService.save(dto);
        return AjaxResult.ok("保存成功");
    }

    @ApiOperationSupport(order = 4)
    @Operation(summary = "删除")
    @PreAuthorize("@ss.hasPermission('setting:privacyPolicy:delete')")
    @GetMapping("delete")
    public AjaxResult<Void> delete(
            @Parameter(description = "ID，多个用逗号分隔", required = true)
            @RequestParam("ids") String ids) {
        privacyPolicyApplicationService.delete(ids);
        return AjaxResult.ok("删除成功");
    }

    @ApiOperationSupport(order = 5)
    @Operation(summary = "禁用")
    @PreAuthorize("@ss.hasPermission('setting:privacyPolicy:disable')")
    @GetMapping("disable")
    public AjaxResult<Void> disable(@RequestParam("id") String id) {
        privacyPolicyApplicationService.disable(id);
        return AjaxResult.ok("禁用成功");
    }

    @ApiOperationSupport(order = 6)
    @Operation(summary = "启用")
    @PreAuthorize("@ss.hasPermission('setting:privacyPolicy:enable')")
    @GetMapping("enable")
    public AjaxResult<Void> enable(@RequestParam("id") String id) {
        privacyPolicyApplicationService.enable(id);
        return AjaxResult.ok("启用成功");
    }
}
