-- Playtomic padel venue records — full metadata extraction.
-- Reads landing zone tenants files, extracts all venue metadata
-- including address, opening hours, court resources, VAT rate, and facilities.
-- Deduplicates on tenant_id (keeps most recent extraction).
--
-- Source: data/landing/playtomic/{year}/{month}/{day}/tenants.jsonl.gz

MODEL (
  name staging.stg_playtomic_venues,
  kind FULL,
  cron '@daily',
  grain tenant_id
);

WITH
parsed AS (
  SELECT
    tenant_id,
    tenant_name,
    slug,
    tenant_type,
    tenant_status,
    playtomic_status,
    booking_type,
    address ->> 'street'                                          AS street,
    address ->> 'city'                                            AS city,
    address ->> 'postal_code'                                     AS postal_code,
    UPPER(address ->> 'country_code')                             AS country_code,
    address ->> 'timezone'                                        AS timezone,
    address ->> 'administrative_area'                             AS administrative_area,
    TRY_CAST(address -> 'coordinate' ->> 'lat' AS DOUBLE)         AS lat,
    TRY_CAST(address -> 'coordinate' ->> 'lon' AS DOUBLE)         AS lon,
    TRY_CAST(vat_rate AS DOUBLE)                                  AS vat_rate,
    default_currency,
    TRY_CAST(booking_settings ->> 'booking_ahead_limit' AS INTEGER) AS booking_ahead_limit_minutes,
    opening_hours                                                 AS opening_hours_json,
    resources                                                     AS resources_json,
    created_at,
    CAST(is_playtomic_partner AS VARCHAR)                         AS is_playtomic_partner_raw,
    filename                                                      AS source_file,
    CURRENT_DATE                                                  AS extracted_date
  FROM read_json(
    @LANDING_DIR || '/playtomic/*/*/*/tenants.jsonl.gz',
    format = 'newline_delimited',
    filename = true,
    columns = {
      tenant_id: 'VARCHAR', tenant_name: 'VARCHAR', slug: 'VARCHAR',
      tenant_type: 'VARCHAR', tenant_status: 'VARCHAR', playtomic_status: 'VARCHAR',
      booking_type: 'VARCHAR', address: 'JSON', vat_rate: 'DOUBLE',
      default_currency: 'VARCHAR', booking_settings: 'JSON',
      opening_hours: 'JSON', resources: 'JSON',
      created_at: 'VARCHAR', is_playtomic_partner: 'VARCHAR'
    }
  )
  WHERE tenant_id IS NOT NULL
),
deduped AS (
  SELECT *,
    ROW_NUMBER() OVER (PARTITION BY tenant_id ORDER BY source_file DESC) AS rn
  FROM parsed
  WHERE tenant_id IS NOT NULL
    AND lat IS NOT NULL AND lon IS NOT NULL
    AND lat BETWEEN -90 AND 90
    AND lon BETWEEN -180 AND 180
)
SELECT
  tenant_id,
  'playtomic'                                     AS source,
  NULLIF(TRIM(tenant_name), '')                   AS name,
  slug,
  tenant_type,
  tenant_status,
  playtomic_status,
  booking_type,
  street,
  NULLIF(TRIM(city), '')                          AS city,
  postal_code                                     AS postcode,
  country_code,
  timezone,
  administrative_area,
  lat,
  lon,
  vat_rate,
  default_currency,
  booking_ahead_limit_minutes,
  opening_hours_json,
  resources_json,
  created_at,
  CASE WHEN LOWER(is_playtomic_partner_raw) IN ('true', '1') THEN TRUE ELSE FALSE END AS is_playtomic_partner,
  extracted_date
FROM deduped
WHERE rn = 1
