package research.loghunter.controller;

import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import research.loghunter.dto.SettingDto;
import research.loghunter.service.SettingService;

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

@RestController
@RequestMapping("/api/settings")
@RequiredArgsConstructor
public class SettingController {

    private final SettingService settingService;

    @GetMapping
    public ResponseEntity<List<SettingDto>> findAll() {
        return ResponseEntity.ok(settingService.findAll());
    }

    @GetMapping("/map")
    public ResponseEntity<Map<String, String>> findAllAsMap() {
        return ResponseEntity.ok(settingService.findAllAsMap());
    }

    @GetMapping("/{key}")
    public ResponseEntity<String> getValue(@PathVariable String key) {
        String value = settingService.getValue(key);
        return value != null ? ResponseEntity.ok(value) : ResponseEntity.notFound().build();
    }

    @PostMapping
    public ResponseEntity<SettingDto> save(@RequestBody SettingDto dto) {
        return ResponseEntity.ok(settingService.save(dto));
    }

    @PutMapping
    public ResponseEntity<Void> saveAll(@RequestBody Map<String, String> settings) {
        settingService.saveAll(settings);
        return ResponseEntity.ok().build();
    }

    @DeleteMapping("/{key}")
    public ResponseEntity<Void> delete(@PathVariable String key) {
        settingService.delete(key);
        return ResponseEntity.ok().build();
    }
}
