"""Generate realistic synthetic Swiss geospatial datasets for offline testing and demo.

Generates nationwide sample records across multiple cantons (BE, ZH, LU, VD, GE, TI, SG)
in Swiss LV95 (EPSG:2056) coordinates, allowing instant pipeline testing without downloading
gigabytes of raw federal archives.
"""

from __future__ import annotations

import logging
from pathlib import Path
from typing import Optional

import duckdb

from ch_housing.ingest import DEFAULT_DB_PATH, DEFAULT_EXT_DIR, get_duckdb_connection

logger = logging.getLogger(__name__)


def seed_sample_database(db_path: Path | str = DEFAULT_DB_PATH) -> int:
    """Populate DuckDB with realistic Swiss residential, transit, and restaurant records."""
    con = get_duckdb_connection(db_path=db_path, extensions_dir=DEFAULT_EXT_DIR)

    logger.info("Seeding sample Swiss geospatial database at %s...", db_path)

    # 1. Standardized Buildings Table (EPSG:2056)
    con.execute("""
        CREATE OR REPLACE TABLE buildings AS
        SELECT * FROM (
            VALUES
                -- Bern (BE)
                -- 200101: Standalone detached house
                (200101::BIGINT, 'BE', 'Bern', 351, 'Gerechtigkeitsgasse', '10', 3011, 'Bern', 1004, 1020, 1110, 1, 140.0, 2600200.0, 1199900.0, ST_Point(2600200.0, 1199900.0)),
                -- 200102: Standalone house
                (200102::BIGINT, 'BE', 'Muri bei Bern', 356, 'Thunstrasse', '45', 3074, 'Muri', 1004, 1020, 1110, 1, 160.0, 2603400.0, 1198500.0, ST_Point(2603400.0, 1198500.0)),
                -- 200103: Row house (Reiheneinfamilienhaus)
                (200103::BIGINT, 'BE', 'Köniz', 355, 'Reihenweg', '4', 3097, 'Liebefeld', 1004, 1020, 1110, 1, 130.0, 2598800.0, 1197200.0, ST_Point(2598800.0, 1197200.0)),
                -- 200104: Standalone house in Spiez
                (200104::BIGINT, 'BE', 'Spiez', 768, 'Seestrasse', '22', 3700, 'Spiez', 1004, 1020, 1110, 1, 125.0, 2618500.0, 1170500.0, ST_Point(2618500.0, 1170500.0)),
                -- 200105: Dual-family house (Zweifamilienhaus, GKLAS=1121, 2 dwellings)
                (200105::BIGINT, 'BE', 'Thun', 942, 'Gwattstrasse', '18', 3604, 'Thun', 1004, 1020, 1121, 2, 180.0, 2613500.0, 1176500.0, ST_Point(2613500.0, 1176500.0)),

                -- Zürich (ZH)
                -- 200201: Standalone house
                (200201::BIGINT, 'ZH', 'Zürich', 261, 'Zürichbergstrasse', '55', 8044, 'Zürich', 1004, 1020, 1110, 1, 190.0, 2684200.0, 1248500.0, ST_Point(2684200.0, 1248500.0)),
                -- 200202: Standalone house
                (200202::BIGINT, 'ZH', 'Küsnacht', 154, 'Seestrasse', '101', 8700, 'Küsnacht', 1004, 1020, 1110, 1, 210.0, 2686800.0, 1242300.0, ST_Point(2686800.0, 1242300.0)),
                -- 200203: Row house in Winterthur
                (200203::BIGINT, 'ZH', 'Winterthur', 230, 'Reihenhausstrasse', '8', 8400, 'Winterthur', 1004, 1020, 1110, 1, 150.0, 2697200.0, 1262100.0, ST_Point(2697200.0, 1262100.0)),
                -- 200204: Dual-family house in Meilen (Zweifamilienhaus, GKLAS=1121, 2 dwellings)
                (200204::BIGINT, 'ZH', 'Meilen', 156, 'Kirchgasse', '8', 8706, 'Meilen', 1004, 1020, 1121, 2, 175.0, 2691500.0, 1237200.0, ST_Point(2691500.0, 1237200.0)),

                -- Luzern (LU)
                (200301::BIGINT, 'LU', 'Luzern', 1061, 'Haldenstrasse', '15', 6006, 'Luzern', 1004, 1020, 1110, 1, 155.0, 2667100.0, 1212200.0, ST_Point(2667100.0, 1212200.0)),
                (200302::BIGINT, 'LU', 'Horw', 1058, 'Kantonsstrasse', '40', 6048, 'Horw', 1004, 1020, 1110, 1, 140.0, 2665800.0, 1208900.0, ST_Point(2665800.0, 1208900.0)),

                -- Vaud (VD)
                (200401::BIGINT, 'VD', 'Lausanne', 5586, 'Chemin de Primerose', '6', 1007, 'Lausanne', 1004, 1020, 1110, 1, 165.0, 2537200.0, 1152800.0, ST_Point(2537200.0, 1152800.0)),
                (200402::BIGINT, 'VD', 'Morges', 5642, 'Rue du Lac', '14', 1110, 'Morges', 1004, 1020, 1110, 1, 135.0, 2528100.0, 1151200.0, ST_Point(2528100.0, 1151200.0)),

                -- Genève (GE)
                (200501::BIGINT, 'GE', 'Genève', 6621, 'Route de Florissant', '70', 1206, 'Genève', 1004, 1020, 1110, 1, 180.0, 2501500.0, 1116200.0, ST_Point(2501500.0, 1116200.0)),
                (200502::BIGINT, 'GE', 'Cologny', 6617, 'Chemin de Ruth', '12', 1223, 'Cologny', 1004, 1020, 1110, 1, 230.0, 2503200.0, 1118900.0, ST_Point(2503200.0, 1118900.0)),

                -- Basel-Landschaft / Basel-Stadt (BL / BS)
                (200601::BIGINT, 'BL', 'Binningen', 2801, 'Bottmingerstrasse', '20', 4102, 'Binningen', 1004, 1020, 1110, 1, 150.0, 2610500.0, 1265800.0, ST_Point(2610500.0, 1265800.0)),

                -- Negative controls:
                -- 200901: Commercial building (GKAT=1060) -> EXCLUDED
                (200901::BIGINT, 'BE', 'Bern', 351, 'Bubenbergplatz', '5', 3011, 'Bern', 1004, 1060, 1211, 0, 900.0, 2600000.0, 1199800.0, ST_Point(2600000.0, 1199800.0)),
                -- 200902: Planned building (GSTAT=1001) -> EXCLUDED
                (200902::BIGINT, 'ZH', 'Zürich', 261, 'Hardturmstrasse', '100', 8005, 'Zürich', 1001, 1020, 1110, 1, 120.0, 2681500.0, 1249500.0, ST_Point(2681500.0, 1249500.0)),
                -- 200903: Dense apartment block (24 units) -> EXCLUDED
                (200903::BIGINT, 'ZH', 'Zürich', 261, 'Badenerstrasse', '500', 8048, 'Zürich', 1004, 1020, 1122, 24, 750.0, 2680200.0, 1247800.0, ST_Point(2680200.0, 1247800.0)),
                -- 200904: House in remote woods (NO restaurant within 10 min / 800m) -> EXCLUDED
                (200904::BIGINT, 'BE', 'Guggisberg', 852, 'Waldweg', '1', 3158, 'Guggisberg', 1004, 1020, 1110, 1, 110.0, 2590000.0, 1175000.0, ST_Point(2590000.0, 1175000.0)),
                -- 200905: House in mountain pass (NO transit within 10 min / 800m) -> EXCLUDED
                (200905::BIGINT, 'VS', 'Simplon', 6000, 'Passstrasse', '99', 3907, 'Simplon', 1004, 1020, 1110, 1, 130.0, 2640000.0, 1120000.0, ST_Point(2640000.0, 1120000.0))
        ) AS t(egid, canton, municipality, municipality_id, street, house_number, postal_code, city, gstat, gkat, gklas, num_dwellings, building_area_m2, gkode, gkodn, geom);
    """)

    # 2. Public Transport Stops Table (BAV Haltestellen ÖV)
    # Stops located within 150m-350m of our candidate houses (approx. 2-4 min walk)
    con.execute("""
        CREATE OR REPLACE TABLE transit_stops AS
        SELECT * FROM (
            VALUES
                ('Bern, Zytglogge', 'Tram', 2600250.0, 1199920.0, ST_Point(2600250.0, 1199920.0), 3.0, 'Bern', 2.0, 0, 'Direct bus/tram (0 transfers)'),
                ('Muri b. Bern, Bahnhof', 'Zug', 2603350.0, 1198420.0, ST_Point(2603350.0, 1198420.0), 10.0, 'Bern', 6.0, 0, 'Direct train (0 transfers)'),
                ('Köniz, Liebefeld Park', 'Bus', 2598750.0, 1197180.0, ST_Point(2598750.0, 1197180.0), 7.5, 'Bern', 8.0, 0, 'Direct bus/tram (0 transfers)'),
                ('Spiez, Schiffstation', 'Schiff', 2618450.0, 1170450.0, ST_Point(2618450.0, 1170450.0), 30.0, 'Bern', 25.0, 1, '1 bus + 1 train (1 transfer)'),
                ('Thun, Gwattzentrum', 'Bus', 2613420.0, 1176460.0, ST_Point(2613420.0, 1176460.0), 15.0, 'Bern', 19.0, 1, '1 bus + 1 train (1 transfer)'),
                ('Zürich, Fluntern', 'Tram', 2684150.0, 1248450.0, ST_Point(2684150.0, 1248450.0), 6.0, 'Zürich', 7.0, 0, 'Direct bus/tram (0 transfers)'),
                ('Küsnacht ZH, Bahnhof', 'Zug', 2686750.0, 1242250.0, ST_Point(2686750.0, 1242250.0), 15.0, 'Zürich', 12.0, 0, 'Direct train (0 transfers)'),
                ('Winterthur, Museumstr.', 'Bus', 2697150.0, 1262050.0, ST_Point(2697150.0, 1262050.0), 7.5, 'Winterthur', 4.0, 0, 'Direct bus/tram (0 transfers)'),
                ('Meilen, Bahnhof', 'Zug', 2691450.0, 1237150.0, ST_Point(2691450.0, 1237150.0), 15.0, 'Zürich', 17.0, 0, 'Direct train (0 transfers)'),
                ('Luzern, Casino-Palace', 'Bus', 2667050.0, 1212150.0, ST_Point(2667050.0, 1212150.0), 7.5, 'Luzern', 5.0, 0, 'Direct bus/tram (0 transfers)'),
                ('Horw, Zentrum', 'Bus', 2665750.0, 1208850.0, ST_Point(2665750.0, 1208850.0), 15.0, 'Luzern', 10.0, 0, 'Direct bus/tram (0 transfers)'),
                ('Lausanne, Ouchy', 'Metro', 2537150.0, 1152750.0, ST_Point(2537150.0, 1152750.0), 5.0, 'Lausanne', 4.0, 0, 'Direct bus/tram (0 transfers)'),
                ('Morges, Casino', 'Bus', 2528050.0, 1151150.0, ST_Point(2528050.0, 1151150.0), 10.0, 'Lausanne', 11.0, 0, 'Direct bus/tram (0 transfers)'),
                ('Genève, Parc Bertrand', 'Bus', 2501450.0, 1116150.0, ST_Point(2501450.0, 1116150.0), 7.5, 'Genève', 8.0, 0, 'Direct bus/tram (0 transfers)'),
                ('Cologny, Mairie', 'Bus', 2503150.0, 1118850.0, ST_Point(2503150.0, 1118850.0), 15.0, 'Genève', 12.0, 0, 'Direct bus/tram (0 transfers)'),
                ('Binningen, Kronenplatz', 'Tram', 2610450.0, 1265750.0, ST_Point(2610450.0, 1265750.0), 7.5, 'Basel', 9.0, 0, 'Direct bus/tram (0 transfers)')
        ) AS t(name, transit_type, gkode, gkodn, geom, cadence_min, nearest_big_city, transit_travel_time_min, transit_hops, transit_route_desc);
    """)

    # 3. Transit Quality Catchment Zones (ARE ÖV-Güteklassen)
    con.execute("""
        CREATE OR REPLACE TABLE transit_quality AS
        SELECT * FROM (
            VALUES
                ('A', ST_Buffer(ST_Point(2600200.0, 1199900.0), 5000.0)),  -- Bern & Muri & Köniz
                ('A', ST_Buffer(ST_Point(2684200.0, 1248500.0), 10000.0)), -- Zurich area
                ('B', ST_Buffer(ST_Point(2691500.0, 1237200.0), 4000.0)),  -- Meilen
                ('B', ST_Buffer(ST_Point(2697200.0, 1262100.0), 4000.0)),  -- Winterthur
                ('B', ST_Buffer(ST_Point(2667100.0, 1212200.0), 5000.0)),  -- Luzern & Horw
                ('B', ST_Buffer(ST_Point(2537200.0, 1152800.0), 8000.0)),  -- Lausanne & Morges
                ('A', ST_Buffer(ST_Point(2501500.0, 1116200.0), 5000.0)),  -- Geneva & Cologny
                ('B', ST_Buffer(ST_Point(2610500.0, 1265800.0), 4000.0)),  -- Basel & Binningen
                ('B', ST_Buffer(ST_Point(2618500.0, 1170500.0), 3000.0)),  -- Spiez
                ('B', ST_Buffer(ST_Point(2613500.0, 1176500.0), 3000.0))   -- Thun
        ) AS t(ov_class, geom);
    """)

    # 4. Restaurants & Gastronomy Locations Table (GWR Gastgewerbe / POI)
    # Restaurants located within 200m-400m of our candidate houses (approx. 2-5 min walk)
    con.execute("""
        CREATE OR REPLACE TABLE restaurants AS
        SELECT * FROM (
            VALUES
                (3001, 'Restaurant Harmonie', 'Hotelgasse', '3', 'Bern', 2600350.0, 1199920.0, ST_Point(2600350.0, 1199920.0)),
                (3002, 'Gasthof Muri', 'Kramgasse', '12', 'Muri', 2603320.0, 1198420.0, ST_Point(2603320.0, 1198420.0)),
                (3003, 'Pizzeria Köniz', 'Schwarzenburgstr.', '8', 'Köniz', 2598720.0, 1197150.0, ST_Point(2598720.0, 1197150.0)),
                (3004, 'Seerestaurant Spiez', 'Seestr.', '15', 'Spiez', 2618420.0, 1170420.0, ST_Point(2618420.0, 1170420.0)),
                (3005, 'Ristorante Gwatt', 'Gwattstr.', '10', 'Thun', 2613400.0, 1176420.0, ST_Point(2613400.0, 1176420.0)),
                (3006, 'Zürichberg Restaurant', 'Orellistr.', '20', 'Zürich', 2684120.0, 1248420.0, ST_Point(2684120.0, 1248420.0)),
                (3007, 'Restaurant Küsnacht', 'Seestr.', '80', 'Küsnacht', 2686720.0, 1242220.0, ST_Point(2686720.0, 1242220.0)),
                (3008, 'Wintirad Bistro', 'Lindstr.', '22', 'Winterthur', 2697120.0, 1262020.0, ST_Point(2697120.0, 1262020.0)),
                (3009, 'Gasthaus zum Löwen', 'Kirchgasse', '2', 'Meilen', 2691420.0, 1237120.0, ST_Point(2691420.0, 1237120.0)),
                (3010, 'Luzerner Stube', 'Haldenstr.', '8', 'Luzern', 2667020.0, 1212120.0, ST_Point(2667020.0, 1212120.0)),
                (3011, 'Horwer Hof', 'Kantonsstr.', '25', 'Horw', 2665720.0, 1208820.0, ST_Point(2665720.0, 1208820.0)),
                (3012, 'Café de Grancy', 'Rond-Point', '1', 'Lausanne', 2537120.0, 1152720.0, ST_Point(2537120.0, 1152720.0)),
                (3013, 'Auberge de Morges', 'Grand-Rue', '10', 'Morges', 2528020.0, 1151120.0, ST_Point(2528020.0, 1151120.0)),
                (3014, 'Bistrot de Genève', 'Route Florissant', '50', 'Genève', 2501420.0, 1116120.0, ST_Point(2501420.0, 1116120.0)),
                (3015, 'Auberge de Ruth', 'Chemin Ruth', '2', 'Cologny', 2503120.0, 1118820.0, ST_Point(2503120.0, 1118820.0)),
                (3016, 'Restaurant Schloss Bottmingen', 'Schlossgasse', '1', 'Binningen', 2610420.0, 1265720.0, ST_Point(2610420.0, 1265720.0))
        ) AS t(id, name, street, house_number, city, gkode, gkodn, geom);
    """)

    # 4b. Supermarkets & Grocery Stores Table (Migros, Coop, Denner, Volg, Aldi, Lidl)
    # Placed within 150m-350m of candidate houses (approx. 2-4 min walk)
    con.execute("""
        CREATE OR REPLACE TABLE supermarkets AS
        SELECT * FROM (
            VALUES
                (4001, 'Migros Supermarkt Bern Marktgasse', 'Migros', 'supermarket', 'Marktgasse', '10', 3011, 'Bern', 'BE', 2600230.0, 1199910.0, ST_Point(2600230.0, 1199910.0)),
                (4002, 'Coop Supermarkt Muri', 'Coop', 'supermarket', 'Thunstrasse', '30', 3074, 'Muri', 'BE', 2603380.0, 1198460.0, ST_Point(2603380.0, 1198460.0)),
                (4003, 'Denner Filiale Köniz', 'Denner', 'supermarket', 'Schwarzenburgstrasse', '15', 3097, 'Köniz', 'BE', 2598770.0, 1197160.0, ST_Point(2598770.0, 1197160.0)),
                (4004, 'Volg Dorfladen Spiez', 'Volg', 'supermarket', 'Oberlandstrasse', '5', 3700, 'Spiez', 'BE', 2618470.0, 1170440.0, ST_Point(2618470.0, 1170440.0)),
                (4005, 'Aldi Suisse Thun Gwatt', 'Aldi', 'supermarket', 'Gwattstrasse', '25', 3604, 'Thun', 'BE', 2613470.0, 1176460.0, ST_Point(2613470.0, 1176460.0)),
                (4006, 'Coop Supermarkt Zürichberg', 'Coop', 'supermarket', 'Zürichbergstrasse', '40', 8044, 'Zürich', 'ZH', 2684170.0, 1248460.0, ST_Point(2684170.0, 1248460.0)),
                (4007, 'Migros Supermarkt Küsnacht', 'Migros', 'supermarket', 'Seestrasse', '90', 8700, 'Küsnacht', 'ZH', 2686760.0, 1242250.0, ST_Point(2686760.0, 1242250.0)),
                (4008, 'Lidl Schweiz Winterthur', 'Lidl', 'supermarket', 'Museumstrasse', '10', 8400, 'Winterthur', 'ZH', 2697170.0, 1262060.0, ST_Point(2697170.0, 1262060.0)),
                (4009, 'Coop Supermarkt Meilen', 'Coop', 'supermarket', 'Kirchgasse', '12', 8706, 'Meilen', 'ZH', 2691470.0, 1237160.0, ST_Point(2691470.0, 1237160.0)),
                (4010, 'Migros Supermarkt Luzern Schweizerhof', 'Migros', 'supermarket', 'Haldenstrasse', '10', 6006, 'Luzern', 'LU', 2667060.0, 1212160.0, ST_Point(2667060.0, 1212160.0)),
                (4011, 'Denner Filiale Horw', 'Denner', 'supermarket', 'Kantonsstrasse', '30', 6048, 'Horw', 'LU', 2665760.0, 1208840.0, ST_Point(2665760.0, 1208840.0)),
                (4012, 'Coop Supermarkt Lausanne Grancy', 'Coop', 'supermarket', 'Boulevard de Grancy', '8', 1007, 'Lausanne', 'VD', 2537160.0, 1152760.0, ST_Point(2537160.0, 1152760.0)),
                (4013, 'Migros Supermarkt Morges', 'Migros', 'supermarket', 'Grand-Rue', '20', 1110, 'Morges', 'VD', 2528060.0, 1151160.0, ST_Point(2528060.0, 1151160.0)),
                (4014, 'Coop Supermarkt Genève Florissant', 'Coop', 'supermarket', 'Route de Florissant', '55', 1206, 'Genève', 'GE', 2501460.0, 1116160.0, ST_Point(2501460.0, 1116160.0)),
                (4015, 'Denner Filiale Cologny', 'Denner', 'supermarket', 'Chemin de Ruth', '8', 1223, 'Cologny', 'GE', 2503160.0, 1118850.0, ST_Point(2503160.0, 1118850.0)),
                (4016, 'Migros Supermarkt Binningen', 'Migros', 'supermarket', 'Hauptstrasse', '45', 4102, 'Binningen', 'BL', 2610460.0, 1265760.0, ST_Point(2610460.0, 1265760.0))
        ) AS t(id, name, brand, shop_type, street, house_number, postal_code, city, canton, gkode, gkodn, geom);
    """)

    # 4c. Gyms & Sports Facilities Table (Fitness, Sporthalle, CrossFit, Boulder)
    # Placed within 200m-500m of candidate houses (approx. 2-6 min walk)
    con.execute("""
        CREATE OR REPLACE TABLE gyms AS
        SELECT * FROM (
            VALUES
                (5001, 'Fitnesspark Bern City', 'gym', 'Aarbergergasse', '30', 3011, 'Bern', 'BE', 2600250.0, 1199920.0, ST_Point(2600250.0, 1199920.0)),
                (5002, 'Sporthalle Muri Gümligen', 'sports_hall', 'Moosstrasse', '15', 3074, 'Muri', 'BE', 2603420.0, 1198520.0, ST_Point(2603420.0, 1198520.0)),
                (5003, 'CrossFit Köniz', 'gym', 'Sägestrasse', '40', 3097, 'Liebefeld', 'BE', 2598820.0, 1197220.0, ST_Point(2598820.0, 1197220.0)),
                (5004, 'Sportzentrum Spiez', 'sports_centre', 'Schachenstrasse', '19', 3700, 'Spiez', 'BE', 2618520.0, 1170520.0, ST_Point(2618520.0, 1170520.0)),
                (5005, 'PureGym Thun Gwatt', 'gym', 'Gwattstrasse', '30', 3604, 'Thun', 'BE', 2613520.0, 1176520.0, ST_Point(2613520.0, 1176520.0)),
                (5006, 'Kieser Training Zürich Fluntern', 'gym', 'Zürichbergstrasse', '60', 8044, 'Zürich', 'ZH', 2684220.0, 1248520.0, ST_Point(2684220.0, 1248520.0)),
                (5007, 'Fitnessclub Küsnacht', 'gym', 'Seestrasse', '110', 8700, 'Küsnacht', 'ZH', 2686820.0, 1242320.0, ST_Point(2686820.0, 1242320.0)),
                (5008, 'Sporthalle Rennweg Winterthur', 'sports_hall', 'Museumstrasse', '15', 8400, 'Winterthur', 'ZH', 2697220.0, 1262120.0, ST_Point(2697220.0, 1262120.0)),
                (5009, 'Sportzentrum Allmend Meilen', 'sports_centre', 'Kirchgasse', '20', 8706, 'Meilen', 'ZH', 2691520.0, 1237220.0, ST_Point(2691520.0, 1237220.0)),
                (5010, 'Fitnesspark Allmend Luzern', 'gym', 'Haldenstrasse', '20', 6006, 'Luzern', 'LU', 2667120.0, 1212220.0, ST_Point(2667120.0, 1212220.0)),
                (5011, 'Sporthalle Horw', 'sports_hall', 'Kantonsstrasse', '45', 6048, 'Horw', 'LU', 2665820.0, 1208920.0, ST_Point(2665820.0, 1208920.0)),
                (5012, 'NonStop Gym Lausanne Gare', 'gym', 'Rue du Simplon', '10', 1007, 'Lausanne', 'VD', 2537220.0, 1152820.0, ST_Point(2537220.0, 1152820.0)),
                (5013, 'Centre Sportif Morges', 'sports_centre', 'Rue du Moulin', '8', 1110, 'Morges', 'VD', 2528120.0, 1151220.0, ST_Point(2528120.0, 1151220.0)),
                (5014, 'Harmony Fitness Genève Champel', 'gym', 'Route de Florissant', '80', 1206, 'Genève', 'GE', 2501520.0, 1116220.0, ST_Point(2501520.0, 1116220.0)),
                (5015, 'Centre Sportif de Cologny', 'sports_centre', 'Chemin de la Gradelle', '10', 1223, 'Cologny', 'GE', 2503220.0, 1118920.0, ST_Point(2503220.0, 1118920.0)),
                (5016, 'Sporthalle Binningen', 'sports_hall', 'Spiegelfeldstrasse', '15', 4102, 'Binningen', 'BL', 2610520.0, 1265820.0, ST_Point(2610520.0, 1265820.0))
        ) AS t(id, name, gym_type, street, house_number, postal_code, city, canton, gkode, gkodn, geom);
    """)

    con.execute("CREATE INDEX IF NOT EXISTS idx_gyms_coords ON gyms(gkode, gkodn);")

    # 5. Population Density Grid (STATPOP)
    con.execute("""
        CREATE OR REPLACE TABLE population_density AS
        SELECT * FROM (
            VALUES
                (2600200::BIGINT, 1199900::BIGINT, 40.0, ST_Point(2600200.0, 1199900.0)),
                (2603400::BIGINT, 1198500::BIGINT, 25.0, ST_Point(2603400.0, 1198500.0)),
                (2598800::BIGINT, 1197200::BIGINT, 30.0, ST_Point(2598800.0, 1197200.0)),
                (2618500::BIGINT, 1170500::BIGINT, 15.0, ST_Point(2618500.0, 1170500.0)),
                (2613500::BIGINT, 1176500::BIGINT, 22.0, ST_Point(2613500.0, 1176500.0)),
                (2684200::BIGINT, 1248500::BIGINT, 50.0, ST_Point(2684200.0, 1248500.0)),
                (2686800::BIGINT, 1242300::BIGINT, 20.0, ST_Point(2686800.0, 1242300.0)),
                (2697200::BIGINT, 1262100::BIGINT, 35.0, ST_Point(2697200.0, 1262100.0)),
                (2691500::BIGINT, 1237200::BIGINT, 18.0, ST_Point(2691500.0, 1237200.0)),
                (2667100::BIGINT, 1212200::BIGINT, 45.0, ST_Point(2667100.0, 1212200.0)),
                (2665800::BIGINT, 1208900::BIGINT, 28.0, ST_Point(2665800.0, 1208900.0)),
                (2537200::BIGINT, 1152800::BIGINT, 55.0, ST_Point(2537200.0, 1152800.0)),
                (2528100::BIGINT, 1151200::BIGINT, 30.0, ST_Point(2528100.0, 1151200.0)),
                (2501500::BIGINT, 1116200::BIGINT, 60.0, ST_Point(2501500.0, 1116200.0)),
                (2503200::BIGINT, 1118900::BIGINT, 18.0, ST_Point(2503200.0, 1118900.0)),
                (2610500::BIGINT, 1265800::BIGINT, 38.0, ST_Point(2610500.0, 1265800.0))
        ) AS t(grid_x, grid_y, population, geom);
    """)

    # 6. ARE Bauzonen (Wohnzonen 11)
    con.execute("""
        CREATE OR REPLACE TABLE building_zones AS
        SELECT * FROM (
            VALUES
                (11, 'Wohnzone', ST_Buffer(ST_Point(2600200.0, 1199900.0), 10000.0)),  -- Bern area
                (11, 'Wohnzone', ST_Buffer(ST_Point(2684200.0, 1248500.0), 15000.0)),  -- Zurich area
                (11, 'Wohnzone', ST_Buffer(ST_Point(2667100.0, 1212200.0), 10000.0)),  -- Luzern
                (11, 'Wohnzone', ST_Buffer(ST_Point(2537200.0, 1152800.0), 12000.0)),  -- Lausanne
                (11, 'Wohnzone', ST_Buffer(ST_Point(2501500.0, 1116200.0), 10000.0)),  -- Geneva
                (11, 'Wohnzone', ST_Buffer(ST_Point(2610500.0, 1265800.0), 8000.0))    -- Basel
        ) AS t(zone_code, zone_name, geom);
    """)

    # 7. Land Cover Green Surfaces (Vegetation, gardens, lawns)
    con.execute("""
        CREATE OR REPLACE TABLE land_cover_green AS
        SELECT * FROM (
            VALUES
                (ST_Buffer(ST_Point(2600200.0, 1199900.0), 40.0)),
                (ST_Buffer(ST_Point(2603400.0, 1198500.0), 50.0)),
                (ST_Buffer(ST_Point(2598800.0, 1197200.0), 45.0)),
                (ST_Buffer(ST_Point(2618500.0, 1170500.0), 60.0)),
                (ST_Buffer(ST_Point(2613500.0, 1176500.0), 40.0)),
                (ST_Buffer(ST_Point(2684200.0, 1248500.0), 55.0)),
                (ST_Buffer(ST_Point(2686800.0, 1242300.0), 65.0)),
                (ST_Buffer(ST_Point(2697200.0, 1262100.0), 45.0)),
                (ST_Buffer(ST_Point(2691500.0, 1237200.0), 50.0)),
                (ST_Buffer(ST_Point(2667100.0, 1212200.0), 50.0)),
                (ST_Buffer(ST_Point(2665800.0, 1208900.0), 45.0)),
                (ST_Buffer(ST_Point(2537200.0, 1152800.0), 50.0)),
                (ST_Buffer(ST_Point(2528100.0, 1151200.0), 40.0)),
                (ST_Buffer(ST_Point(2501500.0, 1116200.0), 45.0)),
                (ST_Buffer(ST_Point(2503200.0, 1118900.0), 70.0)),
                (ST_Buffer(ST_Point(2610500.0, 1265800.0), 45.0))
        ) AS t(geom);
    """)

    # 8. Cantonal Cadastral Gardens Table
    con.execute("""
        CREATE OR REPLACE TABLE canton_cadastral_gardens (
            egid BIGINT,
            garden_m2 DOUBLE,
            geom GEOMETRY
        );
    """)

    # 9. Schools & Daycares Table
    con.execute("""
        CREATE OR REPLACE TABLE schools_daycares AS
        SELECT * FROM (
            VALUES
                (6001, 'Primarschule Kirchenfeld', 'school', 'Aegertenstrasse', '40', 3005, 'Bern', 'BE', 2600300.0, 1199850.0, ST_Point(2600300.0, 1199850.0)),
                (6002, 'Kita Gümligen', 'daycare', 'Dorfstrasse', '10', 3073, 'Gümligen', 'BE', 2603350.0, 1198600.0, ST_Point(2603350.0, 1198600.0)),
                (6003, 'Primarschule Küsnacht', 'school', 'Schulhausstrasse', '5', 8700, 'Küsnacht', 'ZH', 2686750.0, 1242350.0, ST_Point(2686750.0, 1242350.0)),
                (6004, 'Kindergarten Zürichberg', 'daycare', 'Plattenstrasse', '20', 8032, 'Zürich', 'ZH', 2684150.0, 1248450.0, ST_Point(2684150.0, 1248450.0)),
                (6005, 'Collège de Pully', 'school', 'Chemin du Collège', '6', 1009, 'Pully', 'VD', 2541200.0, 1152400.0, ST_Point(2541200.0, 1152400.0)),
                (6006, 'Garderie de Pully', 'daycare', 'Chemin des Eterpeys', '5', 1009, 'Pully', 'VD', 2541150.0, 1152450.0, ST_Point(2541150.0, 1152450.0)),
                (6007, 'Ecole Primaire Lausanne', 'school', 'Avenue Ruchonnet', '12', 1003, 'Lausanne', 'VD', 2537250.0, 1152750.0, ST_Point(2537250.0, 1152750.0)),
                (6008, 'Crèche de Cologny', 'daycare', 'Route de Vandoeuvres', '8', 1223, 'Cologny', 'GE', 2503250.0, 1118850.0, ST_Point(2503250.0, 1118850.0)),
                (6009, 'Primarschule Binningen', 'school', 'Hauptstrasse', '22', 4102, 'Binningen', 'BL', 2610480.0, 1265850.0, ST_Point(2610480.0, 1265850.0))
        ) AS t(id, name, facility_type, street, house_number, postal_code, city, canton, gkode, gkodn, geom);
        CREATE INDEX IF NOT EXISTS idx_sd_coords ON schools_daycares(gkode, gkodn);
    """)

    # 10. Municipal Taxes Table
    con.execute("""
        CREATE OR REPLACE TABLE municipal_taxes AS
        SELECT * FROM (
            VALUES
                ('ZH', 261, 'Zürich', 8000, 119.0, 100.0, 'Average'),
                ('ZH', 154, 'Küsnacht (ZH)', 8700, 73.0, 100.0, 'Very Low'),
                ('ZH', NULL, 'Zürich Default', NULL, 100.0, 100.0, 'Average'),
                ('BE', 351, 'Bern', 3000, 154.0, 125.0, 'High'),
                ('BE', 356, 'Muri bei Bern', 3074, 118.0, 125.0, 'Average'),
                ('BE', NULL, 'Bern Default', NULL, 150.0, 125.0, 'High'),
                ('VD', 5590, 'Pully', 1009, 61.5, 120.0, 'Low'),
                ('VD', 5591, 'Lutry', 1095, 54.0, 120.0, 'Very Low'),
                ('VD', 5586, 'Lausanne', 1000, 78.5, 120.0, 'Above Average'),
                ('VD', NULL, 'Vaud Default', NULL, 70.0, 120.0, 'Above Average'),
                ('GE', 6616, 'Cologny', 1223, 28.0, 115.0, 'Very Low'),
                ('GE', NULL, 'Genève Default', NULL, 45.0, 115.0, 'Average'),
                ('SZ', 1323, 'Wollerau', 8832, 65.0, 60.0, 'Very Low'),
                ('SZ', NULL, 'Schwyz Default', NULL, 65.0, 60.0, 'Very Low'),
                ('ZG', 1711, 'Zug', 6300, 54.0, 55.0, 'Very Low'),
                ('ZG', NULL, 'Zug Default', NULL, 55.0, 55.0, 'Very Low'),
                ('LU', NULL, 'Luzern Default', NULL, 140.0, 110.0, 'Above Average'),
                ('BL', NULL, 'Basel-Landschaft Default', NULL, 60.0, 110.0, 'Above Average')
        ) AS t(canton, municipality_id, municipality, postal_code, tax_multiplier_pct, canton_tax_index, tax_tier);
        CREATE INDEX IF NOT EXISTS idx_taxes_canton ON municipal_taxes(canton);
    """)

    # 11. Natural Hazard Zones Table (Empty by default in seed so sample houses are safe)
    con.execute("""
        CREATE OR REPLACE TABLE natural_hazard_zones (
            id INTEGER,
            hazard_type VARCHAR,
            gkode DOUBLE,
            gkodn DOUBLE,
            geom GEOMETRY
        );
        CREATE INDEX IF NOT EXISTS idx_nhz_coords ON natural_hazard_zones(gkode, gkodn);
    """)

    total_buildings = con.execute("SELECT count(*) FROM buildings").fetchone()[0]
    con.close()
    logger.info("Sample database populated with %d buildings across Switzerland.", total_buildings)
    return total_buildings


if __name__ == "__main__":
    seed_sample_database()
