-- name: CreateUser :one
INSERT INTO users (
    email,
    password_hash,
    role,
    full_name
) VALUES (
    $1,
    $2,
    $3,
    $4
)
RETURNING *;

-- name: GetUserByID :one
SELECT *
FROM users
WHERE id = $1;

-- name: GetAuthUserByID :one
SELECT
    u.id AS user_id,
    u.email AS user_email,
    u.role AS user_role,
    u.full_name AS user_full_name,
    u.is_active AS user_is_active,
    u.created_at AS user_created_at,
    u.updated_at AS user_updated_at,
    p.user_id AS profile_user_id,
    p.preferred_name,
    p.profile_icon_url,
    p.headline,
    p.bio,
    p.timezone,
    p.locale,
    p.grade_level,
    p.learning_goal,
    p.created_at AS profile_created_at,
    p.updated_at AS profile_updated_at
FROM users u
LEFT JOIN profiles p ON p.user_id = u.id
WHERE u.id = $1;

-- name: GetUserByEmail :one
SELECT *
FROM users
WHERE email = $1;

-- name: GetAuthUserByEmail :one
SELECT
    u.id AS user_id,
    u.email AS user_email,
    u.role AS user_role,
    u.full_name AS user_full_name,
    u.is_active AS user_is_active,
    u.password_hash AS user_password_hash,
    u.created_at AS user_created_at,
    u.updated_at AS user_updated_at,
    p.user_id AS profile_user_id,
    p.preferred_name,
    p.profile_icon_url,
    p.headline,
    p.bio,
    p.timezone,
    p.locale,
    p.grade_level,
    p.learning_goal,
    p.created_at AS profile_created_at,
    p.updated_at AS profile_updated_at
FROM users u
LEFT JOIN profiles p ON p.user_id = u.id
WHERE u.email = $1;

-- name: ListUsersByRole :many
SELECT *
FROM users
WHERE role = $1
ORDER BY full_name ASC;

-- name: ListUsersWithProfileByRole :many
SELECT
    u.id AS user_id,
    u.email AS user_email,
    u.role AS user_role,
    u.full_name AS user_full_name,
    u.is_active AS user_is_active,
    u.created_at AS user_created_at,
    u.updated_at AS user_updated_at,
    p.user_id AS profile_user_id,
    p.preferred_name,
    p.profile_icon_url,
    p.headline,
    p.bio,
    p.timezone,
    p.locale,
    p.grade_level,
    p.learning_goal,
    p.created_at AS profile_created_at,
    p.updated_at AS profile_updated_at
FROM users u
LEFT JOIN profiles p ON p.user_id = u.id
WHERE u.role = $1
ORDER BY u.full_name ASC;

-- name: GetUserWithProfileByID :one
SELECT
    u.id AS user_id,
    u.email AS user_email,
    u.role AS user_role,
    u.full_name AS user_full_name,
    u.is_active AS user_is_active,
    u.created_at AS user_created_at,
    u.updated_at AS user_updated_at,
    p.user_id AS profile_user_id,
    p.preferred_name,
    p.profile_icon_url,
    p.headline,
    p.bio,
    p.timezone,
    p.locale,
    p.grade_level,
    p.learning_goal,
    p.created_at AS profile_created_at,
    p.updated_at AS profile_updated_at
FROM users u
LEFT JOIN profiles p ON p.user_id = u.id
WHERE u.id = $1;

-- name: UpdateUserActiveStatus :one
UPDATE users
SET
    is_active = $2,
    updated_at = NOW()
WHERE id = $1
RETURNING *;

-- name: UpdateUserFullName :one
UPDATE users
SET
    full_name = $2,
    updated_at = NOW()
WHERE id = $1
RETURNING *;

-- name: UpsertUserProfile :one
INSERT INTO profiles (
    user_id,
    preferred_name,
    profile_icon_url,
    headline,
    bio,
    timezone,
    locale,
    grade_level,
    learning_goal
) VALUES (
    $1,
    $2,
    $3,
    $4,
    $5,
    $6,
    $7,
    $8,
    $9
)
ON CONFLICT (user_id) DO UPDATE
SET
    preferred_name = EXCLUDED.preferred_name,
    profile_icon_url = EXCLUDED.profile_icon_url,
    headline = EXCLUDED.headline,
    bio = EXCLUDED.bio,
    timezone = EXCLUDED.timezone,
    locale = EXCLUDED.locale,
    grade_level = EXCLUDED.grade_level,
    learning_goal = EXCLUDED.learning_goal,
    updated_at = NOW()
RETURNING *;
