import matplotlib.pyplot as plt

# Data extracted from search results (approximate/representative based on findings)
data = {
    'Province/Region': [
        'Andorra Region', 
        'Málaga (Costa del Sol)', 
        'Girona (Costa Brava)', 
        'Santa Cruz de Tenerife', 
        'Las Palmas', 
        'Valencia', 
        'Murcia (Costa Cálida)', 
        'Castilla-La Mancha',
        'Spain National Average (2026)'
    ],
    'Price_per_m2': [
        6110, 
        2697, 
        2058, 
        2034, 
        1851, 
        1735, 
        1298, 
        1117,
        2039
    ]
}

regions = data['Province/Region']
prices = data['Price_per_m2']

# Create the plot
plt.figure(figsize=(12, 7))
# Highlight national average (index 8)
colors = ['#2c3e50' if i != 8 else '#e74c3c' for i in range(len(regions))]

bars = plt.barh(regions, prices, color=colors)

plt.xlabel('Price per Square Meter (€)')
plt.title('Spanish Property Prices by Province/Region (Mid-2026 Estimates)', fontsize=14, pad=20)
plt.gca().invert_yaxis()  # Highest price on top
plt.grid(axis='x', linestyle='--', alpha=0.7)

# Add value labels to the end of each bar
for bar in bars:
    width = bar.get_width()
    plt.text(width + 50, bar.get_y() + bar.get_height()/2, f'€{int(width)}', 
             va='center', fontsize=10)

plt.tight_layout()

# Save the chart
chart_path = 'artifacts/spanish_property_prices.png'
plt.savefig(chart_path)
print(f"Chart saved to {chart_path}")

# Create a markdown report for PDF conversion
report_content = """# Spanish Property Market Analysis (2025-2026)

## Executive Summary
The Spanish real estate market has shown significant resilience and growth through 2025 and into mid-2026. The national average price per square meter reached approximately €2,039 by June 2026, marking a steady upward trend from the €1,935 recorded in 2025.

### Key Findings:
* High-End Markets: The Andorra region remains the most expensive area with prices reaching €6,110/m2. Coastal hotspots like Málaga (Costa del Sol) continue to command premium prices (€2,697/m2) driven by strong international demand.
* Coastal vs. Interior: There is a clear price gradient where coastal and island provinces (Málaga, Girona, Tenerife) significantly outperform the national average compared to interior regions.
* Growth Drivers: Provinces like Valencia and Málaga have seen double-digit year-on-year growth, fueled by both domestic stability and foreign investment.
* Affordability: The most affordable region identified in mid-2026 was Castilla-La Mancha, with prices around €1,117/m2.

## Property Price Comparison (per m2)
The following chart illustrates the price disparity across various key Spanish regions and provinces.

![Property Prices](spanish_property_prices.png)

*Data Source: Compiled from market analysis reports including Dream Properties International, Indomio, and Bravos Estate.*
"""

with open('artifacts/market_report.md', 'w') as f:
    f.write(report_content)

print("Report markdown created at artifacts/market_report.md")
