package com.mvrent.carrental.controller;

import com.mvrent.carrental.service.TelematicsService;
import com.mvrent.carrental.web.VehicleLocationDto;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/admin/tracking")
@RequiredArgsConstructor
@PreAuthorize("hasRole('ADMIN') or hasAuthority('MANAGE_FLEET')")
public class AdminTelematicsController {

    private final TelematicsService telematicsService;

    @GetMapping
    public String page(Model model) {
        return "admin/tracking";
    }

    @GetMapping(value = "/data", produces = "application/json")
    @ResponseBody
    public List<VehicleLocationDto> data() {
        return telematicsService.findAll().stream().map(VehicleLocationDto::from).toList();
    }
}
