import breezyweather.domain.weather.reference.AlertSeverity;

CREATE TABLE alerts(
    _id INTEGER NOT NULL PRIMARY KEY,
    location_formatted_id TEXT NOT NULL,
    alert_id TEXT NOT NULL,
    start_date INTEGER,
    end_date INTEGER,
    headline TEXT,
    description TEXT,
    instruction TEXT,
    source TEXT,
    severity INTEGER AS AlertSeverity NOT NULL,
    color INTEGER NOT NULL,

    FOREIGN KEY(location_formatted_id) REFERENCES locations (formatted_id)
    ON DELETE CASCADE
);

CREATE INDEX alerts_location_formatted_id_index ON alerts(location_formatted_id);

getAlertListByLocationId:
SELECT alert_id, start_date, end_date, headline, description, instruction, source, severity, color
FROM alerts
WHERE location_formatted_id = :locationFormattedId
ORDER BY severity DESC, start_date ASC;

getCurrentAlertsByLocationId:
SELECT alert_id, start_date, end_date, headline, description, instruction, source, severity, color
FROM alerts
WHERE location_formatted_id = :locationFormattedId
AND (end_date IS NULL OR end_date > :now)
ORDER BY severity DESC, start_date ASC;

insert:
INSERT INTO alerts(location_formatted_id, alert_id, start_date, end_date, headline, description, instruction, source, severity, color)
VALUES (:locationFormattedId, :alertId, :startDate, :endDate, :headline, :description, :instruction, :source, :severity, :color);

deleteAlertListForLocationId:
DELETE FROM alerts
WHERE location_formatted_id = :locationFormattedId;
