package com.honey.honey.controller;

import com.honey.honey.service.FeatureSwitchService;
import com.honey.honey.service.FeatureSwitchService.FeatureSwitchDto;
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.Map;

@RestController
@RequestMapping("/api/admin/feature-switches")
@RequiredArgsConstructor
@PreAuthorize("hasRole('ADMIN')")
public class AdminFeatureSwitchController {

    private final FeatureSwitchService featureSwitchService;

    @GetMapping
    public ResponseEntity<List<FeatureSwitchDto>> getAll() {
        return ResponseEntity.ok(featureSwitchService.getAll());
    }

    @PatchMapping("/{key}")
    public ResponseEntity<FeatureSwitchDto> update(
            @PathVariable String key,
            @RequestBody Map<String, Boolean> body) {
        Boolean enabled = body != null ? body.get("enabled") : null;
        if (enabled == null) {
            return ResponseEntity.badRequest().build();
        }
        return ResponseEntity.ok(featureSwitchService.setEnabled(key, enabled));
    }
}
