package edu.prlab.tyler.iotgateway.cloud.controllers;

import edu.prlab.tyler.iotgateway.cloud.pojo.auth.SensitiveUser;
import edu.prlab.tyler.iotgateway.cloud.pojo.privacy.PrivacyChoice;
import edu.prlab.tyler.iotgateway.cloud.services.PrivacyChoiceService;
import edu.prlab.tyler.iotgateway.cloud.services.SensitiveUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Optional;

@RestController
public class GatewayController {
    private PrivacyChoiceService privacyChoiceService;
    private SensitiveUserService sensitiveUserService;

    @Autowired
    public GatewayController(PrivacyChoiceService privacyChoiceService,
                             SensitiveUserService sensitiveUserService) {
        this.privacyChoiceService = privacyChoiceService;
        this.sensitiveUserService = sensitiveUserService;
    }

    @PostMapping("/choice")
    public ResponseEntity<PrivacyChoice> setPrivacyChoice(@RequestBody PrivacyChoice privacyChoice) {
        return privacyChoiceService.add(privacyChoice)
                .map(ResponseEntity::ok)
                .orElseGet(() -> ResponseEntity.noContent().build());
    }

    @GetMapping("/choice")
    public ResponseEntity<Iterable<PrivacyChoice>> readPrivacyChoice() {
        return Optional.of(privacyChoiceService.readll())
                .map(ResponseEntity::ok)
                .orElseGet(() -> ResponseEntity.noContent().build());
    }
}
