package com.ycwl.basic.puzzle.controller;

import com.ycwl.basic.integration.common.response.PageResponse;
import com.ycwl.basic.puzzle.dto.ElementCreateRequest;
import com.ycwl.basic.puzzle.dto.PuzzleElementDTO;
import com.ycwl.basic.puzzle.dto.PuzzleTemplateDTO;
import com.ycwl.basic.puzzle.dto.TemplateCreateRequest;
import com.ycwl.basic.puzzle.service.IPuzzleTemplateService;
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-17
 */
@Slf4j
@RestController
@RequestMapping("/api/puzzle/admin")
@RequiredArgsConstructor
public class PuzzleTemplateController {

    private final IPuzzleTemplateService templateService;

    /**
     * 创建模板
     */
    @PostMapping("/templates")
    public ApiResponse<Long> createTemplate(@RequestBody TemplateCreateRequest request) {
        log.info("创建模板请求: code={}, name={}", request.getCode(), request.getName());
        Long templateId = templateService.createTemplate(request);
        return ApiResponse.success(templateId);
    }

    /**
     * 更新模板
     */
    @PutMapping("/templates/{id}")
    public ApiResponse<Void> updateTemplate(@PathVariable Long id,
                                            @RequestBody TemplateCreateRequest request) {
        log.info("更新模板请求: id={}", id);
        templateService.updateTemplate(id, request);
        return ApiResponse.success(null);
    }

    /**
     * 删除模板
     */
    @DeleteMapping("/templates/{id}")
    public ApiResponse<Void> deleteTemplate(@PathVariable Long id) {
        log.info("删除模板请求: id={}", id);
        templateService.deleteTemplate(id);
        return ApiResponse.success(null);
    }

    /**
     * 获取模板详情
     */
    @GetMapping("/templates/{id}")
    public ApiResponse<PuzzleTemplateDTO> getTemplateDetail(@PathVariable Long id) {
        log.debug("获取模板详情: id={}", id);
        PuzzleTemplateDTO template = templateService.getTemplateDetail(id);
        return ApiResponse.success(template);
    }

    /**
     * 获取模板列表
     */
    @GetMapping("/templates")
    public ApiResponse<List<PuzzleTemplateDTO>> listTemplates(
            @RequestParam(required = false) Long scenicId,
            @RequestParam(required = false) String category,
            @RequestParam(required = false) Integer status) {
        log.debug("查询模板列表: scenicId={}, category={}, status={}", scenicId, category, status);
        List<PuzzleTemplateDTO> templates = templateService.listTemplates(scenicId, category, status);
        return ApiResponse.success(templates);
    }

    /**
     * 分页获取模板列表
     */
    @GetMapping("/templates/page")
    public ApiResponse<PageResponse<PuzzleTemplateDTO>> pageTemplates(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) Long scenicId,
            @RequestParam(required = false) String category,
            @RequestParam(required = false) Integer status) {
        log.debug("分页查询模板列表: page={}, pageSize={}, scenicId={}, category={}, status={}",
                page, pageSize, scenicId, category, status);
        PageResponse<PuzzleTemplateDTO> templates = templateService.pageTemplates(page, pageSize, scenicId, category, status);
        return ApiResponse.success(templates);
    }

    /**
     * 为模板添加单个元素
     */
    @PostMapping("/elements")
    public ApiResponse<Long> addElement(@RequestBody ElementCreateRequest request) {
        log.info("添加元素请求: templateId={}, elementKey={}", request.getTemplateId(), request.getElementKey());
        Long elementId = templateService.addElement(request);
        return ApiResponse.success(elementId);
    }

    /**
     * 为模板批量添加元素
     */
    @PostMapping("/templates/{templateId}/elements")
    public ApiResponse<Void> batchAddElements(@PathVariable Long templateId,
                                              @RequestBody List<ElementCreateRequest> elements) {
        log.info("批量添加元素请求: templateId={}, count={}", templateId, elements.size());
        templateService.batchAddElements(templateId, elements);
        return ApiResponse.success(null);
    }

    /**
     * 批量替换模板元素（删除旧元素，添加新元素）
     */
    @PutMapping("/templates/{templateId}/elements")
    public ApiResponse<Void> replaceElements(@PathVariable Long templateId,
                                             @RequestBody List<ElementCreateRequest> elements) {
        log.info("批量替换元素请求: templateId={}, count={}", templateId, elements.size());
        templateService.replaceElements(templateId, elements);
        return ApiResponse.success(null);
    }

    /**
     * 更新元素
     */
    @PutMapping("/elements/{id}")
    public ApiResponse<Void> updateElement(@PathVariable Long id,
                                           @RequestBody ElementCreateRequest request) {
        log.info("更新元素请求: id={}", id);
        templateService.updateElement(id, request);
        return ApiResponse.success(null);
    }

    /**
     * 删除元素
     */
    @DeleteMapping("/elements/{id}")
    public ApiResponse<Void> deleteElement(@PathVariable Long id) {
        log.info("删除元素请求: id={}", id);
        templateService.deleteElement(id);
        return ApiResponse.success(null);
    }

    /**
     * 获取元素详情
     */
    @GetMapping("/elements/{id}")
    public ApiResponse<PuzzleElementDTO> getElementDetail(@PathVariable Long id) {
        log.debug("获取元素详情: id={}", id);
        PuzzleElementDTO element = templateService.getElementDetail(id);
        return ApiResponse.success(element);
    }
}
