-- 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
    )
    AND (
        first_name ILIKE '%' || sqlc.narg('query') || '%'
        OR last_name ILIKE '%' || sqlc.narg('query') || '%'
        OR phone_number ILIKE '%' || sqlc.narg('query') || '%'
        OR sqlc.narg('query') IS NULL
    )
    AND (
        created_at > sqlc.narg('created_before')
        OR sqlc.narg('created_before') IS NULL
    )
    AND (
        created_at < sqlc.narg('created_after')
        OR sqlc.narg('created_after') 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 || '%'
    )
    AND (
        role = sqlc.narg('role')
        OR sqlc.narg('role') IS NULL
    )
    AND (
        company_id = sqlc.narg('company_id')
        OR sqlc.narg('company_id') IS NULL
    );
-- name: UpdateUser :exec
UPDATE users
SET first_name = $1,
    last_name = $2,
    suspended = $3,
    updated_at = CURRENT_TIMESTAMP
WHERE id = $4;
-- 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
    );