package gc.mda.kcg.domain.event;

import lombok.RequiredArgsConstructor;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.time.Instant;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/events")
@RequiredArgsConstructor
public class EventController {

    private final EventService eventService;

    @GetMapping
    public ResponseEntity<Map<String, Object>> getEvents(
            @RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) Instant from,
            @RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) Instant to) {
        Instant f = from != null ? from : Instant.parse("2026-03-01T00:00:00Z");
        Instant t = to != null ? to : Instant.now();
        List<EventDto> results = eventService.getByDateRange(f, t);
        return ResponseEntity.ok(Map.of("count", results.size(), "items", results));
    }

    @PostMapping("/import")
    public ResponseEntity<Map<String, Object>> importEvents(@RequestBody List<EventDto> events) {
        int imported = eventService.importEvents(events);
        return ResponseEntity.ok(Map.of("imported", imported, "total", events.size()));
    }
}
