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

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

/**
 * @class Transaction
 * @brief Data model representing a financial purchase event.
 *
 * This class is shared across all microservices (Banking API, Account Manager, 
 * Notification, and Reporting) to represent a credit card transaction.
 */
public class Transaction {
    
    /** @brief The unique identifier or username of the bank customer. */
    private String user;
    
    /** @brief The monetary amount of the purchase. */
    private double amount;
    
    /** @brief The country or location where the transaction was initiated. */
    private String transactionLocation;

    /**
     * @brief Default constructor.
     * Required by Jackson ObjectMapper for JSON deserialization.
     */
    public Transaction() {}

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

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

    /**
     * @brief Gets the location where the transaction occurred.
     * @return String The transaction location.
     */
    public String getTransactionLocation() {
        return transactionLocation;
    }

    /**
     * @brief Sets the username for the transaction.
     * @param user The user ID to set.
     */
    public void setUser(String user) {
        this.user = user;
    }

    /**
     * @brief Sets the monetary amount for the transaction.
     * @param amount The value to set.
     */
    public void setAmount(double amount) {
        this.amount = amount;
    }

    /**
     * @brief Sets the location for the transaction.
     * @param transactionLocation The country/location string.
     */
    public void setTransactionLocation(String transactionLocation) {
        this.transactionLocation = transactionLocation;
    }

    /**
     * @brief Returns a string representation of the Transaction object.
     * @return String Formatted transaction details.
     */
    @Override
    public String toString() {
        return "Transaction{" +
                "user='" + user + '\'' +
                ", amount=" + amount +
                ", transactionLocation='" + transactionLocation + '\'' +
                '}';
    }

    /**
     * @class TransactionDeserializer
     * @brief Custom Kafka Deserializer implementation for Transaction objects.
     *
     * This inner class provides the logic to convert raw JSON bytes received 
     * from a Kafka topic back into a Java @ref Transaction object.
     */
    public static class TransactionDeserializer implements Deserializer<Transaction> {

        /**
         * @brief Deserializes a byte array into a Transaction object using Jackson.
         * @param topic The name of the Kafka topic.
         * @param data The raw byte data from Kafka.
         * @return Transaction The deserialized object, or null if an error occurs.
         */
        @Override
        public Transaction deserialize(String topic, byte[] data) {
            ObjectMapper mapper = new ObjectMapper();
            Transaction transaction = null;
            try {
                transaction = mapper.readValue(data, Transaction.class);
            } catch (Exception e) {
                // In production, consider logging this via a proper Logger (SLF4J)
                e.printStackTrace();
            }
            return transaction;
        }
    }
}