-- name: CreateTarget :one
INSERT INTO targets (name, type, config)
VALUES ($1, $2, $3)
RETURNING *;

-- name: GetTarget :one
SELECT * FROM targets
WHERE id = $1;

-- name: CreateCheck :one
INSERT INTO checks (target_id, type, interval_seconds, timeout_seconds)
VALUES ($1, $2, $3, $4)
RETURNING *;

-- name: GetCheck :one
SELECT * FROM checks
WHERE id = $1;

-- name: ListChecks :many
SELECT * FROM checks
WHERE is_active = TRUE;

-- name: CreateMetric :one
INSERT INTO metrics (time, check_id, value, tags)
VALUES ($1, $2, $3, $4)
RETURNING *;

-- name: GetMetricsForCheck :many
SELECT * FROM metrics
WHERE check_id = $1 AND time >= $2 AND time <= $3
ORDER BY time DESC;

-- name: CreateAlertRule :one
INSERT INTO alert_rules (check_id, name, threshold, operator, for_duration, severity)
VALUES ($1, $2, $3, $4, $5, $6)
RETURNING *;

-- name: ListAlertRules :many
SELECT * FROM alert_rules;

-- name: CreateIncident :one
INSERT INTO incidents (alert_rule_id, status, start_time)
VALUES ($1, $2, $3)
RETURNING *;

-- name: GetIncident :one
SELECT * FROM incidents
WHERE id = $1;

-- name: UpdateIncidentStatus :one
UPDATE incidents
SET status = $2, updated_at = NOW()
WHERE id = $1
RETURNING *;
