#!/usr/bin/env python3
"""
Generate app icon for DistractionTrap-Study
Theme: Open mind / Brain expanding with light rays
Colors: Cyan to purple gradient on dark background
"""

import subprocess
import os

# Icon sizes needed for macOS
SIZES = [16, 32, 64, 128, 256, 512, 1024]

# Output directory
OUTPUT_DIR = "DistractionTrap-Study/Assets.xcassets/AppIcon.appiconset"

def generate_svg(size):
    """Generate SVG icon with brain/mind opening theme"""

    # Scale factors
    s = size / 100  # Base scale

    svg = f'''<?xml version="1.0" encoding="UTF-8"?>
<svg width="{size}" height="{size}" viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg">
  <defs>
    <!-- Background gradient -->
    <linearGradient id="bgGrad" x1="0%" y1="0%" x2="100%" y2="100%">
      <stop offset="0%" style="stop-color:#0a0a14;stop-opacity:1" />
      <stop offset="100%" style="stop-color:#000000;stop-opacity:1" />
    </linearGradient>

    <!-- Cyan to purple gradient -->
    <linearGradient id="brainGrad" x1="0%" y1="0%" x2="100%" y2="100%">
      <stop offset="0%" style="stop-color:#00d4ff;stop-opacity:1" />
      <stop offset="100%" style="stop-color:#a855f7;stop-opacity:1" />
    </linearGradient>

    <!-- Glow effect -->
    <filter id="glow" x="-50%" y="-50%" width="200%" height="200%">
      <feGaussianBlur stdDeviation="2" result="coloredBlur"/>
      <feMerge>
        <feMergeNode in="coloredBlur"/>
        <feMergeNode in="SourceGraphic"/>
      </feMerge>
    </filter>

    <!-- Light rays gradient -->
    <linearGradient id="rayGrad" x1="0%" y1="0%" x2="100%" y2="0%">
      <stop offset="0%" style="stop-color:#00d4ff;stop-opacity:0.8" />
      <stop offset="100%" style="stop-color:#00d4ff;stop-opacity:0" />
    </linearGradient>
  </defs>

  <!-- Background -->
  <rect width="100" height="100" rx="20" fill="url(#bgGrad)"/>

  <!-- Outer glow circle -->
  <circle cx="50" cy="50" r="35" fill="none" stroke="url(#brainGrad)" stroke-width="0.5" opacity="0.3"/>

  <!-- Light rays emanating from brain -->
  <g opacity="0.6" filter="url(#glow)">
    <!-- Top rays -->
    <line x1="50" y1="25" x2="50" y2="10" stroke="#00d4ff" stroke-width="2" stroke-linecap="round"/>
    <line x1="40" y1="28" x2="30" y2="15" stroke="#00d4ff" stroke-width="1.5" stroke-linecap="round"/>
    <line x1="60" y1="28" x2="70" y2="15" stroke="#00d4ff" stroke-width="1.5" stroke-linecap="round"/>

    <!-- Side rays -->
    <line x1="25" y1="45" x2="12" y2="40" stroke="#a855f7" stroke-width="1.5" stroke-linecap="round"/>
    <line x1="75" y1="45" x2="88" y2="40" stroke="#a855f7" stroke-width="1.5" stroke-linecap="round"/>
  </g>

  <!-- Brain shape (stylized) -->
  <g filter="url(#glow)">
    <!-- Left hemisphere -->
    <path d="M 35 50
             Q 30 40, 38 32
             Q 45 28, 48 35
             Q 50 30, 50 35
             Q 50 32, 52 35
             Q 55 28, 62 32
             Q 70 40, 65 50
             Q 70 60, 62 68
             Q 55 72, 50 68
             Q 45 72, 38 68
             Q 30 60, 35 50 Z"
          fill="url(#brainGrad)"
          opacity="0.9"/>

    <!-- Brain folds/details -->
    <path d="M 40 42 Q 45 45, 48 42" stroke="#ffffff" stroke-width="1" fill="none" opacity="0.4"/>
    <path d="M 52 42 Q 55 45, 60 42" stroke="#ffffff" stroke-width="1" fill="none" opacity="0.4"/>
    <path d="M 38 52 Q 44 55, 48 52" stroke="#ffffff" stroke-width="1" fill="none" opacity="0.4"/>
    <path d="M 52 52 Q 56 55, 62 52" stroke="#ffffff" stroke-width="1" fill="none" opacity="0.4"/>
    <path d="M 42 62 Q 46 65, 50 62" stroke="#ffffff" stroke-width="1" fill="none" opacity="0.4"/>
    <path d="M 50 62 Q 54 65, 58 62" stroke="#ffffff" stroke-width="1" fill="none" opacity="0.4"/>

    <!-- Center line -->
    <line x1="50" y1="35" x2="50" y2="68" stroke="#ffffff" stroke-width="0.8" opacity="0.3"/>
  </g>

  <!-- Opening effect at top -->
  <g opacity="0.8">
    <!-- Opening gap -->
    <path d="M 45 32 Q 50 20, 55 32" stroke="#00d4ff" stroke-width="2" fill="none" filter="url(#glow)"/>

    <!-- Sparkles/stars -->
    <circle cx="50" cy="18" r="2" fill="#ffffff"/>
    <circle cx="42" cy="22" r="1" fill="#00d4ff"/>
    <circle cx="58" cy="22" r="1" fill="#a855f7"/>
  </g>

  <!-- Infinity symbol below brain (subtle) -->
  <path d="M 38 78 Q 44 74, 50 78 Q 56 82, 62 78 Q 56 74, 50 78 Q 44 82, 38 78"
        stroke="url(#brainGrad)"
        stroke-width="1.5"
        fill="none"
        opacity="0.5"/>
</svg>'''

    return svg

def main():
    # Create output directory if it doesn't exist
    os.makedirs(OUTPUT_DIR, exist_ok=True)

    for size in SIZES:
        # Generate SVG
        svg_content = generate_svg(size)
        svg_path = f"/tmp/icon_{size}.svg"
        png_path = os.path.join(OUTPUT_DIR, f"icon_{size}.png")

        # Write SVG
        with open(svg_path, 'w') as f:
            f.write(svg_content)

        # Convert to PNG using qlmanage (macOS)
        try:
            # First try with qlmanage
            subprocess.run([
                'qlmanage', '-t', '-s', str(size), '-o', '/tmp', svg_path
            ], capture_output=True, check=True)

            # Move and rename
            tmp_png = f"/tmp/icon_{size}.svg.png"
            if os.path.exists(tmp_png):
                subprocess.run(['mv', tmp_png, png_path], check=True)
                print(f"Generated {png_path}")
            else:
                raise Exception("PNG not generated")

        except Exception as e:
            # Fallback: try with sips
            try:
                subprocess.run([
                    'sips', '-s', 'format', 'png',
                    '-z', str(size), str(size),
                    svg_path, '--out', png_path
                ], capture_output=True, check=True)
                print(f"Generated {png_path} (via sips)")
            except Exception as e2:
                print(f"Error generating {size}px icon: {e2}")

        # Clean up SVG
        if os.path.exists(svg_path):
            os.remove(svg_path)

    print("\nIcon generation complete!")
    print(f"Icons saved to: {OUTPUT_DIR}")

if __name__ == "__main__":
    main()
