package com.mvrent.carrental.controller;

import com.mvrent.carrental.model.AuditLog;
import com.mvrent.carrental.service.AuditService;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/** JSON API backing the read-only audit log viewer. */
@RestController
@RequestMapping("/admin/api/audit")
@RequiredArgsConstructor
@PreAuthorize("hasRole('ADMIN')")
public class AdminAuditApiController {

    private final AuditService auditService;

    @GetMapping
    public List<AuditLog> list(@RequestParam(defaultValue = "500") int limit) {
        return auditService.recent(Math.min(Math.max(limit, 1), 2000));
    }
}
