//! Transaction type - represents a credit movement

use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use uuid::Uuid;

/// Transaction represents a credit movement
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Transaction {
    /// Unique transaction ID
    pub id: String,
    /// Project ID
    pub project_id: String,
    /// Transaction type
    pub transaction_type: TransactionType,
    /// Amount (positive for credit, negative for debit)
    pub amount: i64,
    /// Balance after transaction
    pub balance_after: i64,
    /// Description
    pub description: String,
    /// Related resource ID (optional)
    pub resource_id: Option<String>,
    /// Creation timestamp
    pub created_at: DateTime<Utc>,
}

impl Transaction {
    /// Create a new transaction
    pub fn new(
        project_id: String,
        transaction_type: TransactionType,
        amount: i64,
        balance_after: i64,
        description: String,
    ) -> Self {
        Self {
            id: Uuid::new_v4().to_string(),
            project_id,
            transaction_type,
            amount,
            balance_after,
            description,
            resource_id: None,
            created_at: Utc::now(),
        }
    }

    /// Set resource ID
    pub fn with_resource_id(mut self, resource_id: String) -> Self {
        self.resource_id = Some(resource_id);
        self
    }

    /// Create a new transaction with resource ID
    pub fn new_with_resource(
        project_id: String,
        transaction_type: TransactionType,
        amount: i64,
        balance_after: i64,
        description: String,
        resource_id: Option<String>,
    ) -> Self {
        Self {
            id: Uuid::new_v4().to_string(),
            project_id,
            transaction_type,
            amount,
            balance_after,
            description,
            resource_id,
            created_at: Utc::now(),
        }
    }
}

/// Transaction type
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum TransactionType {
    /// Credit addition
    TopUp,
    /// Temporary hold
    Reservation,
    /// Actual consumption
    Charge,
    /// Reservation release
    Release,
    /// Credit return
    Refund,
    /// Periodic billing charge
    BillingCharge,
}
