package com.dne.ems.controller;

import java.util.List;

import org.springframework.http.ResponseEntity;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.dne.ems.model.MapGrid;
import com.dne.ems.repository.MapGridRepository;

import lombok.RequiredArgsConstructor;

/**
 * 地图控制器，处理地图网格数据的操作
 *
 * <p>主要功能包括：
 * <ul>
 *   <li>获取完整地图网格数据</li>
 *   <li>创建或更新网格单元</li>
 *   <li>初始化地图</li>
 * </ul>
 *
 * @author DNE开发团队
 * @version 1.0
 * @since 2024
 */
@RestController
@RequestMapping("/api/map")
@RequiredArgsConstructor
public class MapController {

    private final MapGridRepository mapGridRepository;

    /**
     * 获取完整地图网格数据
     *
     * @return 包含所有地图网格数据的响应实体
     */
    @GetMapping("/grid")
    public ResponseEntity<List<MapGrid>> getFullMap() {
        List<MapGrid> mapGrids = mapGridRepository.findAllByOrderByYAscXAsc();
        return ResponseEntity.ok(mapGrids);
    }

    /**
     * 创建或更新网格单元
     *
     * @param gridCellRequest 包含网格单元数据的请求体
     * @return 包含保存后的网格单元的响应实体
     * @throws IllegalArgumentException 如果坐标值为负数
     */
    @PostMapping("/grid")
    @Transactional
    public ResponseEntity<MapGrid> createOrUpdateGridCell(@RequestBody MapGrid gridCellRequest) {
        // Simple validation
        if (gridCellRequest.getX() < 0 || gridCellRequest.getY() < 0) {
            return ResponseEntity.badRequest().build();
        }

        MapGrid gridCell = mapGridRepository.findByXAndY(gridCellRequest.getX(), gridCellRequest.getY())
                .orElse(new MapGrid());

        gridCell.setX(gridCellRequest.getX());
        gridCell.setY(gridCellRequest.getY());
        gridCell.setObstacle(gridCellRequest.isObstacle());
        gridCell.setTerrainType(gridCellRequest.getTerrainType());
        
        MapGrid savedCell = mapGridRepository.save(gridCell);
        return ResponseEntity.ok(savedCell);
    }

    /**
     * 初始化地图
     *
     * @param width 地图宽度(默认20)
     * @param height 地图高度(默认20)
     * @return 包含初始化结果的响应实体
     */
    @PostMapping("/initialize")
    @Transactional
    public ResponseEntity<String> initializeMap(
            @RequestParam(defaultValue = "20") int width,
            @RequestParam(defaultValue = "20") int height) {
        
        mapGridRepository.deleteAll(); // Clear existing map

        for (int y = 0; y < height; y++) {
            for (int x = 0; x < width; x++) {
                MapGrid cell = MapGrid.builder()
                        .x(x)
                        .y(y)
                        .isObstacle(false)
                        .terrainType("ground")
                        .build();
                mapGridRepository.save(cell);
            }
        }
        return ResponseEntity.ok("Initialized a " + width + "x" + height + " map.");
    }
} 