<?php
function getCurrencySymbol($currency) {
    $symbols = [
        'PKR' => 'Rs',
        'USD' => '$',
        'EUR' => '€',
        'GBP' => '£',
        'AED' => 'د.إ',
        'SAR' => '﷼'
    ];
    return $symbols[$currency] ?? $currency;
}

function formatMoney($amount, $currency = 'PKR') {
    return getCurrencySymbol($currency) . ' ' . number_format($amount, 2);
}

function getStatusBadge($status) {
    $badges = [
        'draft' => 'bg-gray-200 text-gray-800',
        'sent' => 'bg-blue-100 text-blue-800',
        'paid' => 'bg-green-100 text-green-800',
        'partially_paid' => 'bg-yellow-100 text-yellow-800',
        'overdue' => 'bg-red-100 text-red-800',
        'accepted' => 'bg-green-100 text-green-800',
        'rejected' => 'bg-red-100 text-red-800'
    ];
    $class = $badges[$status] ?? 'bg-gray-200 text-gray-800';
    return "<span class='px-2 py-1 text-xs font-semibold rounded-full $class'>" . ucfirst(str_replace('_', ' ', $status)) . "</span>";
}

function sendEmail($to, $name, $subject, $message) {
    // If you have SMTP configured in config/mail.php, use it.
    // For now, just log (or return true to avoid errors)
    error_log("Email would be sent to $to: $subject");
    return true; // Remove this when you configure actual mail
}

function generateDocumentNumber($pdo, $companyId, $type) {
    $prefixes = [
        'invoice' => 'INV-',
        'estimate' => 'EST-',
        'quotation' => 'QUO-',
        'receipt' => 'REC-',
        'proforma' => 'PRO-'
    ];
    $prefix = $prefixes[$type] ?? 'DOC-';
    $year = date('Y');
    $stmt = $pdo->prepare("SELECT COUNT(*) as count FROM documents WHERE company_id = ? AND type = ? AND YEAR(created_at) = ?");
    $stmt->execute([$companyId, $type, $year]);
    $count = $stmt->fetch()['count'] + 1;
    return $prefix . $year . '-' . str_pad($count, 4, '0', STR_PAD_LEFT);
}
?>