CREATE VIEW test_cycle AS
SELECT
c.test_case_id,
j.value AS test_cycle,
c.suite_id,
ts.name AS suite_name,
COUNT(*) AS test_case_count,
COUNT(CASE WHEN r.status = 'passed' THEN 1 END) AS passed_count,
COUNT(CASE WHEN r.status = 'failed' THEN 1 END) AS failed_count
FROM
test_cases AS c
-- ✅ Properly expand JSON array into rows
, json_each(c.test_cycles) AS j
JOIN test_suites AS ts ON ts.id = c.suite_id
LEFT JOIN test_case_run_results AS r
ON r.test_case_id = c.test_case_id
WHERE
c.test_cycles IS NOT NULL
GROUP BY
j.value,
c.suite_id,
ts.name
ORDER BY
c.suite_id,
test_cycle