package de.cannamanage.service;

import de.cannamanage.domain.entity.NotificationPreference;
import de.cannamanage.domain.enums.NotificationChannel;
import de.cannamanage.service.repository.NotificationPreferenceRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.UUID;

/**
 * Manages per-user notification channel preferences.
 * IN_APP is always enabled and cannot be disabled.
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class NotificationPreferenceService {

    private final NotificationPreferenceRepository preferenceRepository;

    /**
     * Get preferences for a user, creating defaults if none exist.
     */
    @Transactional
    public List<NotificationPreference> getOrCreatePreferences(UUID userId) {
        var prefs = preferenceRepository.findByUserId(userId);
        if (prefs.isEmpty()) {
            prefs = createDefaultPreferences(userId);
        }
        return prefs;
    }

    /**
     * Update a single channel preference. IN_APP cannot be disabled.
     */
    @Transactional
    public NotificationPreference updatePreference(UUID userId, NotificationChannel channel, boolean enabled) {
        // IN_APP cannot be disabled
        if (channel == NotificationChannel.IN_APP && !enabled) {
            throw new IllegalArgumentException("IN_APP notifications cannot be disabled");
        }

        var pref = preferenceRepository.findByUserIdAndChannel(userId, channel)
                .orElseGet(() -> NotificationPreference.builder()
                        .userId(userId)
                        .channel(channel)
                        .enabled(false)
                        .build());

        pref.setEnabled(enabled);
        return preferenceRepository.save(pref);
    }

    /**
     * Check if a specific channel is enabled for a user.
     */
    @Transactional(readOnly = true)
    public boolean isChannelEnabled(UUID userId, NotificationChannel channel) {
        if (channel == NotificationChannel.IN_APP) return true;
        return preferenceRepository.findByUserIdAndChannel(userId, channel)
                .map(NotificationPreference::isEnabled)
                .orElse(false);
    }

    private List<NotificationPreference> createDefaultPreferences(UUID userId) {
        var defaults = List.of(
                NotificationPreference.builder().userId(userId).channel(NotificationChannel.IN_APP).enabled(true).build(),
                NotificationPreference.builder().userId(userId).channel(NotificationChannel.EMAIL).enabled(false).build(),
                NotificationPreference.builder().userId(userId).channel(NotificationChannel.WEB_PUSH).enabled(false).build(),
                NotificationPreference.builder().userId(userId).channel(NotificationChannel.MOBILE_PUSH).enabled(false).build()
        );
        return preferenceRepository.saveAll(defaults);
    }
}
