package com.ycwl.basic.puzzle.controller;

import com.ycwl.basic.puzzle.dto.PuzzleFillRuleDTO;
import com.ycwl.basic.puzzle.dto.PuzzleFillRuleSaveRequest;
import com.ycwl.basic.puzzle.service.IPuzzleFillRuleService;
import com.ycwl.basic.utils.ApiResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 拼图填充规则管理Controller
 *
 * @author Claude
 * @since 2025-01-19
 */
@Slf4j
@RestController
@RequestMapping("/api/puzzle/admin/fill-rule")
@RequiredArgsConstructor
public class PuzzleFillRuleController {

    private final IPuzzleFillRuleService fillRuleService;

    /**
     * 创建填充规则
     *
     * @param request 保存请求(包含主规则+明细列表)
     * @return 规则ID
     */
    @PostMapping
    public ApiResponse<Long> create(@RequestBody PuzzleFillRuleSaveRequest request) {
        log.info("创建填充规则, ruleName={}, itemCount={}",
                request.getRuleName(),
                request.getItems() != null ? request.getItems().size() : 0);

        Long ruleId = fillRuleService.create(request);
        return ApiResponse.success(ruleId);
    }

    /**
     * 更新填充规则
     *
     * @param id 规则ID
     * @param request 保存请求
     * @return 是否成功
     */
    @PutMapping("/{id}")
    public ApiResponse<Boolean> update(@PathVariable Long id,
                                        @RequestBody PuzzleFillRuleSaveRequest request) {
        log.info("更新填充规则, ruleId={}, ruleName={}", id, request.getRuleName());

        request.setId(id);
        Boolean success = fillRuleService.update(request);
        return ApiResponse.success(success);
    }

    /**
     * 删除填充规则
     *
     * @param id 规则ID
     * @return 是否成功
     */
    @DeleteMapping("/{id}")
    public ApiResponse<Boolean> delete(@PathVariable Long id) {
        log.info("删除填充规则, ruleId={}", id);

        Boolean success = fillRuleService.delete(id);
        return ApiResponse.success(success);
    }

    /**
     * 查询单条规则(含明细)
     *
     * @param id 规则ID
     * @return 规则DTO
     */
    @GetMapping("/{id}")
    public ApiResponse<PuzzleFillRuleDTO> getById(@PathVariable Long id) {
        log.info("查询填充规则, ruleId={}", id);

        PuzzleFillRuleDTO rule = fillRuleService.getById(id);
        return ApiResponse.success(rule);
    }

    /**
     * 查询模板的所有规则(含明细)
     *
     * @param templateId 模板ID
     * @return 规则列表
     */
    @GetMapping("/template/{templateId}")
    public ApiResponse<List<PuzzleFillRuleDTO>> listByTemplateId(@PathVariable Long templateId) {
        log.info("查询模板的所有填充规则, templateId={}", templateId);

        List<PuzzleFillRuleDTO> rules = fillRuleService.listByTemplateId(templateId);
        return ApiResponse.success(rules);
    }

    /**
     * 启用/禁用规则
     *
     * @param id 规则ID
     * @param enabled 是否启用(0-禁用 1-启用)
     * @return 是否成功
     */
    @PostMapping("/{id}/toggle")
    public ApiResponse<Boolean> toggleEnabled(@PathVariable Long id,
                                               @RequestParam Integer enabled) {
        log.info("切换规则启用状态, ruleId={}, enabled={}", id, enabled);

        Boolean success = fillRuleService.toggleEnabled(id, enabled);
        return ApiResponse.success(success);
    }
}
