package de.assecutor.votianlt.controller;

import de.assecutor.votianlt.model.LocationPosition;
import de.assecutor.votianlt.service.LocationService;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.Instant;

/**
 * REST-Controller für Location-bezogene API-Endpunkte.
 */
@RestController
@RequestMapping("/api/location")
@RequiredArgsConstructor
@Slf4j
public class LocationApiController {

    private final LocationService locationService;

    /**
     * Gibt die aktuelle Position eines App-Nutzers zurück.
     *
     * @param appUserId
     *            die ID des App-Nutzers
     * @return die aktuelle Position oder 404 wenn keine vorhanden
     */
    @GetMapping("/{appUserId}")
    public ResponseEntity<LocationResponse> getCurrentPosition(@PathVariable String appUserId) {
        LocationPosition position = locationService.getLatestPosition(appUserId);

        if (position == null || position.getLatitude() == null || position.getLongitude() == null) {
            return ResponseEntity.notFound().build();
        }

        LocationResponse response = new LocationResponse();
        response.setLatitude(position.getLatitude());
        response.setLongitude(position.getLongitude());
        response.setAccuracy(position.getAccuracy());
        response.setSpeed(position.getSpeed());
        response.setTimestamp(position.getTimestamp());

        return ResponseEntity.ok(response);
    }

    @Data
    public static class LocationResponse {
        private Double latitude;
        private Double longitude;
        private Double accuracy;
        private Double speed;
        private Instant timestamp;
    }
}
