package com.gnx.telemedicine.controller;

import com.gnx.telemedicine.dto.prescription.MedicationIntakeLogRequestDto;
import com.gnx.telemedicine.dto.prescription.MedicationIntakeLogResponseDto;
import com.gnx.telemedicine.service.MedicationIntakeLogService;
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.responses.ApiResponses;
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.web.bind.annotation.*;

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

@RestController
@RequestMapping("/api/v3/medication-intake-logs")
@RequiredArgsConstructor
@Tag(name = "Medication Intake Logs", description = "Endpoints for managing medication adherence logs")
@SecurityRequirement(name = "bearerAuth")
public class MedicationIntakeLogController {
    
    private final MedicationIntakeLogService medicationIntakeLogService;

    @Operation(
            summary = "Get medication intake logs by prescription ID",
            description = "Retrieve all medication intake logs for a specific prescription.",
            parameters = @Parameter(name = "prescriptionId", description = "Prescription ID", required = true)
    )
    @ApiResponses(value = {
            @ApiResponse(
                    responseCode = "200",
                    description = "List of intake logs retrieved successfully",
                    content = @Content(
                            mediaType = "application/json",
                            array = @ArraySchema(schema = @Schema(implementation = MedicationIntakeLogResponseDto.class))
                    )
            )
    })
    @GetMapping("/prescription/{prescriptionId}")
    public ResponseEntity<List<MedicationIntakeLogResponseDto>> getLogsByPrescriptionId(
            @PathVariable UUID prescriptionId) {
        return ResponseEntity.ok(medicationIntakeLogService.getLogsByPrescriptionId(prescriptionId));
    }

    @Operation(
            summary = "Get missed doses",
            description = "Retrieve all missed medication doses for a prescription."
    )
    @GetMapping("/prescription/{prescriptionId}/missed")
    public ResponseEntity<List<MedicationIntakeLogResponseDto>> getMissedDoses(
            @PathVariable UUID prescriptionId) {
        return ResponseEntity.ok(medicationIntakeLogService.getMissedDoses(prescriptionId));
    }

    @Operation(
            summary = "Get medication adherence rate",
            description = "Calculate the adherence rate (percentage) for a prescription."
    )
    @GetMapping("/prescription/{prescriptionId}/adherence")
    public ResponseEntity<Map<String, Object>> getAdherenceRate(
            @PathVariable UUID prescriptionId) {
        Long adherenceRate = medicationIntakeLogService.getAdherenceRate(prescriptionId);
        return ResponseEntity.ok(Map.of("prescriptionId", prescriptionId, "adherenceRate", adherenceRate));
    }

    @Operation(
            summary = "Create a medication intake log",
            description = "Create a new medication intake log entry.",
            requestBody = @io.swagger.v3.oas.annotations.parameters.RequestBody(
                    description = "Intake log details",
                    required = true,
                    content = @Content(schema = @Schema(implementation = MedicationIntakeLogRequestDto.class))
            )
    )
    @PostMapping
    public ResponseEntity<MedicationIntakeLogResponseDto> createIntakeLog(
            @RequestBody @Valid MedicationIntakeLogRequestDto requestDto) {
        MedicationIntakeLogResponseDto response = medicationIntakeLogService.createIntakeLog(requestDto);
        return ResponseEntity.ok(response);
    }

    @Operation(
            summary = "Mark dose as taken",
            description = "Mark a scheduled medication dose as taken."
    )
    @PatchMapping("/{id}/mark-taken")
    public ResponseEntity<MedicationIntakeLogResponseDto> markDoseAsTaken(@PathVariable UUID id) {
        MedicationIntakeLogResponseDto response = medicationIntakeLogService.markDoseAsTaken(id);
        return ResponseEntity.ok(response);
    }

    @Operation(
            summary = "Get intake log by ID",
            description = "Retrieve a specific medication intake log by its ID."
    )
    @GetMapping("/{id}")
    public ResponseEntity<MedicationIntakeLogResponseDto> getIntakeLogById(@PathVariable UUID id) {
        return ResponseEntity.ok(medicationIntakeLogService.getIntakeLogById(id));
    }

    @Operation(
            summary = "Delete an intake log",
            description = "Delete a medication intake log by ID."
    )
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteIntakeLog(@PathVariable UUID id) {
        medicationIntakeLogService.deleteIntakeLog(id);
        return ResponseEntity.noContent().build();
    }
}

