package com.seer.teach.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.seer.teach.admin.convert.QuestionOptionsConvert;
import com.seer.teach.admin.request.QuestionOptionsQueryReq;
import com.seer.teach.admin.request.QuestionOptionsReq;
import com.seer.teach.admin.response.QuestionOptionsResp;
import com.seer.teach.admin.service.AdminQuestionOptionsService;
import com.seer.teach.teacher.module.entity.QuestionOptionsEntity;
import com.seer.teach.teacher.service.QuestionOptionsService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Objects;

/**
 * <p>
 * 题目选项管理服务实现类
 * </p>
 *
 * @author ZS
 * @since 2025-10-24
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class AdminQuestionOptionsServiceImpl implements AdminQuestionOptionsService {

    private final QuestionOptionsService questionOptionsService;

    /**
     * 分页查询题目选项
     *
     * @param req 查询条件
     * @return 题目选项分页数据
     */
    @Override
    public IPage<QuestionOptionsResp> pageQuestionOptions(QuestionOptionsQueryReq req) {
        log.info("开始分页查询题目选项, 请求参数: {}", req);
        Page<QuestionOptionsEntity> page = new Page<>(req.getPageNo(), req.getPageSize());
        LambdaQueryWrapper<QuestionOptionsEntity> queryWrapper = new LambdaQueryWrapper<>();
        if (req.getQuestionId() != null) {
            queryWrapper.eq(QuestionOptionsEntity::getQuestionId, req.getQuestionId());
        }
        if (req.getContent() != null && !req.getContent().isEmpty()) {
            queryWrapper.like(QuestionOptionsEntity::getContent, req.getContent());
        }
        
        log.debug("查询条件: {}", queryWrapper.getCustomSqlSegment());
        IPage<QuestionOptionsEntity> entityPage = questionOptionsService.page(page, queryWrapper);
        log.info("分页查询题目选项完成, 共查询到 {} 条数据", entityPage.getTotal());
        return entityPage.convert(QuestionOptionsConvert.INSTANCE::convertEntityToResp);
    }

    /**
     * 根据ID获取题目选项详情
     *
     * @param id 题目选项ID
     * @return 题目选项详情
     */
    @Override
    public QuestionOptionsResp getQuestionOptionById(Integer id) {
        log.info("开始获取题目选项详情, ID: {}", id);
        QuestionOptionsEntity entity = questionOptionsService.getById(id);
        if (entity == null) {
            log.warn("未找到指定ID的题目选项, ID: {}", id);
            return null;
        }
        log.info("获取题目选项详情成功, ID: {}", id);
        return QuestionOptionsConvert.INSTANCE.convertEntityToResp(entity);
    }

    /**
     * 添加题目选项
     *
     * @param req 题目选项信息
     * @return 是否添加成功
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean addQuestionOption(QuestionOptionsReq req) {
        log.info("开始添加题目选项, 请求参数: {}", req);
        QuestionOptionsEntity entity = QuestionOptionsConvert.INSTANCE.convertReqToEntity(req);
        boolean result = questionOptionsService.save(entity);
        if (result) {
            log.info("添加题目选项成功, 新增ID: {}", entity.getId());
        } else {
            log.error("添加题目选项失败, 请求参数: {}", req);
        }
        return result;
    }

    /**
     * 更新题目选项
     *
     * @param id  题目选项ID
     * @param req 题目选项信息
     * @return 是否更新成功
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateQuestionOption(Integer id, QuestionOptionsReq req) {
        log.info("开始更新题目选项, ID: {}, 请求参数: {}", id, req);
        QuestionOptionsEntity entity = questionOptionsService.getById(id);
        if (Objects.isNull(entity)) {
            log.warn("未找到指定ID的题目选项, 无法更新, ID: {}", id);
            return false;
        }
        QuestionOptionsConvert.INSTANCE.updateEntityFromReq(req, entity);
        boolean result = questionOptionsService.updateById(entity);
        if (result) {
            log.info("更新题目选项成功, ID: {}", id);
        } else {
            log.error("更新题目选项失败, ID: {}, 请求参数: {}", id, req);
        }
        return result;
    }

    /**
     * 删除题目选项
     *
     * @param id 题目选项ID
     * @return 是否删除成功
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteQuestionOption(Integer id) {
        log.info("开始删除题目选项, ID: {}", id);
        boolean result = questionOptionsService.removeById(id);
        if (result) {
            log.info("删除题目选项成功, ID: {}", id);
        } else {
            log.warn("删除题目选项失败, 可能不存在该ID的数据, ID: {}", id);
        }
        return result;
    }
}