package com.seer.teach.mp.admin.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.seer.teach.common.PageListBean;
import com.seer.teach.common.ResultBean;
import com.seer.teach.common.annotation.DecryptionAnnotation;
import com.seer.teach.common.annotation.EncryptionAnnotation;
import com.seer.teach.common.annotation.LogPrint;
import com.seer.teach.mp.admin.controller.req.ActivityFormVariableQueryReq;
import com.seer.teach.mp.admin.controller.resp.AdminActivityFormVariableResp;
import com.seer.teach.mp.admin.service.IAdminActivityFormVariableService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import java.util.List;

/**
 * <p>
 * 活动表单变量管理端控制器
 * </p>
 *
 * @author Lingma
 * @since 2026-01-10
 */
@Tag(name = "ADMIN - 活动表单变量")
@RestController
@RequestMapping("/activity/form/variable")
@LogPrint
@EncryptionAnnotation
@DecryptionAnnotation
@RequiredArgsConstructor
public class AdminActivityFormVariableController {

    private final IAdminActivityFormVariableService adminActivityFormVariableService;

    @Operation(summary = "表单变量列表")
    @PostMapping("/page-list")
    @SaCheckPermission("mp:admin:activity:form:variable:list")
    public ResultBean<PageListBean<AdminActivityFormVariableResp>> pageList(@RequestBody @Valid ActivityFormVariableQueryReq query) {
        PageListBean<AdminActivityFormVariableResp> result = adminActivityFormVariableService.pageList(query);
        return ResultBean.success(result);
    }

    @Operation(summary = "创建或更新表单变量")
    @PostMapping("/save")
    @SaCheckPermission("mp:admin:activity:form:variable:save")
    public ResultBean<Boolean> save(@Valid @RequestBody ActivityFormVariableQueryReq request) {
        Boolean result = adminActivityFormVariableService.saveOrUpdate(request);
        return ResultBean.success(result);
    }

    @Operation(summary = "删除表单变量")
    @DeleteMapping("/{id}")
    @SaCheckPermission("mp:admin:activity:form:variable:delete")
    public ResultBean<Boolean> delete(@PathVariable Integer id) {
        Boolean result = adminActivityFormVariableService.deleteById(id);
        return ResultBean.success(result);
    }

    @Operation(summary = "详情")
    @GetMapping("/{id}")
    @SaCheckPermission("mp:admin:activity:form:variable:get")
    public ResultBean<AdminActivityFormVariableResp> get(@PathVariable Integer id) {
        AdminActivityFormVariableResp result = adminActivityFormVariableService.getById(id);
        return ResultBean.success(result);
    }
    
    @Operation(summary = "根据执行实例ID获取变量列表")
    @GetMapping("/list-by-execution/{executionId}")
    @SaCheckPermission("mp:admin:activity:form:variable:list")
    public ResultBean<List<AdminActivityFormVariableResp>> listByExecution(@PathVariable Integer executionId) {
        List<AdminActivityFormVariableResp> result = adminActivityFormVariableService.listByExecutionId(executionId);
        return ResultBean.success(result);
    }
}