<?php
declare(strict_types=1);

namespace App\Helpers;

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception as MailException;

class InvoiceEmailHelper
{
    private static function configureMailer(): PHPMailer
    {
        $mail = new PHPMailer(true);
        // Use environment variables or tenant settings in real implementation
        $mail->isSMTP();
        $mail->Host       = $_SERVER['MAIL_HOST'] ?? getenv('MAIL_HOST') ?: 'smtpout.secureserver.net';
        $mail->SMTPAuth   = true;
        $mail->Username   = $_SERVER['MAIL_USER'] ?? getenv('MAIL_USER') ?: 'noreply@lomcos.com';
        $mail->Password   = $_SERVER['MAIL_PASS'] ?? getenv('MAIL_PASS') ?: 'Krishna@8';
        $mail->SMTPSecure = ($_SERVER['MAIL_PORT'] ?? getenv('MAIL_PORT') == 465) ? PHPMailer::ENCRYPTION_SMTPS : PHPMailer::ENCRYPTION_STARTTLS;
        $mail->Port       = (int)($_SERVER['MAIL_PORT'] ?? getenv('MAIL_PORT') ?: 465);

        return $mail;
    }

    public static function sendInvoice(array $invoice, array $customer, array $tenant, string $pdfPath): bool
    {
        try {
            $mail = self::configureMailer();
            $mail->setFrom($tenant['email'] ?? 'billing@saas.com', $tenant['business_name'] ?? 'Billing');
            $mail->addAddress($customer['email'], $customer['name']);
            
            $mail->Subject = "Invoice {$invoice['invoice_number']} from " . ($tenant['business_name'] ?? 'Us');
            $mail->isHTML(true);
            
            // Build body
            $amount = number_format((float)$invoice['grand_total'], 2);
            $mail->Body = "
                <h3>Hello {$customer['name']},</h3>
                <p>Please find attached your invoice <b>{$invoice['invoice_number']}</b> for <b>₹{$amount}</b>.</p>
                <p>Due Date: {$invoice['due_date']}</p>
                <br>
                <p>Thank you for your business!</p>
            ";
            
            if (file_exists($pdfPath)) {
                $mail->addAttachment($pdfPath, "Invoice_{$invoice['invoice_number']}.pdf");
            }

            return $mail->send();
        } catch (MailException $e) {
            // Log error
            return false;
        }
    }

    public static function sendPaymentReminder(array $invoice, array $customer, array $tenant): bool
    {
         try {
            $mail = self::configureMailer();
            $mail->setFrom($tenant['email'] ?? 'billing@saas.com', $tenant['business_name'] ?? 'Billing');
            $mail->addAddress($customer['email'], $customer['name']);
            
            $mail->Subject = "REMINDER: Invoice {$invoice['invoice_number']} is Overdue";
            $mail->isHTML(true);
            
            $amount = number_format((float)$invoice['balance_due'], 2);
            $mail->Body = "
                <h3 style='color:red;'>Payment Reminder</h3>
                <p>Hello {$customer['name']},</p>
                <p>This is a reminder that your invoice <b>{$invoice['invoice_number']}</b> has an outstanding balance of <b>₹{$amount}</b> and was due on {$invoice['due_date']}.</p>
                <p>Please make the payment at your earliest convenience.</p>
                <br>
                <p>Thank you,</p>
                <p>{$tenant['business_name']}</p>
            ";

            return $mail->send();
        } catch (MailException $e) {
            return false;
        }
    }
}
