import { db } from '../../database/db';

export class InvoicePdfService {
  /**
   * Generates printable enterprise HTML invoice with QR codes and VAT breakdown
   */
  public static generatePrintableInvoiceHtml(invoiceNo: string): string {
    const inv = db.invoices.find(i => i.invoice_no === invoiceNo);
    if (!inv) throw new Error('Invoice not found');

    const sub = db.subscribers.find(s => s.id === inv.subscriber_id);
    const pkg = db.packages.find(p => p.id === inv.package_id);
    const reseller = inv.reseller_id ? db.resellers.find(r => r.id === inv.reseller_id) : undefined;
    const tenant = db.tenants[0];

    const companyName = reseller ? reseller.company_name : tenant.name;
    const companyPhone = reseller ? reseller.phone : '09612-888999';
    const vatNo = tenant.vat_registration_no || 'VAT-99882211';
    const footer = reseller ? reseller.invoice_footer : tenant.invoice_footer;

    const baseAmount = inv.amount;
    const vatAmount = inv.vat_amount || Number((baseAmount * 0.05).toFixed(2));
    const totalAmount = inv.net_amount || (baseAmount + vatAmount);

    return `
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>Invoice - ${inv.invoice_no}</title>
  <style>
    body { font-family: 'Helvetica Neue', Arial, sans-serif; color: #1e293b; padding: 40px; background: #f8fafc; }
    .invoice-card { max-width: 800px; margin: 0 auto; background: #fff; padding: 40px; border-radius: 12px; box-shadow: 0 4px 20px rgba(0,0,0,0.08); border: 1px solid #e2e8f0; }
    .header { display: flex; justify-content: space-between; align-items: flex-start; border-bottom: 2px solid #0284c7; padding-bottom: 20px; }
    .logo-text { font-size: 24px; font-weight: 800; color: #0284c7; }
    .invoice-title { font-size: 28px; font-weight: 800; color: #0f172a; text-align: right; }
    .badge-paid { background: #dcfce7; color: #15803d; padding: 4px 12px; border-radius: 999px; font-weight: 700; font-size: 13px; display: inline-block; }
    .grid { display: grid; grid-template-columns: 1fr 1fr; gap: 30px; margin: 24px 0; }
    .section-title { font-size: 11px; text-transform: uppercase; color: #64748b; font-weight: 700; margin-bottom: 6px; }
    table { width: 100%; border-collapse: collapse; margin: 24px 0; }
    th { background: #f1f5f9; padding: 12px; text-align: left; font-size: 12px; text-transform: uppercase; color: #475569; }
    td { padding: 14px 12px; border-bottom: 1px solid #e2e8f0; font-size: 14px; }
    .totals { margin-left: auto; width: 300px; margin-top: 10px; }
    .totals-row { display: flex; justify-content: space-between; padding: 6px 0; font-size: 14px; }
    .grand-total { font-size: 18px; font-weight: 800; color: #0284c7; border-top: 2px solid #e2e8f0; padding-top: 10px; margin-top: 6px; }
    .footer { margin-top: 40px; padding-top: 20px; border-top: 1px solid #e2e8f0; text-align: center; font-size: 12px; color: #64748b; }
    @media print { body { background: #fff; padding: 0; } .invoice-card { box-shadow: none; border: none; } }
  </style>
</head>
<body>
  <div class="invoice-card">
    <div class="header">
      <div>
        <div class="logo-text">⚡ ${companyName}</div>
        <div style="font-size: 13px; color: #64748b; margin-top: 4px;">Nationwide Broadband Internet Service Provider</div>
        <div style="font-size: 12px; color: #64748b;">Govt. VAT Reg: <strong>${vatNo}</strong> • Hotline: ${companyPhone}</div>
      </div>
      <div>
        <div class="invoice-title">TAX INVOICE</div>
        <div style="text-align: right; margin-top: 4px;">
          <span class="badge-paid">${inv.status.toUpperCase()}</span>
        </div>
      </div>
    </div>

    <div class="grid">
      <div>
        <div class="section-title">BILLED TO SUBSCRIBER</div>
        <div style="font-weight: 700; font-size: 16px;">${sub?.full_name || 'Subscriber'}</div>
        <div style="font-size: 13px; color: #475569; margin-top: 4px;">Username: <strong>${sub?.username}</strong></div>
        <div style="font-size: 13px; color: #475569;">Phone: ${sub?.phone}</div>
        <div style="font-size: 13px; color: #475569;">Address: ${sub?.address || 'Dhaka, Bangladesh'}</div>
      </div>
      <div style="text-align: right;">
        <div class="section-title">INVOICE DETAILS</div>
        <div style="font-size: 13px; color: #475569;">Invoice No: <strong style="font-family: monospace;">${inv.invoice_no}</strong></div>
        <div style="font-size: 13px; color: #475569;">Billing Date: ${new Date(inv.created_at).toLocaleDateString('en-GB')}</div>
        <div style="font-size: 13px; color: #475569;">Due / Renewal Date: ${new Date(inv.due_date).toLocaleDateString('en-GB')}</div>
        <div style="font-size: 13px; color: #475569;">Payment Method: <strong>${inv.payment_method || 'bKash Pay Bill'}</strong></div>
      </div>
    </div>

    <table>
      <thead>
        <tr>
          <th>Description</th>
          <th>Bandwidth Tier</th>
          <th>Validity</th>
          <th style="text-align: right;">Amount (BDT)</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td>
            <strong>${pkg?.name || 'Broadband Package'}</strong>
            <div style="font-size: 12px; color: #64748b;">Unlimited BDIX & YouTube Peering, Low Latency Fiber</div>
          </td>
          <td>${pkg?.speed_mbps || 20} Mbps Full Duplex</td>
          <td>30 Days</td>
          <td style="text-align: right; font-weight: 700;">৳ ${baseAmount.toFixed(2)}</td>
        </tr>
      </tbody>
    </table>

    <div class="totals">
      <div class="totals-row">
        <span>Package Subtotal:</span>
        <span>৳ ${baseAmount.toFixed(2)}</span>
      </div>
      <div class="totals-row">
        <span>Govt. 5% VAT (BTRC):</span>
        <span>৳ ${vatAmount.toFixed(2)}</span>
      </div>
      <div class="totals-row grand-total">
        <span>Total Payable:</span>
        <span>৳ ${totalAmount.toFixed(2)}</span>
      </div>
    </div>

    <div class="footer">
      <div>${footer}</div>
      <div style="margin-top: 6px; font-size: 11px; color: #94a3b8;">This is a computer-generated tax invoice. No signature required.</div>
    </div>
  </div>
</body>
</html>
    `.trim();
  }
}
