-- 0013_weather_wttr.sql — 天气表支持 wttr provider
-- 原 CHECK 只允许 qweather/openweathermap，扩展到包含 wttr
-- SQLite 无法直接 ALTER CHECK，需重建表

CREATE TABLE IF NOT EXISTS _weather_snapshots_new (
    id              INTEGER PRIMARY KEY AUTOINCREMENT,
    snapshot_date   TEXT    NOT NULL,
    city            TEXT    NOT NULL,
    provider        TEXT    NOT NULL CHECK (provider IN ('wttr','qweather','openweathermap')),
    temp_c          REAL,
    humidity        INTEGER,
    weather_desc    TEXT,
    weather_code    TEXT,
    wind_kph        REAL,
    precip_mm       REAL,
    raw_json        TEXT,
    fetched_at      TEXT    NOT NULL DEFAULT (datetime('now'))
);
INSERT INTO _weather_snapshots_new
    (id, snapshot_date, city, provider, temp_c, humidity, weather_desc, weather_code, wind_kph, precip_mm, raw_json, fetched_at)
    SELECT id, snapshot_date, city, provider, temp_c, humidity, weather_desc, weather_code, wind_kph, precip_mm, raw_json, fetched_at
    FROM weather_snapshots;
DROP TABLE weather_snapshots;
ALTER TABLE _weather_snapshots_new RENAME TO weather_snapshots;
CREATE UNIQUE INDEX IF NOT EXISTS uk_weather_city_date ON weather_snapshots(city, snapshot_date);
CREATE INDEX IF NOT EXISTS idx_weather_date ON weather_snapshots(snapshot_date);
