package com.zhentao.controller;

import com.zhentao.common.Result;
import com.zhentao.entity.SuccessCase;
import com.zhentao.service.SuccessCaseService;
import com.zhentao.vo.SuccessCaseVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 成功案例控制器
 */
@RestController
public class SuccessCaseController {
    
    @Autowired
    private SuccessCaseService successCaseService;
    
    // ==================== 客户端接口 ====================
    
    /**
     * 获取成功案例详情
     * 
     * @param caseNo 案例编号
     * @return 案例详情
     */
    @GetMapping("/api/success-case/detail/{caseNo}")
    public Result<SuccessCaseVO> getCaseDetail(@PathVariable String caseNo) {
        try {
            SuccessCaseVO caseDetail = successCaseService.getCaseDetail(caseNo);
            if (caseDetail == null) {
                return Result.error("案例不存在或已下架");
            }
            return Result.success(caseDetail);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("获取案例详情失败：" + e.getMessage());
        }
    }
    
    /**
     * 获取成功案例列表
     * 
     * @return 案例列表
     */
    @GetMapping("/api/success-case/list")
    public Result<List<SuccessCaseVO>> getCaseList() {
        try {
            List<SuccessCaseVO> caseList = successCaseService.getCaseList();
            return Result.success(caseList);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("获取案例列表失败：" + e.getMessage());
        }
    }
    
    /**
     * 获取案例时间线
     * 
     * @param caseNo 案例编号
     * @return 时间线列表
     */
    @GetMapping("/api/success-case/timeline/{caseNo}")
    public Result<?> getTimeline(@PathVariable String caseNo) {
        try {
            return Result.success(successCaseService.getTimeline(caseNo));
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("获取时间线失败：" + e.getMessage());
        }
    }
    
    // ==================== 管理端接口 ====================
    
    /**
     * 【管理端】获取案例列表（分页）
     * 
     * @param page 页码
     * @param pageSize 每页数量
     * @return 分页结果
     */
    @GetMapping("/api/case/list")
    public Result<Map<String, Object>> getAdminCaseList(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        try {
            Map<String, Object> result = successCaseService.getAdminCaseList(page, pageSize);
            return Result.success(result);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("获取案例列表失败：" + e.getMessage());
        }
    }
    
    /**
     * 【管理端】获取案例详情
     * 
     * @param caseId 案例ID
     * @return 案例详情
     */
    @GetMapping("/api/case/detail/{caseId}")
    public Result<SuccessCase> getAdminCaseDetail(@PathVariable Integer caseId) {
        try {
            SuccessCase successCase = successCaseService.getAdminCaseById(caseId);
            if (successCase == null) {
                return Result.error("案例不存在");
            }
            return Result.success(successCase);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("获取案例详情失败：" + e.getMessage());
        }
    }
    
    /**
     * 【管理端】创建案例
     * 
     * @param successCase 案例信息
     * @return 创建的案例
     */
    @PostMapping("/api/case/create")
    public Result<SuccessCase> createCase(@RequestBody SuccessCase successCase) {
        try {
            SuccessCase created = successCaseService.createCase(successCase);
            return Result.success(created);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("创建案例失败：" + e.getMessage());
        }
    }
    
    /**
     * 【管理端】更新案例
     * 
     * @param caseId 案例ID
     * @param successCase 案例信息
     * @return 更新结果
     */
    @PutMapping("/api/case/update/{caseId}")
    public Result<String> updateCase(@PathVariable Integer caseId, @RequestBody SuccessCase successCase) {
        try {
            boolean success = successCaseService.updateCase(caseId, successCase);
            if (success) {
                return Result.success("更新成功");
            } else {
                return Result.error("更新失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("更新案例失败：" + e.getMessage());
        }
    }
    
    /**
     * 【管理端】删除案例
     * 
     * @param caseId 案例ID
     * @return 删除结果
     */
    @DeleteMapping("/api/case/delete/{caseId}")
    public Result<String> deleteCase(@PathVariable Integer caseId) {
        try {
            boolean success = successCaseService.deleteCase(caseId);
            if (success) {
                return Result.success("删除成功");
            } else {
                return Result.error("删除失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("删除案例失败：" + e.getMessage());
        }
    }
}

