"""Generate PDF from Surat HTML content using WeasyPrint."""


def generate_surat_pdf(
    no_surat: str,
    subject: str,
    perihal: str,
    tanggal: str,
    body: str,
    tembusan: str = "",
) -> bytes:
    """Return PDF bytes for a surat."""
    from weasyprint import HTML

    html = f"""<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
  body {{ font-family: Arial, sans-serif; font-size: 12pt; margin: 40px; color: #111; }}
  .header {{ text-align: center; border-bottom: 2px solid #111; padding-bottom: 12px; margin-bottom: 24px; }}
  .header h2 {{ margin: 0; font-size: 14pt; text-transform: uppercase; }}
  .meta table {{ width: 100%; border-collapse: collapse; margin-bottom: 20px; }}
  .meta td {{ padding: 3px 8px 3px 0; vertical-align: top; }}
  .meta td:first-child {{ width: 140px; font-weight: bold; }}
  .body {{ margin-top: 16px; line-height: 1.6; }}
  .tembusan {{ margin-top: 32px; font-size: 10pt; color: #555; }}
</style>
</head>
<body>
  <div class="header">
    <h2>Surat</h2>
  </div>
  <div class="meta">
    <table>
      <tr><td>No. Surat</td><td>: {no_surat}</td></tr>
      <tr><td>Perihal</td><td>: {perihal}</td></tr>
      <tr><td>Tanggal</td><td>: {tanggal}</td></tr>
      <tr><td>Subjek</td><td>: {subject}</td></tr>
    </table>
  </div>
  <div class="body">
    {body or '<p><em>(Isi surat kosong)</em></p>'}
  </div>
  {f'<div class="tembusan"><strong>Tembusan:</strong><br>{tembusan}</div>' if tembusan else ''}
</body>
</html>"""

    return HTML(string=html).write_pdf()
