package com.zhentao.service.impl;

import com.zhentao.dto.ReportCreateDTO;
import com.zhentao.entity.DynamicReports;
import com.zhentao.mapper.DynamicReportsMapper;
import com.zhentao.service.ReportService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.time.LocalDateTime;

/**
 * 举报服务实现（匹配数据库表结构）
 */
@Service
@RequiredArgsConstructor
public class ReportServiceImpl implements ReportService {
    
    private final DynamicReportsMapper reportsMapper;
    
    @Override
    public boolean submitReport(ReportCreateDTO dto) {
        if (dto.getDynamicId() == null || dto.getDynamicId() <= 0) {
            throw new IllegalArgumentException("dynamicId 不能为空");
        }
        if (dto.getReporterId() == null || dto.getReporterId() <= 0) {
            // 默认使用1号用户以兼容旧端，生产应从登录态获取
            dto.setReporterId(1);
        }
        // 创建举报记录
        DynamicReports report = new DynamicReports();
        report.setDynamicId(dto.getDynamicId());
        report.setReporterId(dto.getReporterId());
        
        // 直接使用字符串类型的举报原因
        report.setReportType(dto.getReportType());
        
        // 设置详细描述
        report.setDescription(dto.getDescription());
        
        // 设置联系方式
        report.setContact(dto.getContact());
        
        // 处理截图列表，使用逗号分隔
        List<String> screenshots = dto.getScreenshots();
        if (screenshots != null && !screenshots.isEmpty()) {
            report.setScreenshots(String.join(",", screenshots));
        }
        
        report.setStatus(0); // 待处理
        // 显式设置创建时间，避免数据库非空约束报错
        report.setCreatedAt(LocalDateTime.now());

        // 插入数据库
        return reportsMapper.insert(report) > 0;
    }
}

