package com.cameleer.server.app.alerting.storage;

import com.cameleer.server.core.alerting.AlertReadRepository;
import org.springframework.jdbc.core.JdbcTemplate;

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

public class PostgresAlertReadRepository implements AlertReadRepository {

    private final JdbcTemplate jdbc;

    public PostgresAlertReadRepository(JdbcTemplate jdbc) {
        this.jdbc = jdbc;
    }

    @Override
    public void markRead(String userId, UUID alertInstanceId) {
        jdbc.update("""
            INSERT INTO alert_reads (user_id, alert_instance_id)
            VALUES (?, ?)
            ON CONFLICT (user_id, alert_instance_id) DO NOTHING
            """, userId, alertInstanceId);
    }

    @Override
    public void bulkMarkRead(String userId, List<UUID> alertInstanceIds) {
        if (alertInstanceIds == null || alertInstanceIds.isEmpty()) {
            return;
        }
        for (UUID id : alertInstanceIds) {
            markRead(userId, id);
        }
    }
}
