-- name: GetBot :one
SELECT * FROM bots
WHERE id = $1;

-- name: CreateBot :one
INSERT INTO bots (
	id,
  username,
  avatar,
  overview,
  description,
  is_slash,
  install_context,
  guild_count,
  install_count,
  imported_from,
  prefix,
  main_owner_id
)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12)
RETURNING *;

-- name: UpdateBot :one
UPDATE bots
SET
  username      = coalesce(sqlc.narg('username'),      username),
  avatar        = coalesce(sqlc.narg('avatar'),        avatar),
  overview      = coalesce(sqlc.narg('overview'),      overview),
  description   = coalesce(sqlc.narg('description'),   description),
  is_slash      = coalesce(sqlc.narg('is_slash'),      is_slash),
  install_context = coalesce(sqlc.narg('install_context'), install_context),
  guild_count   = coalesce(sqlc.narg('guild_count'),   guild_count),
  install_count = coalesce(sqlc.narg('install_count'), install_count),
  imported_from = coalesce(sqlc.narg('imported_from'), imported_from),
  prefix        = coalesce(sqlc.narg('prefix'),        prefix),
  main_owner_id = coalesce(sqlc.narg('main_owner_id'), main_owner_id),
  updated_at    = now()
WHERE id = sqlc.arg('id')
RETURNING *;

-- name: UpdateBotStatus :one
UPDATE bots
SET
  status     = $2,
  updated_at = now()
WHERE id = $1
RETURNING *;

-- name: CountBotsByUsername :one
SELECT COUNT(*) FROM bots
WHERE (@query::text IS NULL OR username ILIKE '%' || @query || '%') AND status = $1;

-- name: SearchBotsByUsername :many
SELECT * FROM bots
WHERE (@query::text IS NULL OR username ILIKE '%' || @query || '%')
ORDER BY created_at DESC
LIMIT  $1
OFFSET $2;

-- name: ListBots :many
SELECT * FROM bots
ORDER BY id
LIMIT  $1
OFFSET $2;

-- name: ListBotsByStatus :many
SELECT * FROM bots
WHERE status = $1
ORDER BY id
LIMIT  $2
OFFSET $3;

-- name: ListBotsByOwner :many
SELECT * FROM bots
WHERE main_owner_id = $1
ORDER BY id
LIMIT  $2
OFFSET $3;

-- name: DeleteBot :exec
DELETE FROM bots
WHERE id = $1;
