-- QA agent: automated site-test runs and their findings.
--
-- A run is one crawl of the live site (links, images, forms, API, SEO/meta).
-- Each finding is one issue (or pass) with a severity and a fix hint that can be
-- handed to the Web Designer queue as a fix task. Same shape as cja_tasks.

CREATE TABLE IF NOT EXISTS `cja_qa_runs` (
  `run_id`       int(10) unsigned NOT NULL AUTO_INCREMENT,
  `status`       enum('running','done','failed') NOT NULL DEFAULT 'running',
  `trigger`      enum('manual','scheduled') NOT NULL DEFAULT 'manual',
  `summary`      text        DEFAULT NULL,   -- plain-English triage summary
  `counts`       longtext    DEFAULT NULL CHECK (`counts` IS NULL OR json_valid(`counts`)),
  `started_at`   datetime    NOT NULL DEFAULT current_timestamp(),
  `finished_at`  datetime    DEFAULT NULL,
  PRIMARY KEY (`run_id`),
  KEY `idx_cja_qa_runs_recent` (`started_at`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `cja_qa_findings` (
  `finding_id`   int(10) unsigned NOT NULL AUTO_INCREMENT,
  `run_id`       int(10) unsigned NOT NULL,
  `check_type`   varchar(40) NOT NULL DEFAULT '',   -- page, link, image, external, form, health, seo, sitemap, a11y
  `severity`     enum('error','warning','info') NOT NULL DEFAULT 'info',
  `url`          varchar(500) NOT NULL DEFAULT '',
  `detail`       varchar(500) NOT NULL DEFAULT '',
  `fix_hint`     text        DEFAULT NULL,     -- prompt for a Web Designer fix task
  `status`       enum('open','fix_queued','ignored') NOT NULL DEFAULT 'open',
  `created_at`   datetime    NOT NULL DEFAULT current_timestamp(),
  PRIMARY KEY (`finding_id`),
  KEY `idx_cja_qa_findings_run` (`run_id`, `severity`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
