-- Collections table queries

-- name: GetCollection :one
SELECT id, site_id, container_html, created_at, updated_at, last_edited_by
FROM collections 
WHERE id = sqlc.arg(id) AND site_id = sqlc.arg(site_id);

-- name: GetAllCollections :many
SELECT id, site_id, container_html, created_at, updated_at, last_edited_by
FROM collections 
WHERE site_id = sqlc.arg(site_id)
ORDER BY updated_at DESC;

-- name: CreateCollection :one
INSERT INTO collections (id, site_id, container_html, last_edited_by)
VALUES (sqlc.arg(id), sqlc.arg(site_id), sqlc.arg(container_html), sqlc.arg(last_edited_by))
RETURNING id, site_id, container_html, created_at, updated_at, last_edited_by;

-- name: UpdateCollection :one
UPDATE collections 
SET container_html = sqlc.arg(container_html), last_edited_by = sqlc.arg(last_edited_by)
WHERE id = sqlc.arg(id) AND site_id = sqlc.arg(site_id)
RETURNING id, site_id, container_html, created_at, updated_at, last_edited_by;

-- name: UpsertCollection :one
INSERT INTO collections (id, site_id, container_html, last_edited_by)
VALUES (sqlc.arg(id), sqlc.arg(site_id), sqlc.arg(container_html), sqlc.arg(last_edited_by))
ON CONFLICT(id, site_id) DO UPDATE SET
    container_html = EXCLUDED.container_html,
    last_edited_by = EXCLUDED.last_edited_by
RETURNING id, site_id, container_html, created_at, updated_at, last_edited_by;

-- name: DeleteCollection :exec
DELETE FROM collections 
WHERE id = sqlc.arg(id) AND site_id = sqlc.arg(site_id);

-- name: DeleteAllSiteCollections :exec
DELETE FROM collections 
WHERE site_id = sqlc.arg(site_id);