<?php
/**
 * Currency Trait
 *
 * @package Easy_Invoice
 * @subpackage Traits
 */

namespace EasyInvoice\Traits;

use EasyInvoice\Controllers\SettingsController;
use EasyInvoice\Helpers\CurrencyHelper;

/**
 * CurrencyTrait contains methods for handling currency formatting
 */
trait CurrencyTrait {
    /**
     * Format currency amount
     * 
     * @param float $amount Amount to format
     * @param mixed $invoice Invoice object or null (to use invoice-specific currency settings)
     * @param string $currency_symbol Fallback currency symbol
     * @return string Formatted amount
     */
    protected function formatCurrency($amount, $invoice = null, $currency_symbol = '$') {
        $settings_controller = new SettingsController();
        $settings = $settings_controller->getSettings();
        
        // Get currency settings - prioritize invoice-specific settings over global settings
        $currency_code = null;
        $currency_position = null;
        
        if ($invoice && method_exists($invoice, 'getCurrencyCode') && method_exists($invoice, 'getCurrencyPosition')) {
            $currency_code = $invoice->getCurrencyCode();
            $currency_position = $invoice->getCurrencyPosition();
        }
        
        // If invoice doesn't have currency settings or they're empty, use global settings
        if (empty($currency_code) || $currency_code === 'global') {
            $currency_code = $settings['easy_invoice_currency_code'] ?? 'USD';
        }
        
        if (empty($currency_position) || $currency_position === 'global') {
            $currency_position = $settings['easy_invoice_currency_position'] ?? 'before';
        }
        
        // Ensure currency code is uppercase for consistent lookup
        $currency_code = strtoupper($currency_code);
        
        // Get currency symbol from CurrencyHelper based on currency code
        $symbol = CurrencyHelper::getCurrencySymbol($currency_code);
        
        // Get currency symbol type setting
        $currency_symbol_type = $settings['easy_invoice_currency_symbol_type'] ?? 'symbol';
        
        // Use currency code instead of symbol if setting is 'code'
        $display_symbol = ($currency_symbol_type === 'code') ? $currency_code : $symbol;
        
        // Get other formatting settings from global settings
        $thousands_separator = $settings['easy_invoice_thousands_separator'] ?? ',';
        $decimal_separator = $settings['easy_invoice_decimal_separator'] ?? '.';
        $precision = intval($settings['easy_invoice_decimal_precision'] ?? 2);
        
        $formatted = number_format(
            floatval($amount),
            $precision,
            $decimal_separator,
            $thousands_separator
        );
        
        // Handle different currency positions
        switch ($currency_position) {
            case 'left':
            case 'before':
                return $display_symbol . $formatted;
            case 'right':
            case 'after':
                return $formatted . $display_symbol;
            case 'left_space':
                return $display_symbol . ' ' . $formatted;
            case 'right_space':
                return $formatted . ' ' . $display_symbol;
            default:
                return $display_symbol . $formatted; // Default to left
        }
    }
} 