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

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


-- name: CreateUser :one
INSERT INTO users (
    first_name,
    last_name,
    user_name,
    email,
    phone_number,
    role,
    password,
    age,
    education_level,
    country,
    region,

    nick_name,
    occupation,
    learning_goal,
    language_goal,
    language_challange,
    favoutite_topic,

    initial_assessment_completed,
    email_verified,
    phone_verified,
    status,
    profile_completed,
    profile_picture_url,
    preferred_language,
    updated_at
)
VALUES (
    $1,   -- first_name
    $2,   -- last_name
    $3,   -- user_name
    $4,   -- email
    $5,   -- phone_number
    $6,   -- role
    $7,   -- password
    $8,   -- age
    $9,   -- education_level
    $10,  -- country
    $11,  -- region

    $12,  -- nick_name
    $13,  -- occupation
    $14,  -- learning_goal
    $15,  -- language_goal
    $16,  -- language_challange
    $17,  -- favoutite_topic

    $18,  -- initial_assessment_completed
    $19,  -- email_verified
    $20,  -- phone_verified
    $21,  -- status
    $22,  -- profile_completed
    $23,  -- profile_picture_url
    $24,  -- preferred_language
    CURRENT_TIMESTAMP
)
RETURNING
    id,
    first_name,
    last_name,
    user_name,
    email,
    phone_number,
    role,
    age,
    education_level,
    country,
    region,

    nick_name,
    occupation,
    learning_goal,
    language_goal,
    language_challange,
    favoutite_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: GetAllUsers :many
SELECT
    COUNT(*) OVER () AS total_count,
    id,
    first_name,
    last_name,
    user_name,
    email,
    phone_number,
    role,
    age,
    education_level,
    country,
    region,
    nick_name,
    occupation,
    learning_goal,
    language_goal,
    language_challange,
    favoutite_topic,
    initial_assessment_completed,
    profile_picture_url,
    preferred_language,
    email_verified,
    phone_verified,
    status,
    profile_completed,
    created_at,
    updated_at
FROM users
WHERE ($1 IS NULL OR role = $1)
  AND ($2 IS NULL OR first_name ILIKE '%' || $2 || '%' 
             OR last_name ILIKE '%' || $2 || '%' 
             OR phone_number ILIKE '%' || $2 || '%' 
             OR email ILIKE '%' || $2 || '%')
  AND ($3 IS NULL OR created_at >= $3)
  AND ($4 IS NULL OR created_at <= $4)
LIMIT $5
OFFSET $6;

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

-- name: SearchUserByNameOrPhone :many
SELECT
    id,
    first_name,
    last_name,
    user_name,
    email,
    phone_number,
    role,
    age,
    education_level,
    country,
    region,

    nick_name,
    occupation,
    learning_goal,
    language_goal,
    language_challange,
    favoutite_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),
    user_name       = COALESCE($3, user_name),
    knowledge_level = COALESCE($4, knowledge_level),
    age             = COALESCE($5, age),
    education_level = COALESCE($6, education_level),
    country         = COALESCE($7, country),
    region          = COALESCE($8, region),
    nick_name       = COALESCE($9, nick_name),
    occupation      = COALESCE($10, occupation),
    learning_goal   = COALESCE($11, learning_goal),
    language_goal   = COALESCE($12, language_goal),
    language_challange = COALESCE($13, language_challange),
    favoutite_topic = COALESCE($14, favoutite_topic),
    initial_assessment_completed = COALESCE($15, initial_assessment_completed),
    email_verified  = COALESCE($16, email_verified),
    phone_verified  = COALESCE($17, phone_verified),
    status          = COALESCE($18, status),
    profile_completed = COALESCE($19, profile_completed),
    profile_picture_url = COALESCE($20, profile_picture_url),
    preferred_language = COALESCE($21, preferred_language),
    updated_at      = CURRENT_TIMESTAMP
WHERE id = $22;

-- 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,
    user_name,
    email,
    phone_number,
    role,
    password,
    age,
    education_level,
    country,
    region,

    nick_name,
    occupation,
    learning_goal,
    language_goal,
    language_challange,
    favoutite_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,
    user_name,
    email,
    phone_number,
    role,
    password,
    age,
    education_level,
    country,
    region,

    nick_name,
    occupation,
    learning_goal,
    language_goal,
    language_challange,
    favoutite_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 user_name = $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;
