-- name: GetImcUserByUsername :one
SELECT * FROM imc_users WHERE username = ?;

-- name: GetImcUserByID :one
SELECT * FROM imc_users WHERE id = ?;

-- name: CreateImcUser :exec
INSERT INTO imc_users (username, password_hash, role, domain_id) VALUES (?, ?, ?, ?);

-- name: UpdateImcUserPassword :exec
UPDATE imc_users SET password_hash = ? WHERE id = ?;

-- name: DeleteImcUser :exec
DELETE FROM imc_users WHERE id = ?;

-- name: GetUsersForDomain :many
SELECT imc_users.* FROM imc_users WHERE domain_id = ?;

-- name: GetUsersForDomainViaJoin :many
SELECT imc_users.* FROM imc_users 
JOIN imc_users2domains ON imc_users.id = imc_users2domains.user_id 
WHERE imc_users2domains.domain_id = ?;

-- name: AddUserToDomain :exec
INSERT INTO imc_users2domains (user_id, domain_id) VALUES (?, ?);

-- name: RemoveUserFromDomain :exec
DELETE FROM imc_users2domains WHERE user_id = ? AND domain_id = ?;

-- name: IsUserInDomain :one
SELECT COUNT(*) as count FROM imc_users2domains WHERE user_id = ? AND domain_id = ?;

-- name: GetAdditionalDomainsForUser :many
SELECT virtual_domains.* FROM virtual_domains 
JOIN imc_users2domains ON virtual_domains.id = imc_users2domains.domain_id 
WHERE imc_users2domains.user_id = ?;
