package org.dromara.main.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.exception.ServiceException;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.main.domain.MainBackCandidate;
import org.dromara.main.domain.MainPostCandidateHireAttachment;
import org.dromara.main.domain.bo.MainPostCandidateHireBo;
import org.dromara.main.domain.bo.MainPostCandidateQueryBo;
import org.dromara.main.domain.bo.MainPostCandidateStatusBo;
import org.dromara.main.domain.vo.MainPostCandidateVo;
import org.dromara.main.mapper.MainBackCandidateMapper;
import org.dromara.main.mapper.MainPostCandidateHireAttachmentMapper;
import org.dromara.main.service.IMainPostCandidateService;
import org.springframework.stereotype.Service;

import java.util.List;

@RequiredArgsConstructor
@Service
public class MainPostCandidateServiceImpl implements IMainPostCandidateService {

    private static final String ENTERPRISE_STATUS_ADOPTED = "adopted";
    private static final String ENTERPRISE_STATUS_REJECTED = "rejected";
    private static final String ENTERPRISE_STATUS_PENDING = "pending";

    private final MainBackCandidateMapper mainBackCandidateMapper;
    private final MainPostCandidateHireAttachmentMapper hireAttachmentMapper;

    @Override
    public TableDataInfo<MainPostCandidateVo> queryPageList(MainPostCandidateQueryBo bo, PageQuery pageQuery) {
        if (bo.getPostId() == null) {
            throw new ServiceException("岗位ID不能为空");
        }
        IPage<MainPostCandidateVo> page = mainBackCandidateMapper.selectPostCandidatePage(pageQuery.build(), bo);
        return TableDataInfo.build(page);
    }

    @Override
    public List<MainPostCandidateVo> queryList(MainPostCandidateQueryBo bo) {
        if (bo.getPostId() == null) {
            throw new ServiceException("岗位ID不能为空");
        }
        return mainBackCandidateMapper.selectPostCandidateList(bo);
    }

    @Override
    public Boolean hireCandidate(MainPostCandidateHireBo bo) {
        if (bo.getId() == null) {
            throw new ServiceException("报名记录ID不能为空");
        }
        if (bo.getOfferAttachment() == null || bo.getOfferAttachment().isBlank()) {
            throw new ServiceException("请先上传录用附件");
        }
        MainBackCandidate entity = new MainBackCandidate();
        entity.setId(bo.getId());
        entity.setEnterpriseStatus(ENTERPRISE_STATUS_ADOPTED);
        entity.setRemark(bo.getRemark());
        boolean updated = mainBackCandidateMapper.updateById(entity) > 0;
        if (!updated) {
            return false;
        }

        MainPostCandidateHireAttachment attachment = hireAttachmentMapper.selectOne(
            Wrappers.<MainPostCandidateHireAttachment>lambdaQuery()
                .eq(MainPostCandidateHireAttachment::getCandidateId, bo.getId())
                .last("limit 1")
        );
        if (attachment == null) {
            attachment = new MainPostCandidateHireAttachment();
            attachment.setCandidateId(bo.getId());
            attachment.setOssIds(bo.getOfferAttachment());
            return hireAttachmentMapper.insert(attachment) > 0;
        }
        attachment.setOssIds(bo.getOfferAttachment());
        return hireAttachmentMapper.updateById(attachment) > 0;
    }

    @Override
    public Boolean updateStatus(MainPostCandidateStatusBo bo) {
        if (bo.getId() == null) {
            throw new ServiceException("报名记录ID不能为空");
        }
        if (bo.getStatus() == null || bo.getStatus().isBlank()) {
            throw new ServiceException("状态不能为空");
        }
        String normalizedStatus = normalizeStatus(bo.getStatus());
        if (normalizedStatus == null) {
            throw new ServiceException("状态值不合法");
        }
        MainBackCandidate entity = new MainBackCandidate();
        entity.setId(bo.getId());
        entity.setEnterpriseStatus(normalizedStatus);
        entity.setRemark(bo.getRemark());
        return mainBackCandidateMapper.updateById(entity) > 0;
    }

    private String normalizeStatus(String status) {
        return switch (status.trim()) {
            case ENTERPRISE_STATUS_ADOPTED, "0" -> ENTERPRISE_STATUS_ADOPTED;
            case ENTERPRISE_STATUS_REJECTED, "1" -> ENTERPRISE_STATUS_REJECTED;
            case ENTERPRISE_STATUS_PENDING, "2" -> ENTERPRISE_STATUS_PENDING;
            default -> null;
        };
    }
}
