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

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.stp.StpUtil;
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.app.controller.req.ActivityFormSubmitReq;
import com.seer.teach.mp.app.controller.req.AppActivityFormExecutionQueryReq;
import com.seer.teach.mp.app.controller.resp.AppActivityFormExecutionResp;
import com.seer.teach.mp.app.controller.resp.AppActivityFormFieldResp;
import com.seer.teach.mp.app.controller.resp.AppActivityFormResp;
import com.seer.teach.mp.app.service.IAppActivityFormService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * APP端活动表单控制器
 * </p>
 *
 * @author Lingma
 * @since 2026-01-10
 */
@Tag(name = "APP - 活动表单")
@RestController
@RequestMapping("/app/activity/form")
@LogPrint
@EncryptionAnnotation
@DecryptionAnnotation
@RequiredArgsConstructor
@Slf4j
public class AppActivityFormController {

    private final IAppActivityFormService appActivityFormService;

    @Operation(summary = "获取活动可用的表单")
    @GetMapping("/{activityId}")
    @SaCheckLogin
    public ResultBean<AppActivityFormResp> getFormByActivity(@PathVariable Integer activityId) {
        AppActivityFormResp result = appActivityFormService.getFormByActivity(activityId);
        return ResultBean.success(result);
    }

    @Operation(summary = "获取表单字段列表")
    @GetMapping("/field/{templateId}")
    @SaCheckLogin
    public ResultBean<List<AppActivityFormFieldResp>> getFieldList(@PathVariable Integer templateId) {
        List<AppActivityFormFieldResp> result = appActivityFormService.getFieldListByTemplate(templateId);
        return ResultBean.success(result);
    }

    @Operation(summary = "获取用户提交的表单列表")
    @PostMapping("/execution/page-list")
    @SaCheckLogin
    public ResultBean<PageListBean<AppActivityFormExecutionResp>> getExecutionList(@RequestBody @Valid AppActivityFormExecutionQueryReq query) {
        Integer userId = StpUtil.getLoginIdAsInt();
        PageListBean<AppActivityFormExecutionResp> result = appActivityFormService.getExecutionList(query, userId);
        return ResultBean.success(result);
    }

    @Operation(summary = "提交表单")
    @PostMapping("/submit")
    @SaCheckLogin
    public ResultBean<String> submitForm(@RequestBody @Valid ActivityFormSubmitReq req) {
        Integer userId = StpUtil.getLoginIdAsInt();
        String executionNo = appActivityFormService.submitForm(req, userId);
        return ResultBean.success(executionNo);
    }

    @Operation(summary = "获取表单执行详情")
    @GetMapping("/execution/{id}")
    @SaCheckLogin
    public ResultBean<AppActivityFormExecutionResp> getExecutionDetail(@PathVariable Integer id) {
        return ResultBean.success(appActivityFormService.getExecutionWithFormData(id));
    }
}