/**
 * @file Transaction.java
 * @brief Definition of the Transaction data model and its Kafka Serializer.
 */

import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.kafka.common.serialization.Serializer;

/**
 * @class Transaction
 * @brief Data model representing a financial purchase event for the producer service.
 *
 * In the Banking API service, this class is used to model incoming raw 
 * transactions. It provides the structure required to perform fraud detection 
 * and is then serialized into JSON format to be published to Kafka topics.
 */
public class Transaction {
    
    /** @brief The unique identifier of the user initiating the purchase. */
    private String user;
    
    /** @brief The monetary value of the purchase transaction. */
    private double amount;
    
    /** @brief The geographical location (country) where the purchase was made. */
    private String transactionLocation;

    /**
     * @brief Constructor to initialize a Transaction with specific data.
     * 
     * @param user The customer's username or ID.
     * @param amount The cost of the items purchased.
     * @param transactionLocation The country where the credit card was used.
     */
    public Transaction(String user, double amount, String transactionLocation) {
        this.user = user;
        this.amount = amount;
        this.transactionLocation = transactionLocation;
    }

    /**
     * @brief Gets the username associated with this transaction.
     * @return String The user identifier.
     */
    public String getUser() {
        return user;
    }

    /**
     * @brief Gets the monetary amount of the transaction.
     * @return double The transaction value.
     */
    public double getAmount() {
        return amount;
    }

    /**
     * @brief Gets the location of the transaction.
     * @return String The country name/code.
     */
    public String getTransactionLocation() {
        return transactionLocation;
    }

    /**
     * @brief Provides a string-based representation of the transaction data.
     * @return String A formatted debug string containing user, amount, and location.
     */
    @Override
    public String toString() {
        return "Transaction{" +
                "user='" + user + '\'' +
                ", amount=" + amount +
                ", transactionLocation='" + transactionLocation + '\'' +
                '}';
    }

    /**
     * @class TransactionSerializer
     * @brief Custom Kafka Serializer implementation for the @ref Transaction class.
     *
     * This inner class allows Kafka Producers to convert high-level Java 
     * @ref Transaction objects into JSON byte arrays that can be transmitted 
     * across the network to Kafka brokers.
     * 
     * @implements Serializer<Transaction>
     */
    public static class TransactionSerializer implements Serializer<Transaction> {
        
        /**
         * @brief Converts a Transaction object into a JSON byte array.
         * 
         * @details Uses the Jackson @c ObjectMapper to perform the conversion. 
         * If serialization fails, the method logs the error and returns @c null.
         * 
         * @param topic The name of the destination Kafka topic.
         * @param data The Transaction instance to be serialized.
         * @return byte[] The JSON byte array representation of the object.
         */
        @Override
        public byte[] serialize(String topic, Transaction data) {
            byte[] serializedData = null;
            ObjectMapper objectMapper = new ObjectMapper();
            try {
                serializedData = objectMapper.writeValueAsString(data).getBytes();
            } catch (Exception e) {
                // Log serialization failures
                e.printStackTrace();
            }
            return serializedData;
        }
    }
}