package com.dne.ems.controller;

import java.util.List;

import org.springframework.http.ResponseEntity;
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.RestController;

import com.dne.ems.dto.PathfindingRequest;
import com.dne.ems.dto.Point;
import com.dne.ems.service.pathfinding.AStarService;

import lombok.RequiredArgsConstructor;

/**
 * 路径规划控制器，提供A*寻路算法功能
 *
 * <p>主要功能：
 * <ul>
 *   <li>计算两点之间的最优路径</li>
 *   <li>考虑地图障碍物</li>
 * </ul>
 *
 * @author DNE开发团队
 * @version 1.0
 * @since 2024
 */
@RestController
@RequestMapping("/api/pathfinding")
@RequiredArgsConstructor
public class PathfindingController {

    private final AStarService aStarService;

    /**
     * 使用A*算法查找两点之间的路径
     *
     * <p>实现细节：
     * <ol>
     *   <li>验证请求参数</li>
     *   <li>调用A*算法服务计算路径</li>
     *   <li>返回路径点列表</li>
     * </ol>
     *
     * @param request 包含起点和终点坐标的请求体
     * @return 包含路径点列表的响应实体，若无路径则返回空列表
     * @throws IllegalArgumentException 如果请求参数无效
     */
    @PostMapping("/find")
    public ResponseEntity<List<Point>> findPath(@RequestBody PathfindingRequest request) {
        if (request == null) {
            return ResponseEntity.badRequest().build();
        }
        Point start = new Point(request.getStartX(), request.getStartY());
        Point end = new Point(request.getEndX(), request.getEndY());

        List<Point> path = aStarService.findPath(start, end);
        return ResponseEntity.ok(path);
    }
} 