package com.iqser.red.persistence.service.v1.external.api.impl.controller;

import static com.iqser.red.service.persistence.management.v1.processor.roles.ActionRoles.READ_NOTIFICATIONS;
import static com.iqser.red.service.persistence.management.v1.processor.roles.ActionRoles.UPDATE_NOTIFICATIONS;


import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import com.knecon.fforesight.keycloakcommons.security.KeycloakSecurity;
import com.iqser.red.service.persistence.management.v1.processor.service.persistence.NotificationPreferencesPersistenceService;
import com.knecon.fforesight.databasetenantcommons.providers.utils.MagicConverter;
import com.iqser.red.service.persistence.service.v1.api.external.resource.NotificationPreferencesResource;
import com.iqser.red.service.persistence.service.v1.api.shared.model.notification.NotificationPreferences;

import lombok.RequiredArgsConstructor;

@RestController
@RequiredArgsConstructor
public class NotificationPreferencesController implements NotificationPreferencesResource {

    private final NotificationPreferencesPersistenceService notificationPreferencesPersistenceService;


    @Override
    @PreAuthorize("hasAuthority('" + READ_NOTIFICATIONS + "')")
    public NotificationPreferences getNotificationPreferences() {
        return notificationPreferencesPersistenceService.getOrCreateNotificationPreferences(KeycloakSecurity.getUserId());
    }


    @Override
    @PreAuthorize("hasAuthority('" + UPDATE_NOTIFICATIONS + "')")
    public void setNotificationPreferences(@RequestBody NotificationPreferences notificationPreferences) {

        notificationPreferencesPersistenceService.setNotificationPreference(KeycloakSecurity.getUserId(), notificationPreferences);
    }


    @Override
    @PreAuthorize("hasAuthority('" + UPDATE_NOTIFICATIONS + "')")
    public void deleteNotificationPreferences() {

        notificationPreferencesPersistenceService.deleteNotificationPreferences(KeycloakSecurity.getUserId());
    }

}
