-- name: CreateUser :one
INSERT INTO users (email, username, password_hash, first_name, last_name, theme, role)
VALUES ($1, $2, $3, $4, $5, $6, $7)
RETURNING id, email, username, theme, first_name, last_name, role, created_at, updated_at;

-- name: GetUserByEmail :one
SELECT id, email, username, theme, first_name, last_name, role, created_at, updated_at FROM users WHERE email = $1;

-- name: GetUserByUsername :one
SELECT id, email, username, theme, first_name, last_name, role, created_at, updated_at FROM users WHERE username = $1;

-- name: GetUserByEmailOrUsername :one
SELECT id, email, username, theme, first_name, last_name, role, created_at, updated_at FROM users WHERE email = $1 OR username = $1;

-- name: GetUserForLogin :one
SELECT id, email, username, password_hash, theme, first_name, last_name, role, created_at, updated_at FROM users WHERE email = $1 OR username = $1;

-- name: GetUser :one
SELECT id, email, username, theme, first_name, last_name, role, created_at, updated_at FROM users WHERE id = $1;

-- name: GetUserPasswordHash :one
SELECT password_hash FROM users WHERE id = $1;

-- name: ListUsers :many
SELECT id, email, username, theme, first_name, last_name, role, created_at, updated_at FROM users ORDER BY created_at DESC;

-- name: GetEbook :one
SELECT * FROM ebooks WHERE id = $1;

-- name: ListEbooks :many
SELECT * FROM ebooks ORDER BY created_at DESC LIMIT $1 OFFSET $2;

-- name: CreateEbook :one
INSERT INTO ebooks (title, author, isbn, description, file_path, file_size, mime_type, cover_image_path, series, series_number, tags, asin, date_published, publisher, contributors, added_by_admin_id)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16)
RETURNING *;

-- name: UpdateEbook :one
UPDATE ebooks SET
    title = $2,
    author = $3,
    isbn = $4,
    description = $5,
    cover_image_path = $6,
    series = $7,
    series_number = $8,
    tags = $9,
    asin = $10,
    date_published = $11,
    publisher = $12,
    contributors = $13,
    updated_at = NOW()
WHERE id = $1
RETURNING *;

-- name: DeleteEbook :exec
DELETE FROM ebooks WHERE id = $1;

-- name: GetReadingProgress :one
SELECT * FROM reading_progress WHERE ebook_id = $1 AND user_id = $2;

-- name: UpdateReadingProgress :one
INSERT INTO reading_progress (ebook_id, user_id, current_page, total_pages, last_read_at)
VALUES ($1, $2, $3, $4, NOW())
ON CONFLICT (ebook_id, user_id)
DO UPDATE SET
    current_page = EXCLUDED.current_page,
    total_pages = EXCLUDED.total_pages,
    last_read_at = NOW()
RETURNING *;

-- name: DeleteReadingProgress :exec
DELETE FROM reading_progress WHERE ebook_id = $1 AND user_id = $2;

-- name: UpdateUserTheme :exec
UPDATE users SET theme = $2, updated_at = NOW() WHERE id = $1;

-- name: UpdateUserProfile :exec
UPDATE users SET first_name = $2, last_name = $3, updated_at = NOW() WHERE id = $1;

-- name: UpdateUsername :exec
UPDATE users SET username = $2, updated_at = NOW() WHERE id = $1;

-- name: UpdateEmail :exec
UPDATE users SET email = $2, updated_at = NOW() WHERE id = $1;

-- name: UpdatePassword :exec
UPDATE users SET password_hash = $2, updated_at = NOW() WHERE id = $1;

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

-- name: UpdateScanSettings :exec
UPDATE users SET scan_frequency_minutes = $2, auto_scan_enabled = $3, updated_at = NOW() WHERE id = $1;

-- name: GetScanSettings :one
SELECT scan_frequency_minutes, auto_scan_enabled FROM users WHERE id = $1;

-- name: CreateEbookRating :one
INSERT INTO ebook_ratings (ebook_id, user_id, rating)
VALUES ($1, $2, $3)
ON CONFLICT (ebook_id, user_id)
DO UPDATE SET
    rating = EXCLUDED.rating,
    updated_at = NOW()
RETURNING *;

-- name: GetEbookRating :one
SELECT * FROM ebook_ratings WHERE ebook_id = $1 AND user_id = $2;

-- name: GetEbookRatings :many
SELECT er.*, u.username
FROM ebook_ratings er
JOIN users u ON er.user_id = u.id
WHERE er.ebook_id = $1
ORDER BY er.created_at DESC;

-- name: UpdateEbookRating :one
UPDATE ebook_ratings SET
    rating = $3,
    updated_at = NOW()
WHERE ebook_id = $1 AND user_id = $2
RETURNING *;

-- name: DeleteEbookRating :exec
DELETE FROM ebook_ratings WHERE ebook_id = $1 AND user_id = $2;

-- name: AddUserEbookFolder :one
INSERT INTO user_ebook_folders (user_id, folder_path) VALUES ($1, $2) RETURNING *;

-- name: GetUserEbookFolders :many
SELECT * FROM user_ebook_folders WHERE user_id = $1 ORDER BY created_at;

-- name: DeleteUserEbookFolder :one
DELETE FROM user_ebook_folders WHERE user_id = $1 AND folder_path = $2 RETURNING *;

-- name: GetEbookByFilePath :one
SELECT * FROM ebooks WHERE file_path = $1;