package com.gnx.telemedicine.controller;

import com.gnx.telemedicine.dto.patient_safety.SentinelEventRequestDto;
import com.gnx.telemedicine.dto.patient_safety.SentinelEventResponseDto;
import com.gnx.telemedicine.dto.patient_safety.SentinelEventUpdateRequestDto;
import com.gnx.telemedicine.model.enums.SentinelEventStatus;
import com.gnx.telemedicine.service.SentinelEventReportingService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;

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

@RestController
@RequestMapping("/api/v3/sentinel-events")
@RequiredArgsConstructor
@Tag(name = "Sentinel Events", description = "Endpoints for reporting and managing sentinel events")
@SecurityRequirement(name = "bearerAuth")
public class SentinelEventController {
    
    private final SentinelEventReportingService sentinelEventReportingService;
    
    @Operation(summary = "Get all sentinel events")
    @GetMapping
    public ResponseEntity<List<SentinelEventResponseDto>> getAllSentinelEvents() {
        return ResponseEntity.ok(sentinelEventReportingService.getAllSentinelEvents());
    }
    
    @Operation(summary = "Get sentinel events by status")
    @GetMapping("/status/{status}")
    public ResponseEntity<List<SentinelEventResponseDto>> getSentinelEventsByStatus(@PathVariable SentinelEventStatus status) {
        return ResponseEntity.ok(sentinelEventReportingService.getSentinelEventsByStatus(status));
    }
    
    @Operation(summary = "Get active sentinel events")
    @GetMapping("/active")
    public ResponseEntity<List<SentinelEventResponseDto>> getActiveSentinelEvents() {
        return ResponseEntity.ok(sentinelEventReportingService.getActiveSentinelEvents());
    }
    
    @Operation(summary = "Get sentinel events by time range")
    @GetMapping("/time-range")
    public ResponseEntity<List<SentinelEventResponseDto>> getSentinelEventsByTimeRange(
            @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) Instant startDate,
            @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) Instant endDate) {
        return ResponseEntity.ok(sentinelEventReportingService.getSentinelEventsByTimeRange(startDate, endDate));
    }
    
    @Operation(summary = "Report a sentinel event")
    @PostMapping
    public ResponseEntity<SentinelEventResponseDto> reportSentinelEvent(
            Authentication authentication,
            @RequestBody @Valid SentinelEventRequestDto requestDto) {
        String userEmail = authentication.getName();
        return ResponseEntity.ok(sentinelEventReportingService.reportSentinelEvent(userEmail, requestDto));
    }
    
    @Operation(summary = "Update a sentinel event")
    @PutMapping("/{eventId}")
    public ResponseEntity<SentinelEventResponseDto> updateSentinelEvent(
            Authentication authentication,
            @PathVariable UUID eventId,
            @RequestBody @Valid SentinelEventUpdateRequestDto requestDto) {
        String userEmail = authentication.getName();
        return ResponseEntity.ok(sentinelEventReportingService.updateSentinelEvent(userEmail, eventId, requestDto));
    }
    
    @Operation(summary = "Get sentinel event by ID")
    @GetMapping("/{id}")
    public ResponseEntity<SentinelEventResponseDto> getSentinelEventById(@PathVariable UUID id) {
        return ResponseEntity.ok(sentinelEventReportingService.getSentinelEventById(id));
    }
}

