-- name: GetImageByID :one
SELECT id, storage_type, image_path
FROM images
WHERE id = $1;

-- name: CreateImage :one
INSERT INTO images (storage_type, image_path)
VALUES ($1, $2)
RETURNING id, storage_type, image_path;

-- name: GetUserByID :one
SELECT id, avatar_id, mail, nickname, disp_name, user_desc, creation_date
FROM users
WHERE id = $1;

-- -- name: ListUsers :many
-- SELECT user_id, avatar_id, passhash, mail, nickname, disp_name, user_desc, creation_date
-- FROM users
-- ORDER BY user_id
-- LIMIT $1 OFFSET $2;

-- -- name: CreateUser :one
-- INSERT INTO users (avatar_id, passhash, mail, nickname, disp_name, user_desc, creation_date)
-- VALUES ($1, $2, $3, $4, $5, $6, $7)
-- RETURNING user_id, avatar_id, nickname, disp_name, user_desc, creation_date;

-- -- name: UpdateUser :one
-- UPDATE users
-- SET
--   avatar_id = COALESCE(sqlc.narg('avatar_id'), avatar_id),
--   disp_name = COALESCE(sqlc.narg('disp_name'), disp_name),
--   user_desc = COALESCE(sqlc.narg('user_desc'), user_desc),
--   passhash  = COALESCE(sqlc.narg('passhash'), passhash)
-- WHERE user_id = sqlc.arg('user_id')
-- RETURNING user_id, avatar_id, nickname, disp_name, user_desc, creation_date;

-- -- name: DeleteUser :exec
-- DELETE FROM users
-- WHERE user_id = $1;

-- -- name: GetTitleByID :one
-- SELECT title_id, title_names, studio_id, poster_id, signal_ids,
--        title_status, rating, rating_count, release_year, release_season,
--        season, episodes_aired, episodes_all, episodes_len
-- FROM titles
-- WHERE title_id = $1;

-- -- name: ListTitles :many
-- SELECT title_id, title_names, studio_id, poster_id, signal_ids,
--        title_status, rating, rating_count, release_year, release_season,
--        season, episodes_aired, episodes_all, episodes_len
-- FROM titles
-- ORDER BY title_id
-- LIMIT $1 OFFSET $2;

-- -- name: UpdateTitle :one
-- UPDATE titles
-- SET
--   title_names = COALESCE(sqlc.narg('title_names'), title_names),
--   studio_id = COALESCE(sqlc.narg('studio_id'), studio_id),
--   poster_id = COALESCE(sqlc.narg('poster_id'), poster_id),
--   signal_ids = COALESCE(sqlc.narg('signal_ids'), signal_ids),
--   title_status = COALESCE(sqlc.narg('title_status'), title_status),
--   release_year = COALESCE(sqlc.narg('release_year'), release_year),
--   release_season = COALESCE(sqlc.narg('release_season'), release_season),
--   episodes_aired = COALESCE(sqlc.narg('episodes_aired'), episodes_aired),
--   episodes_all = COALESCE(sqlc.narg('episodes_all'), episodes_all),
--   episodes_len = COALESCE(sqlc.narg('episodes_len'), episodes_len)
-- WHERE title_id = sqlc.arg('title_id')
-- RETURNING *;

-- -- name: GetReviewByID :one
-- SELECT review_id, user_id, title_id, image_ids, review_text, creation_date
-- FROM reviews
-- WHERE review_id = $1;

-- -- name: CreateReview :one
-- INSERT INTO reviews (user_id, title_id, image_ids, review_text, creation_date)
-- VALUES ($1, $2, $3, $4, $5)
-- RETURNING review_id, user_id, title_id, image_ids, review_text, creation_date;

-- -- name: UpdateReview :one
-- UPDATE reviews
-- SET
--   image_ids = COALESCE(sqlc.narg('image_ids'), image_ids),
--   review_text = COALESCE(sqlc.narg('review_text'), review_text)
-- WHERE review_id = sqlc.arg('review_id')
-- RETURNING *;

-- -- name: DeleteReview :exec
-- DELETE FROM reviews
-- WHERE review_id = $1;

-- -- name: ListReviewsByTitle :many
-- SELECT review_id, user_id, title_id, image_ids, review_text, creation_date
-- FROM reviews
-- WHERE title_id = $1
-- ORDER BY creation_date DESC
-- LIMIT $2 OFFSET $3;

-- -- name: ListReviewsByUser :many
-- SELECT review_id, user_id, title_id, image_ids, review_text, creation_date
-- FROM reviews
-- WHERE user_id = $1
-- ORDER BY creation_date DESC
-- LIMIT $2 OFFSET $3;

-- -- name: GetUserTitle :one
-- SELECT usertitle_id, user_id, title_id, status, rate, review_id
-- FROM usertitles
-- WHERE user_id = $1 AND title_id = $2;

-- -- name: ListUserTitles :many
-- SELECT usertitle_id, user_id, title_id, status, rate, review_id
-- FROM usertitles
-- WHERE user_id = $1
-- ORDER BY usertitle_id
-- LIMIT $2 OFFSET $3;

-- -- name: CreateUserTitle :one
-- INSERT INTO usertitles (user_id, title_id, status, rate, review_id)
-- VALUES ($1, $2, $3, $4, $5)
-- RETURNING usertitle_id, user_id, title_id, status, rate, review_id;

-- -- name: UpdateUserTitle :one
-- UPDATE usertitles
-- SET
--   status = COALESCE(sqlc.narg('status'), status),
--   rate = COALESCE(sqlc.narg('rate'), rate),
--   review_id = COALESCE(sqlc.narg('review_id'), review_id)
-- WHERE user_id = $1 AND title_id = $2
-- RETURNING *;

-- -- name: DeleteUserTitle :exec
-- DELETE FROM usertitles
-- WHERE user_id = $1 AND ($2::int IS NULL OR title_id = $2);

-- -- name: ListTags :many
-- SELECT tag_id, tag_names
-- FROM tags
-- ORDER BY tag_id
-- LIMIT $1 OFFSET $2;