import os
import subprocess
from PIL import Image

EMPRESA_SVGS = {
    # 1. Empresas: Sede corporativa / edificio empresarial en azul y oro
    "icon_emp_empresas": """<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512" width="512" height="512">
  <defs>
    <filter id="shadow" x="-5%" y="-5%" width="110%" height="115%">
      <feDropShadow dx="0" dy="8" stdDeviation="10" flood-color="#14334F" flood-opacity="0.2"/>
    </filter>
  </defs>
  <g filter="url(#shadow)">
    <!-- Base pavement -->
    <rect x="70" y="420" width="372" height="30" rx="10" fill="#1C4468" stroke="#14334F" stroke-width="6"/>
    <!-- Main Corporate Tower -->
    <rect x="110" y="100" width="292" height="320" rx="20" fill="#1C4468" stroke="#14334F" stroke-width="12"/>
    <!-- Roof cornice -->
    <rect x="94" y="90" width="324" height="30" rx="8" fill="#D29C38" stroke="#B37F22" stroke-width="6"/>
    <!-- Corporate Entrance Arch / Portico -->
    <rect x="210" y="320" width="92" height="100" rx="12" fill="#D29C38" stroke="#B37F22" stroke-width="8"/>
    <rect x="230" y="350" width="52" height="70" rx="6" fill="#F7F4EC"/>
    <line x1="256" y1="350" x2="256" y2="420" stroke="#1C4468" stroke-width="4"/>
    <!-- Windows Floor 1 -->
    <rect x="146" y="146" width="54" height="42" rx="8" fill="#DCE8F2"/>
    <rect x="229" y="146" width="54" height="42" rx="8" fill="#DCE8F2"/>
    <rect x="312" y="146" width="54" height="42" rx="8" fill="#DCE8F2"/>
    <!-- Windows Floor 2 -->
    <rect x="146" y="206" width="54" height="42" rx="8" fill="#DCE8F2"/>
    <rect x="229" y="206" width="54" height="42" rx="8" fill="#DCE8F2"/>
    <rect x="312" y="206" width="54" height="42" rx="8" fill="#DCE8F2"/>
    <!-- Windows Floor 3 -->
    <rect x="146" y="266" width="54" height="42" rx="8" fill="#DCE8F2"/>
    <rect x="229" y="266" width="54" height="42" rx="8" fill="#DCE8F2"/>
    <rect x="312" y="266" width="54" height="42" rx="8" fill="#DCE8F2"/>
    <!-- Golden antenna / tech beacon -->
    <line x1="256" y1="40" x2="256" y2="90" stroke="#D29C38" stroke-width="10" stroke-linecap="round"/>
    <circle cx="256" cy="40" r="12" fill="#D29C38"/>
  </g>
</svg>""",

    # 2. Selec. Emp: Selector/cambio de empresa con flechas circulares de cambio
    "icon_emp_selec_emp": """<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512" width="512" height="512">
  <defs>
    <filter id="shadow" x="-5%" y="-5%" width="110%" height="115%">
      <feDropShadow dx="0" dy="8" stdDeviation="10" flood-color="#14334F" flood-opacity="0.2"/>
    </filter>
  </defs>
  <g filter="url(#shadow)">
    <!-- Central Building inside switcher -->
    <rect x="156" y="150" width="200" height="230" rx="16" fill="#1C4468" stroke="#14334F" stroke-width="10"/>
    <rect x="144" y="140" width="224" height="24" rx="6" fill="#D29C38"/>
    <!-- Windows -->
    <rect x="180" y="180" width="40" height="32" rx="6" fill="#DCE8F2"/>
    <rect x="240" y="180" width="40" height="32" rx="6" fill="#DCE8F2"/>
    <rect x="180" y="230" width="40" height="32" rx="6" fill="#DCE8F2"/>
    <rect x="240" y="230" width="40" height="32" rx="6" fill="#DCE8F2"/>
    <rect x="292" y="180" width="40" height="32" rx="6" fill="#DCE8F2"/>
    <rect x="292" y="230" width="40" height="32" rx="6" fill="#DCE8F2"/>
    <!-- Door -->
    <rect x="226" y="300" width="60" height="80" rx="6" fill="#D29C38"/>
    <!-- Top Switching Arrow (Clockwise, gold) -->
    <path d="M120 180 C130 90, 240 60, 360 80" fill="none" stroke="#D29C38" stroke-width="20" stroke-linecap="round"/>
    <polygon points="380,84 350,60 350,110" fill="#D29C38"/>
    <!-- Bottom Switching Arrow (Counter-clockwise, blue) -->
    <path d="M392 332 C382 422, 272 452, 152 432" fill="none" stroke="#1C4468" stroke-width="20" stroke-linecap="round"/>
    <polygon points="132,428 162,452 162,402" fill="#1C4468"/>
  </g>
</svg>""",

    # 3. Series Facturación: Fichero / series de documentos con pestañas A, B, C
    "icon_emp_series": """<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512" width="512" height="512">
  <defs>
    <filter id="shadow" x="-5%" y="-5%" width="110%" height="115%">
      <feDropShadow dx="0" dy="8" stdDeviation="10" flood-color="#14334F" flood-opacity="0.2"/>
    </filter>
  </defs>
  <g filter="url(#shadow)">
    <!-- Back binder / folder -->
    <rect x="90" y="110" width="310" height="340" rx="20" fill="#14334F"/>
    <!-- Folder body -->
    <rect x="106" y="120" width="294" height="330" rx="16" fill="#1C4468" stroke="#14334F" stroke-width="10"/>
    <!-- Sheet with series tabs -->
    <!-- Tab 1 (A) -->
    <rect x="360" y="140" width="70" height="50" rx="8" fill="#D29C38" stroke="#B37F22" stroke-width="4"/>
    <text x="395" y="176" font-family="Arial, sans-serif" font-weight="900" font-size="28" fill="#1C4468" text-anchor="middle">A</text>
    <!-- Tab 2 (B) -->
    <rect x="360" y="210" width="70" height="50" rx="8" fill="#DCE8F2" stroke="#1C4468" stroke-width="4"/>
    <text x="395" y="246" font-family="Arial, sans-serif" font-weight="900" font-size="28" fill="#1C4468" text-anchor="middle">B</text>
    <!-- Tab 3 (C) -->
    <rect x="360" y="280" width="70" height="50" rx="8" fill="#D29C38" stroke="#B37F22" stroke-width="4"/>
    <text x="395" y="316" font-family="Arial, sans-serif" font-weight="900" font-size="28" fill="#1C4468" text-anchor="middle">C</text>
    <!-- Invoice Page in front -->
    <rect x="140" y="150" width="220" height="270" rx="12" fill="#F7F4EC" stroke="#D29C38" stroke-width="8"/>
    <!-- Invoice lines -->
    <rect x="165" y="180" width="130" height="24" rx="6" fill="#1C4468"/>
    <line x1="165" y1="230" x2="330" y2="230" stroke="#1C4468" stroke-width="8" stroke-linecap="round"/>
    <line x1="165" y1="265" x2="310" y2="265" stroke="#1C4468" stroke-width="8" stroke-linecap="round"/>
    <line x1="165" y1="300" x2="280" y2="300" stroke="#D29C38" stroke-width="8" stroke-linecap="round"/>
    <!-- Stamp / seal -->
    <circle cx="290" cy="360" r="32" fill="#D29C38" stroke="#1C4468" stroke-width="6"/>
    <path d="M280 360 L288 368 L302 352" fill="none" stroke="#FFFFFF" stroke-width="6" stroke-linecap="round"/>
  </g>
</svg>""",

    # 4. Tipos IVA: Porcentaje fiscal, calculadora y recibo
    "icon_emp_tipos_iva": """<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512" width="512" height="512">
  <defs>
    <filter id="shadow" x="-5%" y="-5%" width="110%" height="115%">
      <feDropShadow dx="0" dy="8" stdDeviation="10" flood-color="#14334F" flood-opacity="0.2"/>
    </filter>
  </defs>
  <g filter="url(#shadow)">
    <!-- Receipt document base -->
    <rect x="90" y="90" width="270" height="360" rx="20" fill="#F7F4EC" stroke="#1C4468" stroke-width="12"/>
    <rect x="120" y="130" width="150" height="26" rx="6" fill="#1C4468"/>
    <line x1="120" y1="185" x2="310" y2="185" stroke="#DCE8F2" stroke-width="10" stroke-linecap="round"/>
    <line x1="120" y1="225" x2="280" y2="225" stroke="#DCE8F2" stroke-width="10" stroke-linecap="round"/>
    <line x1="120" y1="265" x2="250" y2="265" stroke="#DCE8F2" stroke-width="10" stroke-linecap="round"/>
    <!-- Huge golden % IVA Tax Badge in foreground -->
    <circle cx="340" cy="310" r="110" fill="#D29C38" stroke="#1C4468" stroke-width="14"/>
    <circle cx="340" cy="310" r="92" fill="#F7F4EC"/>
    <!-- Percentage symbol -->
    <line x1="290" y1="360" x2="390" y2="260" stroke="#1C4468" stroke-width="18" stroke-linecap="round"/>
    <circle cx="305" cy="275" r="22" fill="#1C4468"/>
    <circle cx="305" cy="275" r="10" fill="#F7F4EC"/>
    <circle cx="375" cy="345" r="22" fill="#1C4468"/>
    <circle cx="375" cy="345" r="10" fill="#F7F4EC"/>
  </g>
</svg>""",

    # 5. Configuración / Conexión: Engranajes de sistema y ajustes
    "icon_emp_config": """<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512" width="512" height="512">
  <defs>
    <filter id="shadow" x="-5%" y="-5%" width="110%" height="115%">
      <feDropShadow dx="0" dy="8" stdDeviation="10" flood-color="#14334F" flood-opacity="0.2"/>
    </filter>
  </defs>
  <g filter="url(#shadow)">
    <!-- Large Blue Master Gear -->
    <g transform="translate(210, 210)">
      <!-- 8 Teeth -->
      <rect x="-30" y="-150" width="60" height="300" rx="14" fill="#1C4468"/>
      <rect x="-30" y="-150" width="60" height="300" rx="14" fill="#1C4468" transform="rotate(45)"/>
      <rect x="-30" y="-150" width="60" height="300" rx="14" fill="#1C4468" transform="rotate(90)"/>
      <rect x="-30" y="-150" width="60" height="300" rx="14" fill="#1C4468" transform="rotate(135)"/>
      <!-- Gear Outer Circle -->
      <circle cx="0" cy="0" r="126" fill="#1C4468" stroke="#14334F" stroke-width="10"/>
      <!-- Inner hole -->
      <circle cx="0" cy="0" r="54" fill="#F7F4EC" stroke="#14334F" stroke-width="8"/>
    </g>
    <!-- Interlocking Golden Small Gear -->
    <g transform="translate(350, 350)">
      <rect x="-20" y="-95" width="40" height="190" rx="10" fill="#D29C38"/>
      <rect x="-20" y="-95" width="40" height="190" rx="10" fill="#D29C38" transform="rotate(45)"/>
      <rect x="-20" y="-95" width="40" height="190" rx="10" fill="#D29C38" transform="rotate(90)"/>
      <rect x="-20" y="-95" width="40" height="190" rx="10" fill="#D29C38" transform="rotate(135)"/>
      <circle cx="0" cy="0" r="80" fill="#D29C38" stroke="#B37F22" stroke-width="8"/>
      <circle cx="0" cy="0" r="34" fill="#1C4468" stroke="#B37F22" stroke-width="6"/>
    </g>
  </g>
</svg>""",

    # 6. Formas Pago: Tarjeta de crédito bancaria y monedas de oro
    "icon_emp_formas_pago": """<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512" width="512" height="512">
  <defs>
    <filter id="shadow" x="-5%" y="-5%" width="110%" height="115%">
      <feDropShadow dx="0" dy="8" stdDeviation="10" flood-color="#14334F" flood-opacity="0.2"/>
    </filter>
  </defs>
  <g filter="url(#shadow)">
    <!-- Credit Card Body -->
    <rect x="76" y="110" width="360" height="230" rx="24" fill="#1C4468" stroke="#14334F" stroke-width="12"/>
    <!-- Magnetic stripe -->
    <rect x="76" y="160" width="360" height="44" fill="#14334F"/>
    <!-- Golden EMV Chip -->
    <rect x="120" y="230" width="64" height="50" rx="10" fill="#D29C38" stroke="#B37F22" stroke-width="6"/>
    <line x1="120" y1="255" x2="184" y2="255" stroke="#B37F22" stroke-width="4"/>
    <line x1="152" y1="230" x2="152" y2="280" stroke="#B37F22" stroke-width="4"/>
    <!-- Contactless symbol -->
    <path d="M210 240 C216 248, 216 262, 210 270" fill="none" stroke="#DCE8F2" stroke-width="6" stroke-linecap="round"/>
    <path d="M222 234 C234 246, 234 266, 222 276" fill="none" stroke="#DCE8F2" stroke-width="6" stroke-linecap="round"/>
    <!-- Stack of Golden Coins in foreground -->
    <g transform="translate(300, 270)">
      <!-- Bottom Coin -->
      <ellipse cx="70" cy="120" rx="70" ry="32" fill="#B37F22" stroke="#1C4468" stroke-width="8"/>
      <ellipse cx="70" cy="112" rx="70" ry="32" fill="#D29C38" stroke="#1C4468" stroke-width="8"/>
      <!-- Middle Coin -->
      <ellipse cx="70" cy="80" rx="70" ry="32" fill="#B37F22" stroke="#1C4468" stroke-width="8"/>
      <ellipse cx="70" cy="72" rx="70" ry="32" fill="#D29C38" stroke="#1C4468" stroke-width="8"/>
      <!-- Top Coin with currency symbol -->
      <ellipse cx="70" cy="40" rx="70" ry="32" fill="#B37F22" stroke="#1C4468" stroke-width="8"/>
      <ellipse cx="70" cy="32" rx="70" ry="32" fill="#F7F4EC" stroke="#D29C38" stroke-width="8"/>
      <text x="70" y="44" font-family="Arial, sans-serif" font-weight="900" font-size="36" fill="#1C4468" text-anchor="middle">€</text>
    </g>
  </g>
</svg>"""
}

masters_dir = "resources/iconos_secciones/masters"
icons_dir = "resources/iconos_secciones"
os.makedirs(masters_dir, exist_ok=True)
os.makedirs("scratch/rendered", exist_ok=True)

chrome_path = r"C:\Program Files\Google\Chrome\Application\chrome.exe"

print(f"Generating {len(EMPRESA_SVGS)} 2D Empresa icons...")
for name, svg in EMPRESA_SVGS.items():
    html_file = f"scratch/rendered/{name}.html"
    master_png = os.path.join(masters_dir, f"{name}.png")
    ui_png = os.path.join(icons_dir, f"{name}.png")
    
    html = f"""<!DOCTYPE html><html><head><style>
body {{ margin: 0; padding: 0; background: transparent; overflow: hidden; }}
svg {{ display: block; width: 512px; height: 512px; }}
</style></head><body>{svg}</body></html>"""
    
    with open(html_file, "w", encoding="utf-8") as f:
        f.write(html)
        
    cmd = [
        chrome_path, "--headless=new", "--disable-gpu",
        "--default-background-color=00000000", "--window-size=512,512",
        f"--screenshot={os.path.abspath(master_png)}",
        f"file:///{os.path.abspath(html_file)}"
    ]
    subprocess.run(cmd, check=True)
    
    # Resize master to 42x42 for UI
    im = Image.open(master_png)
    im42 = im.resize((42, 42), Image.Resampling.LANCZOS)
    im42.save(ui_png)
    print(f"Generated {name}: master (512x512) and UI (42x42)")

print("All 6 Empresa icons successfully generated!")
