-- name: CreateGoogleUser :one
INSERT INTO users (
    first_name,
    last_name,
    email,
    google_id,
    google_email_verified,
    role,
    status,
    email_verified,
    profile_picture_url,
    profile_completed
)
VALUES (
    $1, $2, $3, $4, $5, $6, $7, true, $8, false
)
RETURNING *;

-- name: LinkGoogleAccount :exec
UPDATE users
SET
    google_id = $2,
    google_email_verified = true,
    updated_at = CURRENT_TIMESTAMP
WHERE id = $1;

-- name: IsUserPending :one
SELECT 
    CASE WHEN status = 'PENDING' THEN true ELSE false END AS is_pending
FROM users
WHERE id = $1
LIMIT 1;


-- name: IsProfileCompleted :one
SELECT 
    CASE WHEN profile_completed = true THEN true ELSE false END AS is_pending
FROM users
WHERE id = $1
LIMIT 1;


-- name: IsUserNameUnique :one
SELECT 
    CASE WHEN COUNT(*) = 0 THEN true ELSE false END AS is_unique
FROM users
WHERE id = $1;



-- name: CreateUser :one
INSERT INTO users (
    first_name,
    last_name,
    gender,
    birth_day,
    email,
    phone_number,
    role,
    password,
    age_group,
    education_level,
    country,
    region,

    nick_name,
    occupation,
    learning_goal,
    language_goal,
    language_challange,
    favourite_topic,

    initial_assessment_completed,
    email_verified,
    phone_verified,
    status,
    profile_completed,
    profile_picture_url,
    preferred_language,
    updated_at
)
VALUES (
    $1,
    $2,
    $3,
    $4,
    $5,
    $6,
    $7,
    $8,
    $9,   -- age_group
    $10,
    $11,
    $12,

    $13,
    $14,
    $15,
    $16,
    $17,
    $18,

    $19,
    $20,
    $21,
    $22,
    $23,
    $24,
    $25,
    CURRENT_TIMESTAMP
)
RETURNING
    id,
    first_name,
    last_name,
    gender,
    birth_day,
    email,
    phone_number,
    role,
    age_group,
    education_level,
    country,
    region,

    nick_name,
    occupation,
    learning_goal,
    language_goal,
    language_challange,
    favourite_topic,

    initial_assessment_completed,
    email_verified,
    phone_verified,
    status,
    profile_completed,
    profile_picture_url,
    preferred_language,
    created_at,
    updated_at;


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

-- name: GetUserByGoogleID :one
SELECT *
FROM users
WHERE google_id = $1;


-- name: GetAllUsers :many
SELECT
    COUNT(*) OVER () AS total_count,
    id,
    first_name,
    last_name,
    gender,
    birth_day,
    email,
    phone_number,
    role,
    age_group,
    education_level,
    country,
    region,
    knowledge_level,
    nick_name,
    occupation,
    learning_goal,
    language_goal,
    language_challange,
    favourite_topic,
    initial_assessment_completed,
    email_verified,
    phone_verified,
    status,
    last_login,
    profile_completed,
    profile_picture_url,
    preferred_language,
    created_at,
    updated_at
FROM users
LIMIT sqlc.narg('limit')::INT
OFFSET sqlc.narg('offset')::INT;


-- name: GetTotalUsers :one
SELECT COUNT(*)
FROM users
WHERE (role = $1 OR $1 IS NULL);


-- name: SearchUserByNameOrPhone :many
SELECT
    id,
    first_name,
    last_name,
    gender,
    birth_day,
    email,
    phone_number,
    role,
    age_group,
    education_level,
    country,
    region,

    nick_name,
    occupation,
    learning_goal,
    language_goal,
    language_challange,
    favourite_topic,

    initial_assessment_completed,
    profile_picture_url,
    preferred_language,
    email_verified,
    phone_verified,
    status,
    profile_completed,
    created_at,
    updated_at
FROM users
WHERE (
        first_name ILIKE '%' || $1 || '%'
     OR last_name  ILIKE '%' || $1 || '%'
     OR phone_number ILIKE '%' || $1 || '%'
     OR email ILIKE '%' || $1 || '%'
    )
  AND (
        role = sqlc.narg('role')
        OR sqlc.narg('role') IS NULL
    );


-- name: UpdateUser :exec
UPDATE users
SET
    first_name      = COALESCE($1, first_name),
    last_name       = COALESCE($2, last_name),
    knowledge_level = COALESCE($3, knowledge_level),
    age_group       = COALESCE($4, age_group),
    education_level = COALESCE($5, education_level),
    country         = COALESCE($6, country),
    region          = COALESCE($7, region),
    nick_name       = COALESCE($8, nick_name),
    occupation      = COALESCE($9, occupation),
    learning_goal   = COALESCE($10, learning_goal),
    language_goal   = COALESCE($11, language_goal),
    language_challange = COALESCE($12, language_challange),
    favourite_topic = COALESCE($13, favourite_topic),
    initial_assessment_completed = COALESCE($14, initial_assessment_completed),
    profile_completed = COALESCE($15, profile_completed),
    profile_picture_url = COALESCE($16, profile_picture_url),
    preferred_language = COALESCE($17, preferred_language),
    gender       = COALESCE($18, gender),
    birth_day    = COALESCE($19, birth_day),
    updated_at   = CURRENT_TIMESTAMP
WHERE id = $20;

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


-- name: CheckPhoneEmailExist :one
SELECT
    EXISTS (
        SELECT 1 FROM users u1 WHERE u1.phone_number = $1
    ) AS phone_exists,
    EXISTS (
        SELECT 1 FROM users u2 WHERE u2.email = $2
    ) AS email_exists;


-- -- name: GetUserByUserName :one
-- SELECT
--     id,
--     first_name,
--     last_name,
--     email,
--     phone_number,
--     role,
--     password,
--     age,
--     education_level,
--     country,
--     region,

--     nick_name,
--     occupation,
--     learning_goal,
--     language_goal,
--     language_challange,
--     favourite_topic,

--     email_verified,
--     phone_verified,
--     status,
--     profile_completed,
--     last_login,
--     profile_picture_url,
--     preferred_language,
--     created_at,
--     updated_at
-- FROM users
-- WHERE user_name = $1 AND $1 IS NOT NULL
-- LIMIT 1;

-- name: GetUserByEmailPhone :one
SELECT
    id,
    first_name,
    last_name,
    gender,
    birth_day,
    email,
    phone_number,
    role,
    password,
    age_group,
    education_level,
    country,
    region,

    nick_name,
    occupation,
    learning_goal,
    language_goal,
    language_challange,
    favourite_topic,

    email_verified,
    phone_verified,
    status,
    profile_completed,
    last_login,
    profile_picture_url,
    preferred_language,
    created_at,
    updated_at
FROM users
WHERE (email = $1 AND $1 IS NOT NULL)
   OR (phone_number = $2 AND $2 IS NOT NULL)
LIMIT 1;

-- name: UpdatePassword :exec
UPDATE users
SET
    password = $1,
    updated_at = CURRENT_TIMESTAMP
WHERE id = $2;

-- name: UpdateUserStatus :exec
UPDATE users
SET
    status = $1,
    updated_at = CURRENT_TIMESTAMP
WHERE id = $2;

-- name: UpdateUserKnowledgeLevel :exec
UPDATE users
SET
    knowledge_level = $1,
    updated_at = CURRENT_TIMESTAMP
WHERE id = $2;

