package com.ruoyi.web.controller.client;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.UserFeedback;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.ip.IpUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.service.IUserFeedbackService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * 用户反馈Controller
 * 
 * @author ruoyi
 * @date 2025-08-21
 */
@RestController
@RequestMapping("/client/feedback")
public class ClientFeedbackController extends BaseController
{
    @Autowired
    private IUserFeedbackService userFeedbackService;

    /**
     * 提交用户反馈
     */
    @PostMapping("/submit")
    public AjaxResult submit(@RequestBody UserFeedback userFeedback, HttpServletRequest request)
    {
        // 验证必填字段
        if (StringUtils.isEmpty(userFeedback.getFeedbackContent())) {
            return AjaxResult.error("反馈内容不能为空");
        }
        
        // 设置ID
        userFeedback.setId(UUID.randomUUID().toString().replaceAll("-", ""));
        
        // 设置IP地址
        userFeedback.setIpAddress(IpUtils.getIpAddr(request));
        
        // 设置用户浏览器信息
        userFeedback.setUserAgent(request.getHeader("User-Agent"));
        
        // 设置状态，如果未提供则默认为pending
        if (StringUtils.isEmpty(userFeedback.getStatus())) {
            userFeedback.setStatus("pending");
        } else {
            // 验证状态值是否合法
            String status = userFeedback.getStatus();
            if (!status.equals("pending") && !status.equals("processing") && 
                !status.equals("resolved") && !status.equals("closed")) {
                return AjaxResult.error("状态值无效，必须是 pending、processing、resolved 或 closed");
            }
        }
        
        // 设置创建时间和更新时间
        Date now = new Date();
        userFeedback.setCreatedAt(now);
        userFeedback.setUpdatedAt(now);
        
        return toAjax(userFeedbackService.insertUserFeedback(userFeedback));
    }
    
    /**
     * 查询用户反馈详情
     */
    @GetMapping("/detail/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return AjaxResult.success(userFeedbackService.selectUserFeedbackById(id));
    }
}