-- Tennis court locations from OpenStreetMap via Overpass API (sport=tennis).
-- Used as a "racket sport culture" signal in the opportunity score:
-- areas with high tennis court density are prime padel adoption markets.
--
-- Source: data/landing/overpass_tennis/{year}/{month}/courts.jsonl.gz
-- Format: one OSM element per line; nodes have lat/lon directly,
--         ways/relations have center.lat/center.lon (Overpass out center)

MODEL (
  name staging.stg_tennis_courts,
  kind FULL,
  cron '@daily',
  grain osm_id
);

WITH
parsed AS (
  SELECT
    type                                                          AS osm_type,
    TRY_CAST(id AS BIGINT)                                        AS osm_id,
    -- Nodes: lat/lon direct. Ways/relations: center object (Overpass out center).
    COALESCE(
      TRY_CAST(lat AS DOUBLE),
      TRY_CAST(center ->> 'lat' AS DOUBLE)
    )                                                             AS lat,
    COALESCE(
      TRY_CAST(lon AS DOUBLE),
      TRY_CAST(center ->> 'lon' AS DOUBLE)
    )                                                             AS lon,
    tags ->> 'name'                                               AS name,
    tags ->> 'addr:country'                                       AS country_code,
    tags ->> 'addr:city'                                          AS city_tag,
    filename                                                      AS source_file,
    CURRENT_DATE                                                  AS extracted_date
  FROM read_json(
    @LANDING_DIR || '/overpass_tennis/*/*/courts.jsonl.gz',
    format = 'newline_delimited',
    columns = {
      type: 'VARCHAR', id: 'BIGINT', lat: 'DOUBLE', lon: 'DOUBLE',
      center: 'JSON', tags: 'JSON'
    },
    filename = true
  )
  WHERE type IS NOT NULL
),
deduped AS (
  SELECT *,
    ROW_NUMBER() OVER (PARTITION BY osm_id ORDER BY extracted_date DESC) AS rn
  FROM parsed
  WHERE lat IS NOT NULL AND lon IS NOT NULL
    AND lat BETWEEN -90  AND 90
    AND lon BETWEEN -180 AND 180
),
with_country AS (
  SELECT
    osm_id, lat, lon,
    COALESCE(NULLIF(TRIM(UPPER(country_code)), ''), CASE
      WHEN lat BETWEEN 47.27 AND 55.06 AND lon BETWEEN  5.87 AND 15.04 THEN 'DE'
      WHEN lat BETWEEN 35.95 AND 43.79 AND lon BETWEEN -9.39 AND  4.33 THEN 'ES'
      WHEN lat BETWEEN 49.90 AND 60.85 AND lon BETWEEN -8.62 AND  1.77 THEN 'GB'
      WHEN lat BETWEEN 41.36 AND 51.09 AND lon BETWEEN -5.14 AND  9.56 THEN 'FR'
      WHEN lat BETWEEN 45.46 AND 47.80 AND lon BETWEEN  5.96 AND 10.49 THEN 'CH'
      WHEN lat BETWEEN 46.37 AND 49.02 AND lon BETWEEN  9.53 AND 17.16 THEN 'AT'
      WHEN lat BETWEEN 36.35 AND 47.09 AND lon BETWEEN  6.62 AND 18.51 THEN 'IT'
      WHEN lat BETWEEN 37.00 AND 42.15 AND lon BETWEEN -9.50 AND -6.19 THEN 'PT'
      ELSE NULL
    END)                       AS country_code,
    NULLIF(TRIM(name), '')     AS name,
    NULLIF(TRIM(city_tag), '') AS city,
    extracted_date
  FROM deduped
  WHERE rn = 1
)
SELECT
  osm_id,
  lat,
  lon,
  country_code,
  name,
  city,
  extracted_date
FROM with_country
