package com.gnx.telemedicine.controller;

import com.gnx.telemedicine.dto.patient_safety.DuplicatePatientRecordResponseDto;
import com.gnx.telemedicine.dto.patient_safety.DuplicatePatientReviewRequestDto;
import com.gnx.telemedicine.model.enums.DuplicateStatus;
import com.gnx.telemedicine.service.DuplicatePatientDetectionService;
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.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/duplicate-patients")
@RequiredArgsConstructor
@Tag(name = "Duplicate Patients", description = "Endpoints for managing duplicate patient detection")
@SecurityRequirement(name = "bearerAuth")
public class DuplicatePatientController {
    
    private final DuplicatePatientDetectionService duplicatePatientDetectionService;
    
    @Operation(summary = "Get all duplicate records")
    @GetMapping
    public ResponseEntity<List<DuplicatePatientRecordResponseDto>> getAllDuplicateRecords() {
        return ResponseEntity.ok(duplicatePatientDetectionService.getAllDuplicateRecords());
    }
    
    @Operation(summary = "Get duplicate records by status")
    @GetMapping("/status/{status}")
    public ResponseEntity<List<DuplicatePatientRecordResponseDto>> getDuplicateRecordsByStatus(@PathVariable DuplicateStatus status) {
        return ResponseEntity.ok(duplicatePatientDetectionService.getDuplicateRecordsByStatus(status));
    }
    
    @Operation(summary = "Get pending duplicate records")
    @GetMapping("/pending")
    public ResponseEntity<List<DuplicatePatientRecordResponseDto>> getPendingDuplicateRecords() {
        return ResponseEntity.ok(duplicatePatientDetectionService.getPendingDuplicateRecords());
    }
    
    @Operation(summary = "Get duplicates for a specific patient")
    @GetMapping("/patient/{patientId}")
    public ResponseEntity<List<DuplicatePatientRecordResponseDto>> getDuplicatesForPatient(@PathVariable UUID patientId) {
        return ResponseEntity.ok(duplicatePatientDetectionService.getDuplicatesForPatient(patientId));
    }
    
    @Operation(summary = "Review a duplicate record")
    @PostMapping("/{duplicateId}/review")
    public ResponseEntity<DuplicatePatientRecordResponseDto> reviewDuplicate(
            Authentication authentication,
            @PathVariable UUID duplicateId,
            @RequestBody @Valid DuplicatePatientReviewRequestDto requestDto) {
        String userEmail = authentication.getName();
        return ResponseEntity.ok(duplicatePatientDetectionService.reviewDuplicate(userEmail, duplicateId, requestDto));
    }
    
    @Operation(summary = "Get duplicate record by ID")
    @GetMapping("/{id}")
    public ResponseEntity<DuplicatePatientRecordResponseDto> getDuplicateRecordById(@PathVariable UUID id) {
        return ResponseEntity.ok(duplicatePatientDetectionService.getDuplicateRecordById(id));
    }
    
    @Operation(summary = "Scan for duplicate patients")
    @PostMapping("/scan")
    public ResponseEntity<Void> scanForDuplicates() {
        duplicatePatientDetectionService.scanForDuplicates();
        return ResponseEntity.ok().build();
    }
}

