import matplotlib
matplotlib.use('PDF')
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches

# Summary data
summary_data = {
    "title": "Spanish Property Market Summary 2025–2026",
    "national_avg": 2430,
    "highest": {"region": "Balearic Islands", "price": 5073},
    "lowest": {"region": "Castilla-La Mancha", "price": 1117},
    "top5": [
        ("Balearic Islands", 5073),
        ("Madrid", 3902),
        ("Barcelona", 3083),
        ("Andalucía", 3010),
        ("Cataluña", 2837),
    ],
    "bottom5": [
        ("Castilla-La Mancha", 1117),
        ("Aragón", 1138),
        ("Extremadura", 1152),
        ("Castilla y León", 1409),
        ("Galicia", 1499),
    ],
    "key_insights": [
        "The Balearic Islands lead with €5,073/m² — nearly 4.5× the cheapest region.",
        "Madrid and Barcelona remain the most expensive mainland provinces at €3,902 and €3,083/m².",
        "Interior regions (Castilla-La Mancha, Aragón, Extremadura) offer the lowest prices, all under €1,200/m².",
        "Coastal and island regions command a significant premium over inland areas.",
        "Year-over-year growth ranges from ~0.6% to 16.8%, with Madrid and Valencia showing the strongest gains.",
        "The national average sits around €2,430/m², but this masks extreme regional disparities.",
    ]
}

fig, ax = plt.subplots(figsize=(14, 10))
ax.axis('off')

# Title
title_text = "Spanish Property Prices per m²\nMarket Summary — 2025/2026"
ax.text(0.5, 0.95, title_text, ha='center', va='top', fontsize=22, fontweight='bold',
        color='#1a1a2e', transform=ax.transAxes)

# National average box
avg = summary_data["national_avg"]
box1 = mpatches.FancyBboxPatch((0.05, 0.82), 0.9, 0.1, boxstyle="round,pad=0.01",
                                edgecolor='#1a1a2e', facecolor='#e8f4f8', linewidth=2, transform=ax.transAxes)
ax.add_patch(box1)
ax.text(0.5, 0.88, f"National Average: €{avg:,.0f} per m²", ha='center', va='center',
        fontsize=16, fontweight='bold', color='#1a1a2e', transform=ax.transAxes)

# Top 5 table
top5 = summary_data["top5"]
ax.text(0.05, 0.73, "🔴 TOP 5 MOST EXPENSIVE REGIONS", ha='left', va='top',
        fontsize=13, fontweight='bold', color='#c0392b', transform=ax.transAxes)

y_pos = 0.68
for i, (region, price) in enumerate(top5):
    color = '#fde8e8' if i < 3 else '#fff5f5'
    box = mpatches.FancyBboxPatch((0.07, y_pos - 0.035), 0.86, 0.035,
                                   boxstyle="round,pad=0.005", facecolor=color,
                                   edgecolor='none', transform=ax.transAxes)
    ax.add_patch(box)
    ax.text(0.08, y_pos - 0.017, f"{i+1}. {region}", ha='left', va='center',
            fontsize=11, color='#333', transform=ax.transAxes)
    ax.text(0.92, y_pos - 0.017, f"€{price:,.0f} / m²", ha='right', va='center',
            fontsize=11, fontweight='bold', color='#c0392b', transform=ax.transAxes)
    y_pos -= 0.045

# Bottom 5 table
bottom5 = summary_data["bottom5"]
ax.text(0.05, y_pos + 0.02, "🟢 MOST AFFORDABLE REGIONS", ha='left', va='top',
        fontsize=13, fontweight='bold', color='#27ae60', transform=ax.transAxes)

y_pos -= 0.04
for i, (region, price) in enumerate(bottom5):
    color = '#e8f8e8' if i < 3 else '#f5fff5'
    box = mpatches.FancyBboxPatch((0.07, y_pos - 0.035), 0.86, 0.035,
                                   boxstyle="round,pad=0.005", facecolor=color,
                                   edgecolor='none', transform=ax.transAxes)
    ax.add_patch(box)
    ax.text(0.08, y_pos - 0.017, f"{i+1}. {region}", ha='left', va='center',
            fontsize=11, color='#333', transform=ax.transAxes)
    ax.text(0.92, y_pos - 0.017, f"€{price:,.0f} / m²", ha='right', va='center',
            fontsize=11, fontweight='bold', color='#27ae60', transform=ax.transAxes)
    y_pos -= 0.045

# Key insights
y_pos -= 0.03
ax.text(0.05, y_pos, "KEY MARKET INSIGHTS", ha='left', va='top',
        fontsize=13, fontweight='bold', color='#1a1a2e', transform=ax.transAxes)

y_pos -= 0.04
for insight in summary_data["key_insights"]:
    ax.text(0.07, y_pos, f"• {insight}", ha='left', va='top',
            fontsize=10, color='#444', transform=ax.transAxes, linespacing=1.6)
    y_pos -= 0.045

# Sources
ax.text(0.5, 0.02, "Sources: INE (Q4 2025), Engel Völkers (Q2 2026), Indomio (June 2026), GlobalPropertyGuide", 
        ha='center', va='bottom', fontsize=8, color='#888', style='italic', transform=ax.transAxes)

plt.tight_layout()
plt.savefig('artifacts/spain_property_prices_summary.pdf', dpi=150, bbox_inches='tight', facecolor='white')
print("Summary PDF saved")
