package de.cannamanage.api.controller;

import de.cannamanage.api.dto.compliance.QuotaResponse;
import de.cannamanage.service.ComplianceService;
import de.cannamanage.service.dto.QuotaStatus;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.UUID;

@RestController
@RequestMapping("/api/v1/compliance")
@RequiredArgsConstructor
@Tag(name = "Compliance", description = "CanG §19 compliance quota checks")
public class ComplianceController {

    private final ComplianceService complianceService;

    @GetMapping("/quota/{memberId}")
    @Operation(summary = "Get member quota status",
            description = "Returns current monthly remaining quota for a member per CanG §19")
    @PreAuthorize("hasRole('ADMIN') or hasRole('MEMBER') or @staffPermissions.has(authentication, T(de.cannamanage.domain.enums.StaffPermission).VIEW_MEMBER_QUOTA)")
    public ResponseEntity<QuotaResponse> getQuotaStatus(@PathVariable UUID memberId) {
        QuotaStatus status = complianceService.getQuotaStatus(memberId);

        QuotaResponse response = new QuotaResponse(
                status.totalAllowed(),
                status.totalUsed(),
                status.remaining(),
                status.isUnder21(),
                status.year(),
                status.month()
        );

        return ResponseEntity.ok(response);
    }
}
