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.ActivityFormFieldQueryReq;
import com.seer.teach.mp.admin.controller.resp.AdminActivityFormFieldResp;
import com.seer.teach.mp.admin.service.IAdminActivityFormFieldService;
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/field")
@LogPrint
@EncryptionAnnotation
@DecryptionAnnotation
@RequiredArgsConstructor
public class AdminActivityFormFieldController {

    private final IAdminActivityFormFieldService adminActivityFormFieldService;

    @Operation(summary = "表单字段列表")
    @PostMapping("/page-list")
    @SaCheckPermission("mp:admin:activity:form:field:list")
    public ResultBean<PageListBean<AdminActivityFormFieldResp>> pageList(@RequestBody @Valid ActivityFormFieldQueryReq query) {
        PageListBean<AdminActivityFormFieldResp> result = adminActivityFormFieldService.pageList(query);
        return ResultBean.success(result);
    }

    @Operation(summary = "创建或更新表单字段")
    @PostMapping("/save")
    @SaCheckPermission("mp:admin:activity:form:field:save")
    public ResultBean<Boolean> save(@Valid @RequestBody ActivityFormFieldQueryReq request) {
        Boolean result = adminActivityFormFieldService.saveOrUpdate(request);
        return ResultBean.success(result);
    }

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

    @Operation(summary = "详情")
    @GetMapping("/{id}")
    @SaCheckPermission("mp:admin:activity:form:field:get")
    public ResultBean<AdminActivityFormFieldResp> get(@PathVariable Integer id) {
        AdminActivityFormFieldResp result = adminActivityFormFieldService.getById(id);
        return ResultBean.success(result);
    }
    
    @Operation(summary = "根据模板ID获取字段列表")
    @GetMapping("/list-by-template/{templateId}")
    @SaCheckPermission("mp:admin:activity:form:field:list")
    public ResultBean<List<AdminActivityFormFieldResp>> listByTemplate(@PathVariable Integer templateId) {
        List<AdminActivityFormFieldResp> result = adminActivityFormFieldService.listByTemplateId(templateId);
        return ResultBean.success(result);
    }
}