-- name: GetPublicNetworks :many
SELECT * FROM networks
WHERE is_public = true;

-- name: GetNetworkById :one
SELECT * FROM networks
WHERE id = ?;

-- name: CreateNetwork :one
INSERT INTO networks (
  id, owner_id, name, is_public,
  icon, bg_hex_color, fg_hex_color
) VALUES (
  ?, ?, ?, ?,
  ?, ?, ?
)
RETURNING *;

-- name: GetNetworkBannedUsers :many
SELECT
  sqlc.embed(users),
  users_networks.ban_reason
FROM users_networks
JOIN users ON users.id = users_networks.user_id
WHERE users_networks.network_id = ?;

-- name: GetNetworkMembers :many
SELECT
  sqlc.embed(users),
  users_networks.joined_at,
  users_networks.is_admin,
  users_networks.is_muted
FROM users_networks
JOIN users ON users.id = users_networks.user_id
WHERE users_networks.network_id = ? AND is_member = true;

-- name: SetNetworkName :one
UPDATE networks SET
  name = ?
WHERE id = ?
RETURNING *;

-- name: SetNetworkIcon :one
UPDATE networks SET
  icon = ?,
  bg_hex_color = ?,
  fg_hex_color = ?
WHERE id = ?
RETURNING *;

-- name: SetNetworkIsPublic :one
UPDATE networks SET
  is_public = ?
WHERE id = ?
RETURNING *;

-- name: TransferNetwork :one
UPDATE networks SET
  owner_id = ?
WHERE id = ?
RETURNING *;

-- name: DeleteNetwork :exec
DELETE FROM networks WHERE id = ?;

-- name: SetNetworkUser :one
INSERT INTO users_networks (
  user_id, network_id,
  is_member, is_admin, is_muted,
  is_banned, ban_reason
) VALUES (
  ?1, ?2,
  ?3, ?4, ?5,
  ?6, ?7
)
ON CONFLICT DO 
UPDATE SET
  is_member = ?3, is_admin = ?4, is_muted = ?5,
  is_banned = ?6, ban_reason = ?7
WHERE user_id = ?1 AND network_id = ?2
RETURNING *;
