-- name: GetUtmsByID :many
-- SELECT ID,AmoFieldID,QuizID,AccountID,Name
-- FROM utms
-- WHERE
--     ID = ANY($1::int[]) AND Deleted = FALSE;
--

-- name: UpdateUtms :exec
-- UPDATE utms AS u
-- SET name = (update_data ->> 'Name')::varchar(512),
--     AmoFieldID = (update_data ->> 'AmoFieldID')::INT
-- FROM json_array_elements($1::json) AS update_data
-- WHERE u.ID = (update_data ->> 'ID')::INT;

-- name: UpdateUtmsFields :exec
-- UPDATE utms AS u SET AmoFieldID = f.AmoID FROM fields AS f
-- WHERE u.Name = f.Name AND u.ID = ANY($1::int[]) AND u.Deleted = FALSE;

-- name: DeletingUTM :exec
-- UPDATE utms SET Deleted = true WHERE ID = ANY($1::int[]);

-- name: GetUTMsWithPagination :many
-- SELECT ut.*, COUNT(*) OVER() as total_count
-- FROM utms ut JOIN (SELECT AmoID FROM users WHERE users.AccountID = $1) u ON ut.AccountID = u.AmoID
-- WHERE ut.Deleted = false and ut.QuizID = $4
-- ORDER BY ut.ID OFFSET ($2 - 1) * $3 LIMIT $3;

-- name: SaveUTMs :many
-- WITH user_data AS (
--     SELECT AmoID
--     FROM users
--     WHERE users.AccountID = $1
-- ), new_UTMs AS (
--     SELECT (utm->>'AmoFieldID')::INT AS amoFieldID,
--            COALESCE(utm->>'QuizID', '')::INT AS quizID,
--            COALESCE(utm->>'Name', '')::varchar(512) AS name,
--            CURRENT_TIMESTAMP AS createdAt
--     FROM json_array_elements($2::json) AS utm
-- ), inserted_utms AS(
--     INSERT INTO utms (AmoFieldID, QuizID, AccountID, Name, createdAt)
--         SELECT nu.amoFieldID,
--                nu.quizID,
--                ud.AmoID,
--                nu.name,
--                nu.createdAt
--         FROM new_UTMs nu
--                  JOIN user_data ud ON true
--         RETURNING *
-- )
-- SELECT * from inserted_utms;







-- WITH QuizAnswers AS (
--     SELECT
--     session, start, result, question_id, created_at, content, quiz_id
-- FROM answer
-- WHERE answer.quiz_id = 27930 AND created_at between TO_TIMESTAMP(1757785974)::timestamp and TO_TIMESTAMP(1749155806)::timestamp
--     ), QuizQuestions AS (SELECT title, page, id from question where quiz_id = 27930), Funnel AS (
-- SELECT
--     COUNT(DISTINCT a.session) FILTER (WHERE a.start = FALSE) AS count_start_false,
--     COUNT(DISTINCT a.session) FILTER (WHERE a.start = TRUE) AS count_start_true,
--     COUNT(DISTINCT CASE WHEN a.result = FALSE AND qid_true_result IS NOT NULL THEN a.session END) AS count_f_result_with_t_question,
--     COUNT(DISTINCT a.session) FILTER (WHERE a.result = TRUE) AS count_t_result
-- FROM
--     QuizAnswers a
--     LEFT JOIN (
--     SELECT DISTINCT a.session, q.id AS qid_true_result
--     FROM QuizAnswers a
--     JOIN question q ON a.question_id = q.id
--     WHERE a.result = TRUE AND a.quiz_id = 27930
--     ) AS q ON a.session = q.session
--     ),
--     Results AS (
-- SELECT
--     COALESCE(q.title, '') AS question_title,
--     COUNT(*) AS total_answers,
--     CAST(COUNT(*) * 100.0 / NULLIF(SUM(COUNT(*)) FILTER (WHERE a.result = TRUE) OVER (PARTITION BY a.quiz_id), 0) AS FLOAT8) AS percentage
-- FROM
--     question q
--     JOIN answer a ON q.id = a.question_id
-- WHERE
--     a.quiz_id = 27930
--   AND a.created_at >= TO_TIMESTAMP(1757785974)
--   AND a.created_at <= TO_TIMESTAMP(1749155806)
--   AND a.result = TRUE
--   AND a.start = FALSE
-- GROUP BY
--     q.title, a.quiz_id, a.result
-- HAVING
--     COUNT(*) >= 1
--     ),
--     LastContent AS (
-- SELECT
--     a.question_id,
--     a.content AS last_answer_content,
--     a.result,
--     a.start,
--     a.session,
--     a.quiz_id
-- FROM
--     QuizAnswers a
--     LEFT JOIN (
--     SELECT
--     session,
--     question_id,
--     MAX(created_at) AS last_created_at
--     FROM
--     QuizAnswers
--     WHERE
--     quiz_id = 27930
--     AND start = FALSE
--     AND created_at >= TO_TIMESTAMP(1757785974)
--     AND created_at <= TO_TIMESTAMP(1749155806)
--     GROUP BY
--     question_id, session
--     ) AS last_created_at_one_session ON a.session = last_created_at_one_session.session AND a.question_id = last_created_at_one_session.question_id AND a.created_at = last_created_at_one_session.last_created_at
-- WHERE a.start = FALSE
--     ),
--     Questions AS (
-- SELECT
--     q.title AS question_title,
--     q.page AS question_page,
--     lc.last_answer_content AS answer_content,
--     CAST(
--     COUNT(CASE WHEN lc.result = FALSE THEN 1 END) * 100.0 / NULLIF(SUM(COUNT(CASE WHEN lc.result = FALSE THEN 1 END)) OVER (PARTITION BY q.id), 0) AS FLOAT8
--     ) AS percentage
-- FROM
--     question q
--     JOIN LastContent lc ON q.id = lc.question_id
-- WHERE
--     lc.quiz_id = 27930
-- GROUP BY
--     q.id, q.title, lc.last_answer_content
-- HAVING
--     COUNT(*) >= 1
--     )
-- SELECT
--     Funnel.count_start_false,
--     Funnel.count_start_true,
--     Funnel.count_f_result_with_t_question,
--     Funnel.count_t_result,
--     COALESCE(Results.question_title, '') AS results_title,
--     COALESCE(Results.percentage, 0) AS results_percentage,
--     COALESCE(Questions.question_title, '') AS questions_title,
--     COALESCE(Questions.question_page, 0) AS questions_page,
--     COALESCE(Questions.answer_content, '') AS answer_content,
--     COALESCE(Questions.percentage, 0) AS questions_percentage
-- FROM
--     Funnel
--         LEFT JOIN Results ON true
--         LEFT JOIN Questions ON Questions.percentage >= 1;