package com.gnx.telemedicine.controller;

import com.gnx.telemedicine.dto.audit.BreachNotificationRequestDto;
import com.gnx.telemedicine.dto.audit.BreachNotificationResponseDto;
import com.gnx.telemedicine.model.enums.BreachStatus;
import com.gnx.telemedicine.service.BreachNotificationService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.ArraySchema;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
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.http.ResponseEntity;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/api/v3/audit/breaches")
@RequiredArgsConstructor
@Tag(name = "Breach Notifications", description = "Endpoints for managing HIPAA breach notifications (Admin only)")
@SecurityRequirement(name = "bearerAuth")
public class BreachNotificationController {
    
    private final BreachNotificationService breachNotificationService;

    @Operation(
            summary = "Get all breach notifications",
            description = "Retrieve all breach notifications. Admin only.",
            responses = @ApiResponse(
                    responseCode = "200",
                    description = "List of breach notifications retrieved successfully",
                    content = @Content(
                            mediaType = "application/json",
                            array = @ArraySchema(schema = @Schema(implementation = BreachNotificationResponseDto.class))
                    )
            )
    )
    @GetMapping
    public ResponseEntity<List<BreachNotificationResponseDto>> getAllBreachNotifications() {
        return ResponseEntity.ok(breachNotificationService.getAllBreachNotifications());
    }

    @Operation(
            summary = "Get breach notifications by status",
            description = "Retrieve breach notifications filtered by status."
    )
    @GetMapping("/status/{status}")
    public ResponseEntity<List<BreachNotificationResponseDto>> getBreachNotificationsByStatus(
            @PathVariable BreachStatus status) {
        return ResponseEntity.ok(breachNotificationService.getBreachNotificationsByStatus(status));
    }

    @Operation(
            summary = "Create a breach notification",
            description = "Create a new breach notification. Admin only.",
            requestBody = @io.swagger.v3.oas.annotations.parameters.RequestBody(
                    description = "Breach notification details",
                    required = true,
                    content = @Content(schema = @Schema(implementation = BreachNotificationRequestDto.class))
            )
    )
    @PostMapping
    public ResponseEntity<BreachNotificationResponseDto> createBreachNotification(
            Authentication authentication,
            @RequestBody @Valid BreachNotificationRequestDto requestDto) {
        String userEmail = authentication.getName();
        BreachNotificationResponseDto response = breachNotificationService.createBreachNotification(userEmail, requestDto);
        return ResponseEntity.ok(response);
    }

    @Operation(
            summary = "Update breach notification status",
            description = "Update the status of a breach notification."
    )
    @PatchMapping("/{id}/status")
    public ResponseEntity<BreachNotificationResponseDto> updateBreachStatus(
            @PathVariable UUID id,
            @RequestParam BreachStatus status) {
        BreachNotificationResponseDto response = breachNotificationService.updateBreachStatus(id, status);
        return ResponseEntity.ok(response);
    }

    @Operation(
            summary = "Get breach notification by ID",
            description = "Retrieve a specific breach notification by its ID."
    )
    @GetMapping("/{id}")
    public ResponseEntity<BreachNotificationResponseDto> getBreachNotificationById(@PathVariable UUID id) {
        return ResponseEntity.ok(breachNotificationService.getBreachNotificationById(id));
    }
}

