package com.mvrent.carrental.controller;

import com.mvrent.carrental.service.ReviewService;
import com.mvrent.carrental.web.ReviewDto;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/** JSON API backing the admin review moderation screen. */
@RestController
@RequestMapping("/admin/api/reviews")
@RequiredArgsConstructor
@PreAuthorize("hasRole('ADMIN') or hasAuthority('MANAGE_FLEET')")
public class AdminReviewApiController {

    private final ReviewService reviewService;

    @GetMapping
    public List<ReviewDto> list() {
        return reviewService.findAll().stream().map(ReviewDto::from).toList();
    }

    @PostMapping("/{id}/approve")
    public ResponseEntity<?> approve(@PathVariable Long id, @RequestParam boolean approved) {
        reviewService.setApproved(id, approved);
        return ResponseEntity.ok(Map.of("ok", true));
    }

    @PostMapping("/{id}/delete")
    public ResponseEntity<?> delete(@PathVariable Long id) {
        reviewService.delete(id);
        return ResponseEntity.ok(Map.of("ok", true));
    }
}
