import org.apache.kafka.clients.consumer.Consumer;
import org.apache.kafka.clients.consumer.ConsumerConfig;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.KafkaConsumer;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

import org.apache.kafka.common.serialization.StringDeserializer;

import java.time.Duration;
import java.util.Collections;
import java.util.Properties;

public class Application {

    private static final String SUSPICIOUS_TRANSACTIONS_TOPIC = "suspicious-transactions";
    private static final String BOOTSTRAP_SERVERS = "localhost:9092,localhost:9093,localhost:9094";

    public static void main(String[] args) {
        String consumerGroup = "user-notification-service";

        System.out.println("Consumer is part of consumer group " + consumerGroup);

        Consumer<String, Transaction> kafkaConsumer = createKafkaConsumer(BOOTSTRAP_SERVERS, consumerGroup);

        consumeMessages(SUSPICIOUS_TRANSACTIONS_TOPIC, kafkaConsumer);
    }

    public static void consumeMessages(String topic, Consumer<String, Transaction> kafkaConsumer) {

      
        kafkaConsumer.subscribe(Collections.singletonList(topic));

  
        while (true) {
            ConsumerRecords<String, Transaction> records = kafkaConsumer.poll(Duration.ofMillis(100));

            for (ConsumerRecord<String, Transaction> record : records) {
                Transaction transaction = record.value();

                sendUserNotification(transaction);
            }
        }
    }

    public static Consumer<String, Transaction> createKafkaConsumer(String bootstrapServers, String consumerGroup) {
        Properties properties = new Properties();

        properties.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers);

        
        properties.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName());
        properties.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, Transaction.TransactionDeserializer.class.getName());

        properties.put(ConsumerConfig.GROUP_ID_CONFIG, consumerGroup);
        properties.put(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG, "true");

        return new KafkaConsumer<>(properties);
    }

    private static void sendUserNotification(Transaction transaction) {

        String message = String.format(
                "Sending user %s notification about a suspicious transaction of $%.2f in their account originating in %s",
                transaction.getUser(),
                transaction.getAmount(),
                transaction.getTransactionLocation()
        );
    
        System.out.println(message);
    
        try {
    
            
            Path notificationsDir = Paths.get("..", "notifications");
    
            
            if (!Files.exists(notificationsDir)) {
                Files.createDirectories(notificationsDir);
            }
    
            
            String fileName = transaction.getUser() + ".txt";
            Path userFile = notificationsDir.resolve(fileName);
    
            
            try (FileWriter writer = new FileWriter(userFile.toFile(), true)) {
                writer.write(message + System.lineSeparator());
            }
    
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}