package com.gnx.telemedicine.controller;

import com.gnx.telemedicine.model.DataRetentionPolicy;
import com.gnx.telemedicine.service.DataRetentionService;
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.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

@RestController
@RequestMapping("/api/v3/data-retention")
@RequiredArgsConstructor
@Tag(name = "Data Retention", description = "Data retention policy management endpoints")
@SecurityRequirement(name = "bearerAuth")
public class DataRetentionController {

    private final DataRetentionService retentionService;

    @Operation(summary = "Get all retention policies", description = "Get all data retention policies (Admin only)")
    @PreAuthorize("hasRole('ADMIN')")
    @GetMapping("/policies")
    public ResponseEntity<List<DataRetentionPolicy>> getAllPolicies() {
        List<DataRetentionPolicy> policies = retentionService.getAllPolicies();
        return ResponseEntity.ok(policies);
    }

    @Operation(summary = "Get policy by data type", description = "Get retention policy for a specific data type")
    @PreAuthorize("hasRole('ADMIN')")
    @GetMapping("/policy")
    public ResponseEntity<DataRetentionPolicy> getPolicyByDataType(@RequestParam @NotNull String dataType) {
        Optional<DataRetentionPolicy> policy = retentionService.getPolicyByDataType(dataType);
        return policy.map(ResponseEntity::ok)
                .orElse(ResponseEntity.notFound().build());
    }

    @Operation(summary = "Create or update policy", description = "Create or update a data retention policy (Admin only)")
    @PreAuthorize("hasRole('ADMIN')")
    @PostMapping("/policy")
    public ResponseEntity<DataRetentionPolicy> createOrUpdatePolicy(
            @RequestParam @NotNull String dataType,
            @RequestParam @NotNull Integer retentionPeriodDays,
            @RequestParam(required = false) Boolean autoDeleteEnabled,
            @RequestParam(required = false) String legalRequirement) {
        
        DataRetentionPolicy policy = retentionService.createOrUpdatePolicy(
                dataType, retentionPeriodDays, autoDeleteEnabled, legalRequirement);
        return ResponseEntity.ok(policy);
    }
}

