package com.zhentao.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhentao.common.Result;
import com.zhentao.dto.MatchmakerQueryDTO;
import com.zhentao.entity.Matchmaker;
import com.zhentao.service.MatchmakerService;
import com.zhentao.vo.MatchmakerVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 红娘控制器
 */
@RestController
@RequestMapping("/api/matchmaker")
public class MatchmakerController {
    
    @Autowired
    private MatchmakerService matchmakerService;
    
    /**
     * 分页查询红娘列表
     * 
     * @param queryDTO 查询条件
     * @return 分页数据
     */
    @PostMapping("/list")
    public Result<Page<MatchmakerVO>> getMatchmakerList(@RequestBody MatchmakerQueryDTO queryDTO) {
        try {
            Page<MatchmakerVO> page = matchmakerService.getMatchmakerPage(queryDTO);
            return Result.success(page);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("查询红娘列表失败：" + e.getMessage());
        }
    }
    
    /**
     * 获取红娘详情
     * 
     * @param matchmakerId 红娘ID
     * @return 红娘详情
     */
    @GetMapping("/detail/{matchmakerId}")
    public Result<MatchmakerVO> getMatchmakerDetail(@PathVariable Integer matchmakerId) {
        try {
            MatchmakerVO matchmakerVO = matchmakerService.getMatchmakerDetail(matchmakerId);
            if (matchmakerVO == null) {
                return Result.error("红娘不存在或已离职");
            }
            return Result.success(matchmakerVO);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("查询红娘详情失败：" + e.getMessage());
        }
    }
    
    /**
     * 获取全职红娘列表（首页展示）
     * 
     * @param pageNum 页码
     * @param pageSize 每页大小
     * @return 全职红娘列表
     */
    @GetMapping("/formal")
    public Result<Page<MatchmakerVO>> getFormalMatchmakers(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        try {
            Page<MatchmakerVO> page = matchmakerService.getFormalMatchmakers(pageNum, pageSize);
            return Result.success(page);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("查询全职红娘列表失败：" + e.getMessage());
        }
    }
    
    /**
     * 创建红娘
     * 
     * @param matchmaker 红娘信息
     * @return 创建结果
     */
    @PostMapping("/create")
    public Result<Matchmaker> createMatchmaker(@RequestBody Matchmaker matchmaker) {
        try {
            // 如果没有提供username，使用手机号作为用户名
            if (matchmaker.getUsername() == null || matchmaker.getUsername().isEmpty()) {
                matchmaker.setUsername(matchmaker.getPhone());
            }
            
            // 如果没有提供密码，设置默认密码（建议首次登录时强制修改）
            if (matchmaker.getPassword() == null || matchmaker.getPassword().isEmpty()) {
                matchmaker.setPassword("123456"); // 默认密码，实际应该加密
            }
            
            // 如果没有提供状态，设置默认状态为正常
            if (matchmaker.getStatus() == null) {
                matchmaker.setStatus(1); // 1-正常
            }
            
            boolean success = matchmakerService.save(matchmaker);
            if (success) {
                // 清除列表缓存
                matchmakerService.clearMatchmakerCache(null);
                return Result.success(matchmaker);
            } else {
                return Result.error("创建红娘失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("创建红娘失败：" + e.getMessage());
        }
    }
    
    /**
     * 更新红娘信息
     * 
     * @param matchmakerId 红娘ID
     * @param matchmaker 红娘信息
     * @return 更新结果
     */
    @PutMapping("/update/{matchmakerId}")
    public Result<Matchmaker> updateMatchmaker(
            @PathVariable Integer matchmakerId,
            @RequestBody Matchmaker matchmaker) {
        try {
            matchmaker.setMatchmakerId(matchmakerId);
            boolean success = matchmakerService.updateById(matchmaker);
            if (success) {
                // 清除相关缓存
                matchmakerService.clearMatchmakerCache(matchmakerId);
                return Result.success(matchmaker);
            } else {
                return Result.error("更新红娘信息失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("更新红娘信息失败：" + e.getMessage());
        }
    }
    
    /**
     * 删除红娘
     * 
     * @param matchmakerId 红娘ID
     * @return 删除结果
     */
    @DeleteMapping("/delete/{matchmakerId}")
    public Result<Void> deleteMatchmaker(@PathVariable Integer matchmakerId) {
        try {
            boolean success = matchmakerService.removeById(matchmakerId);
            if (success) {
                // 清除相关缓存
                matchmakerService.clearMatchmakerCache(matchmakerId);
                return Result.success(null);
            } else {
                return Result.error("删除红娘失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("删除红娘失败：" + e.getMessage());
        }
    }
}

