package com.zhentao.controller;

import com.zhentao.common.Result;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

/**
 * 头像测试控制器
 */
@RestController
@RequestMapping("/api/avatar/test")
@CrossOrigin(origins = "*")
public class AvatarTestController {
    
    @Value("${avatar.upload.path}")
    private String uploadPath;
    
    @Value("${avatar.url.prefix}")
    private String urlPrefix;
    
    /**
     * 测试配置是否正确
     */
    @GetMapping("/config")
    public Result<Map<String, Object>> testConfig() {
        Map<String, Object> result = new HashMap<>();
        
        result.put("uploadPath", uploadPath);
        result.put("urlPrefix", urlPrefix);
        
        // 检查目录是否存在
        File dir = new File(uploadPath);
        result.put("directoryExists", dir.exists());
        result.put("isDirectory", dir.isDirectory());
        result.put("canRead", dir.canRead());
        result.put("canWrite", dir.canWrite());
        
        // 列出目录中的文件
        if (dir.exists() && dir.isDirectory()) {
            File[] files = dir.listFiles();
            result.put("filesCount", files != null ? files.length : 0);
            if (files != null && files.length > 0) {
                String[] fileNames = new String[Math.min(10, files.length)];
                for (int i = 0; i < fileNames.length; i++) {
                    fileNames[i] = files[i].getName();
                }
                result.put("filesSample", fileNames);
            }
        }
        
        return Result.success("配置信息", result);
    }
    
    /**
     * 检查特定文件是否存在
     */
    @GetMapping("/check-file")
    public Result<Map<String, Object>> checkFile(@RequestParam String fileName) {
        Map<String, Object> result = new HashMap<>();
        
        File file = new File(uploadPath, fileName);
        result.put("fileName", fileName);
        result.put("fullPath", file.getAbsolutePath());
        result.put("exists", file.exists());
        result.put("isFile", file.isFile());
        result.put("size", file.length());
        result.put("canRead", file.canRead());
        
        return Result.success("文件检查结果", result);
    }
}

