-- name: InsertBadge :one
INSERT INTO public.badges (id, name, description, color) 
VALUES 
  (
    uuid_generate_v4(), 
    $1, 
    $2,
    $3
  )
RETURNING *;

-- name: UpdateBadge :one
UPDATE public.badges 
SET 
  name = $2, 
  description = $3,
  color = $4
WHERE 
  id = $1
RETURNING *;

-- name: DeleteBadge :exec
DELETE FROM public.badges 
WHERE 
  id = $1;

-- name: GetAllBadges :many
SELECT * 
FROM public.badges;

-- name: GetBadgeByID :one
SELECT * 
FROM public.badges 
WHERE id = $1;

-- name: AddBadgeToUser :one
INSERT INTO public.users_badges (user_id, badge_id) 
VALUES 
  (
    $1, 
    $2
  )
RETURNING *;

-- name: RemoveBadgeFromUser :exec
DELETE FROM public.users_badges 
WHERE 
  user_id = $1 
  AND badge_id = $2;

-- name: GetUserBadges :many
SELECT b.* 
FROM public.badges b 
JOIN public.users_badges ub ON b.id = ub.badge_id 
WHERE ub.user_id = $1;

-- name: GetUsersWithBadge :many
SELECT u.* 
FROM public.users u 
JOIN public.users_badges ub ON u.user_id = ub.user_id 
WHERE ub.badge_id = $1;