-- 001_cpanel_imports_awaiting_selection.sql
--
-- Adds the "awaiting_selection" status to whp.cpanel_imports.status, and a
-- selected_site_groups JSON column, to support the two-phase
-- analyze-then-confirm import flow.
--
-- Flow:
--   1. Operator clicks "Analyze". Panel writes the import row with status=
--      pending then flips to running for the parse-only phase.
--   2. Importer runs ONLY extract + parseBackupStructure, writes the discovered
--      site groups to /var/lib/whp/cpanel-imports/<import_id>.manifest.json,
--      and the API endpoint flips status=awaiting_selection.
--   3. UI redirects the operator to the selector page.
--   4. Operator picks site groups + submits, API writes the chosen keys to
--      cpanel_imports.selected_site_groups and flips status=running, kicking
--      off the second worker pass which resumes from the file-copy stage and
--      filters $this->siteGroups to only the selected docroot keys.
--
-- Idempotent per feedback_idempotent_migrations:
--   - The ENUM rewrite is wrapped in a procedure that no-ops if the value is
--     already present (MariaDB raises 1067 on duplicate-default, but the safer
--     "is it already in the enum string?" check sidesteps that entirely).
--   - The JSON column add uses ADD COLUMN IF NOT EXISTS (MariaDB 10.0.2+).
--
-- skip_selection=1 (used by WHMCS / headless callers) bypasses the
-- awaiting_selection state entirely; those rows transition directly from
-- pending → running just like before.

USE whp;

-- 1) selected_site_groups: JSON list of the docroot keys the operator opted
-- into. NULL = "import everything" (backwards-compatible; matches the
-- skip_selection=1 fast path).
ALTER TABLE whp.cpanel_imports
    ADD COLUMN IF NOT EXISTS selected_site_groups JSON NULL
    COMMENT 'JSON array of docroot keys the operator selected on the analyze-and-confirm step; NULL means import all (skip_selection=1 / pre-2-phase rows)';

-- 2) Status ENUM gains 'awaiting_selection'. Procedural to make it idempotent
-- — calling ALTER TABLE ... MODIFY ... ENUM(...) twice with the same list is
-- harmless in MariaDB, but checking the schema first avoids touching the
-- column at all when the value already exists. Procedural DDL needs USE whp;
-- (per feedback_sql_migrations_use_whp).

DROP PROCEDURE IF EXISTS whp_cpanel_imports_add_awaiting_selection;

DELIMITER //

CREATE PROCEDURE whp_cpanel_imports_add_awaiting_selection()
BEGIN
    DECLARE has_value INT DEFAULT 0;

    SELECT COUNT(*) INTO has_value
      FROM information_schema.COLUMNS
     WHERE TABLE_SCHEMA = 'whp'
       AND TABLE_NAME   = 'cpanel_imports'
       AND COLUMN_NAME  = 'status'
       AND COLUMN_TYPE LIKE '%awaiting_selection%';

    IF has_value = 0 THEN
        ALTER TABLE whp.cpanel_imports
            MODIFY COLUMN status
            ENUM('pending','awaiting_selection','running','completed','failed')
            NOT NULL DEFAULT 'pending';
    END IF;
END //

DELIMITER ;

CALL whp_cpanel_imports_add_awaiting_selection();

DROP PROCEDURE IF EXISTS whp_cpanel_imports_add_awaiting_selection;
