// conditions_queries.go contains SQL text for current-conditions reads.
// Layer: adapters/outbound/postgres conditions feature.
package postgres

const (
	queryCurrentConditions = `
WITH windowed AS (
  SELECT
    temperature_c,
    apparent_temperature_c,
    dewpoint_c,
    relative_humidity_percent,
    wind_speed_kmh,
    wind_direction_degrees,
    condition_code,
    is_day,
    observed_at
  FROM observations
  WHERE observed_at > CURRENT_TIMESTAMP - make_interval(mins => $1)
)
SELECT
  COUNT(*) AS sample_count,
  AVG(temperature_c) AS temperature_c,
  AVG(apparent_temperature_c) AS apparent_temperature_c,
  AVG(dewpoint_c) AS dewpoint_c,
  AVG(relative_humidity_percent) AS relative_humidity_percent,
  AVG(wind_speed_kmh) AS wind_speed_kmh,
  CASE
    WHEN atan2d(
      AVG(sind(wind_direction_degrees)),
      AVG(cosd(wind_direction_degrees))
    ) < 0
    THEN atan2d(
      AVG(sind(wind_direction_degrees)),
      AVG(cosd(wind_direction_degrees))
    ) + 360.0
    ELSE atan2d(
      AVG(sind(wind_direction_degrees)),
      AVG(cosd(wind_direction_degrees))
    )
  END AS wind_direction_degrees,
  MAX(condition_code) AS condition_code,
  (
    SELECT is_day
    FROM windowed
    ORDER BY observed_at DESC
    LIMIT 1
  ) AS is_day
FROM windowed`
)
