package org.example.controller;

import org.example.entity.Hospital;
import org.example.service.HospitalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

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

/**
 * 医院信息控制器
 */
@RestController
@RequestMapping("/api/hospital")
public class HospitalController {

    @Autowired
    private HospitalService hospitalService;

    @Value("${file.upload-path:uploads/}")
    private String uploadPath;

    /**
     * 获取医院信息
     */
    @GetMapping("/info")
    public Map<String, Object> getHospital() {
        Map<String, Object> result = new HashMap<>();
        Hospital hospital = hospitalService.getHospital();
        result.put("code", 200);
        result.put("data", hospital);
        return result;
    }

    /**
     * 保存医院信息
     */
    @PostMapping("/save")
    public Map<String, Object> saveHospital(@RequestBody Hospital hospital) {
        Map<String, Object> result = new HashMap<>();
        try {
            hospitalService.saveHospital(hospital);
            result.put("code", 200);
            result.put("message", "保存成功");
        } catch (Exception e) {
            result.put("code", 500);
            result.put("message", "保存失败：" + e.getMessage());
        }
        return result;
    }

    /**
     * 上传Logo
     */
    @PostMapping("/upload-logo")
    public Map<String, Object> uploadLogo(@RequestParam("file") MultipartFile file) {
        Map<String, Object> result = new HashMap<>();
        
        if (file.isEmpty()) {
            result.put("code", 500);
            result.put("message", "请选择文件");
            return result;
        }

        // 检查文件大小（10MB）
        if (file.getSize() > 10 * 1024 * 1024) {
            result.put("code", 500);
            result.put("message", "文件大小不能超过10M");
            return result;
        }

        // 检查文件类型
        String originalFilename = file.getOriginalFilename();
        String suffix = originalFilename.substring(originalFilename.lastIndexOf(".")).toLowerCase();
        if (!".jpg".equals(suffix) && !".png".equals(suffix) && !".jpeg".equals(suffix)) {
            result.put("code", 500);
            result.put("message", "只支持jpg、png格式图片");
            return result;
        }

        try {
            // 创建上传目录
            File uploadDir = new File(uploadPath);
            if (!uploadDir.exists()) {
                uploadDir.mkdirs();
            }

            // 生成新文件名
            String newFileName = UUID.randomUUID().toString() + suffix;
            File destFile = new File(uploadPath + newFileName);
            file.transferTo(destFile);

            result.put("code", 200);
            result.put("message", "上传成功");
            // 返回前端可直接访问的路径
            result.put("data", "/img/" + newFileName);
        } catch (IOException e) {
            result.put("code", 500);
            result.put("message", "上传失败：" + e.getMessage());
        }

        return result;
    }
}
