#!/usr/bin/env python3
"""Relocate all assets to known land locations on Martinique."""
import subprocess

def run_sql(query):
    subprocess.run([
        "docker", "exec", "flexmeasures-db", "psql", "-U", "flexmeasures", "-d", "flexmeasures",
        "-c", query
    ], capture_output=True, text=True)

# Real locations on Martinique (verified land positions)
# Format: (lat, lon, name)
LOCATIONS = [
    # Fort-de-France area (center)
    (14.6091, -61.2155, "Fort-de-France"),
    (14.6150, -61.2080, "Fort-de-France Nord"),
    (14.6050, -61.2200, "Fort-de-France Sud"),
    (14.6200, -61.2000, "Fort-de-France Est"),
    (14.5950, -61.2100, "Fort-de-France Ouest"),
    # Le Lamentin (west, industrial)
    (14.6100, -61.0100, "Le Lamentin"),
    (14.6150, -61.0050, "Aeroport Lamentin"),
    (14.6050, -61.0200, "Lamentin Zone Industrielle"),
    (14.6080, -61.0150, "Lamentin Centre"),
    # Schoelcher (north-west)
    (14.6200, -61.1000, "Schoelcher"),
    (14.6250, -61.0900, "Schoelcher Nord"),
    # Saint-Joseph (south)
    (14.4800, -61.0400, "Saint-Joseph"),
    (14.4900, -61.0350, "Saint-Joseph Nord"),
    (14.4700, -61.0450, "Saint-Joseph Sud"),
    # Le Marin (south-east)
    (14.4700, -60.8700, "Le Marin"),
    (14.4650, -60.8800, "Le Marin Ouest"),
    # Sainte-Anne (south-east coast)
    (14.4400, -60.8500, "Sainte-Anne"),
    (14.4350, -60.8600, "Sainte-Anne Nord"),
    # Le Vauclin (south-east tip)
    (14.5500, -60.8400, "Le Vauclin"),
    (14.5450, -60.8500, "Le Vauclin Nord"),
    # Le Diamant (south-west)
    (14.4800, -61.0200, "Le Diamant"),
    (14.4850, -61.0150, "Le Diamant Nord"),
    # Les Trois-Ilets (south-west)
    (14.5000, -61.0800, "Les Trois-Ilets"),
    (14.4950, -61.0900, "Les Trois-Ilets Est"),
    # Rivière-Salée (south)
    (14.4900, -60.9700, "Riviere-Salee"),
    (14.4850, -60.9650, "Riviere-Salee Nord"),
    # Le François (east coast)
    (14.6600, -60.9000, "Le Francois"),
    (14.6650, -60.8950, "Le Francois Nord"),
    # Sainte-Marie (north-east)
    (14.7800, -60.9800, "Sainte-Marie"),
    (14.7850, -60.9750, "Sainte-Marie Nord"),
    # Le Lorrain (north)
    (14.7200, -60.9300, "Le Lorrain"),
    (14.7250, -60.9250, "Le Lorrain Sud"),
    # Le Robert (east)
    (14.6800, -60.9400, "Le Robert"),
    (14.6850, -60.9350, "Le Robert Nord"),
    # Basse-Pointe (north-west)
    (14.7200, -61.1200, "Basse-Pointe"),
    # Grand'Rivière (north-west tip)
    (14.7500, -61.1500, "Grand-Riviere"),
    # Macouba (north-west)
    (14.7000, -61.1300, "Macouba"),
    # Ajoupa-Bouillon (north)
    (14.7500, -61.1000, "Ajoupa-Bouillon"),
    # Morne-Rouge (north)
    (14.7000, -61.1000, "Morne-Rouge"),
    # L'Ajoupa (north-west)
    (14.7300, -61.1100, "L'Ajoupa"),
    # Bellefontaine (north)
    (14.7300, -61.1500, "Bellefontaine"),
]

# Get all assets
result = subprocess.run([
    "docker", "exec", "flexmeasures-db", "psql", "-U", "flexmeasures", "-d", "flexmeasures",
    "-t", "-c", """
    SELECT g.id, g.name, gt.name as type_name
    FROM generic_asset g
    JOIN generic_asset_type gt ON g.generic_asset_type_id = gt.id
    ORDER BY gt.id, g.name;
    """
], capture_output=True, text=True)

assets = []
for line in result.stdout.strip().split('\n'):
    parts = [p.strip() for p in line.split('|')]
    if len(parts) == 3:
        assets.append((int(parts[0]), parts[1], parts[2]))

# Assign locations (spread across types)
for i, (aid, name, atype) in enumerate(assets):
    lat, lon, loc_name = LOCATIONS[i % len(LOCATIONS)]
    run_sql("UPDATE generic_asset SET latitude = {}, longitude = {} WHERE id = {};".format(lat, lon, aid))
    print("  {} -> {:.4f}, {:.4f} ({})".format(name, lat, lon, loc_name))

print("\n✅ All assets relocated to verified land locations on Martinique")
