-- name: CreateSite :one
INSERT INTO sites (user_id, domain, is_custom_domain)
VALUES (?, ?, ?)
RETURNING *;

-- name: GetSiteByID :one
SELECT * FROM sites
WHERE id = ?;

-- name: GetSitesByUserID :many
SELECT * FROM sites
WHERE user_id = ?
ORDER BY created_at DESC;

-- name: GetSiteByDomain :one
SELECT * FROM sites
WHERE domain = ?;

-- name: DeleteSite :exec
DELETE FROM sites
WHERE id = ?;

-- name: GetSiteCountByUserID :one
SELECT COUNT(*) as count FROM sites
WHERE user_id = ?;

-- name: DeleteSiteByUserIDAndDomain :exec
DELETE FROM sites
WHERE user_id = ? AND domain = ?;

-- name: GetAllSites :many
SELECT * FROM sites
ORDER BY domain ASC;

-- name: GetAllSiteDomains :many
SELECT domain FROM sites
ORDER BY domain ASC;

-- name: UpsertSiteByDomain :one
INSERT INTO sites (user_id, domain, is_custom_domain, owner_oidc_subject)
VALUES (?, ?, ?, ?)
ON CONFLICT(domain) DO UPDATE SET
    user_id = excluded.user_id,
    owner_oidc_subject = excluded.owner_oidc_subject,
    updated_at = CURRENT_TIMESTAMP
RETURNING *;

-- name: DeleteSiteByDomain :exec
DELETE FROM sites
WHERE domain = ?;

-- name: GetSitesByOwnerOIDCSubject :many
SELECT * FROM sites
WHERE owner_oidc_subject = ?
ORDER BY domain ASC;

-- name: ReassignSitesByOwnerOIDCSubject :exec
UPDATE sites
SET user_id = ?, updated_at = CURRENT_TIMESTAMP
WHERE owner_oidc_subject = ?;

-- name: UpdateSiteOwnership :exec
UPDATE sites
SET user_id = ?, owner_oidc_subject = ?, updated_at = CURRENT_TIMESTAMP
WHERE domain = ?;
