-- name: CreateUser :one
INSERT INTO users (
        first_name,
        last_name,
        email,
        phone_number,
        role,
        password,
        email_verified,
        phone_verified,
        created_at,
        updated_at,
        suspended,
        company_id
    )
VALUES (
        $1,
        $2,
        $3,
        $4,
        $5,
        $6,
        $7,
        $8,
        $9,
        $10,
        $11,
        $12
    )
RETURNING id,
    first_name,
    last_name,
    email,
    phone_number,
    role,
    email_verified,
    phone_verified,
    created_at,
    updated_at,
    suspended,
    company_id;
-- name: GetUserByID :one
SELECT *
FROM users
WHERE id = $1;
-- name: GetAllUsers :many
SELECT id,
    first_name,
    last_name,
    email,
    phone_number,
    role,
    email_verified,
    phone_verified,
    created_at,
    updated_at,
    suspended,
    suspended_at,
    company_id
FROM users
wHERE (
        role = $1
        OR $1 IS NULL
    )
    AND (
        company_id = $2
        OR $2 IS NULL
    )
LIMIT sqlc.narg('limit') OFFSET sqlc.narg('offset');
-- name: GetTotalUsers :one
SELECT COUNT(*)
FROM users
wHERE (
        role = $1
        OR $1 IS NULL
    )
    AND (
        company_id = $2
        OR $2 IS NULL
    );
-- name: SearchUserByNameOrPhone :many
SELECT id,
    first_name,
    last_name,
    email,
    phone_number,
    role,
    email_verified,
    phone_verified,
    created_at,
    updated_at,
    suspended,
    suspended_at,
    company_id
FROM users
WHERE first_name ILIKE '%' || $1 || '%'
    OR last_name ILIKE '%' || $1 || '%'
    OR phone_number LIKE '%' || $1 || '%';
-- name: UpdateUser :exec
UPDATE users
SET first_name = $1,
    last_name = $2,
    email = $3,
    phone_number = $4,
    role = $5,
    updated_at = $6
WHERE id = $7;
-- name: UpdateUserCompany :exec
UPDATE users
SET company_id = $1
WHERE id = $2;
-- name: SuspendUser :exec
UPDATE users
SET suspended = $1,
    suspended_at = $2,
    updated_at = CURRENT_TIMESTAMP
WHERE id = $3;
-- name: DeleteUser :exec
DELETE FROM users
WHERE id = $1;
-- name: CheckPhoneEmailExist :one
SELECT EXISTS (
        SELECT 1
        FROM users
        WHERE users.phone_number = $1
            AND users.phone_number IS NOT NULL
    ) AS phone_exists,
    EXISTS (
        SELECT 1
        FROM users
        WHERE users.email = $2
            AND users.email IS NOT NULL
    ) AS email_exists;
-- name: GetUserByEmail :one
SELECT id,
    first_name,
    last_name,
    email,
    phone_number,
    role,
    email_verified,
    phone_verified,
    created_at,
    updated_at,
    suspended,
    suspended_at,
    company_id
FROM users
WHERE email = $1;
-- name: GetUserByPhone :one
SELECT id,
    first_name,
    last_name,
    email,
    phone_number,
    role,
    email_verified,
    phone_verified,
    created_at,
    updated_at,
    suspended,
    suspended_at,
    company_id
FROM users
WHERE phone_number = $1;
-- name: UpdatePassword :exec
UPDATE users
SET password = $1,
    updated_at = $4
WHERE (
        email = $2
        OR phone_number = $3
    );