-- name: SelectSitesOwnedByUser :many
SELECT * FROM sites WHERE owner_id = ? ORDER BY title ASC;

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

-- name: SelectSiteByGUID :one
SELECT * FROM sites WHERE guid = ?;

-- name: InsertSite :one
INSERT INTO sites (
    owner_id,
    guid,
    title,
    tagline,
    timezone,
    created_at
) VALUES (?, ?, ?, ?, ?, ?)
RETURNING id;

-- name: HasUsersAndSites :one
SELECT (SELECT COUNT(*) FROM users) > 0 AND (SELECT COUNT(*) FROM sites) > 0 AS has_users_and_sites;

-- name: UpdateSite :exec
UPDATE sites SET title = ?, tagline = ?, timezone = ? WHERE id = ?;

-- name: SelectAllSitesWithOwners :many
SELECT s.id, s.guid, s.title, s.owner_id, u.username
FROM sites s
JOIN users u ON s.owner_id = u.id
ORDER BY s.title ASC;