-- Opt-in "Import existing mail" — NoSync → IMAP full-archive tier.
--
-- Adds:
--   1. email_accounts.archival_mode ENUM('nosync','imap')
--      Which Bichon archival tier the mailbox is on. Fresh enables create a
--      credential-less NoSync (go-forward push only) account; the user-triggered
--      "Import existing mail" action upgrades to an IMAP account (history pull +
--      push + throttled safety-net poll).
--   2. email_accounts.archival_import_at TIMESTAMP NULL
--      Set to NOW() when an import (NoSync→IMAP) is kicked off; the daily
--      reconcile (bichon-archival-sync.php) clears it once the initial download
--      finishes and throttles download_interval_min to 525600.
--   3. Extend email_archival_events.event_type ENUM with 'import_started' and
--      'import_completed' so the import lifecycle is auditable.
--
-- Idempotent: ADD COLUMN IF NOT EXISTS (MariaDB).
--
-- The event_type MODIFY is NOT a no-op: a MODIFY replaces the ENTIRE value set,
-- so it must list the FULL UNION of every value already in use (which includes
-- soft_disabled/purged/password_reset emitted by live code in emailmgmt.php)
-- PLUS the two new import lifecycle values. Omitting any in-use value would
-- corrupt existing rows and throw under strict MySQL. Charset/NULL/collation
-- attributes are kept identical to the canonical definition in
-- complete-database-setup.sql so the only change is the appended value set,
-- making re-runs harmless.
USE whp;

ALTER TABLE whp.email_accounts
    ADD COLUMN IF NOT EXISTS archival_mode ENUM('nosync','imap') NOT NULL DEFAULT 'nosync'
        COMMENT 'Bichon archival tier: nosync (push-only) or imap (history + push)'
        AFTER bichon_username,
    ADD COLUMN IF NOT EXISTS archival_import_at TIMESTAMP NULL DEFAULT NULL
        COMMENT 'Set when a NoSync->IMAP import is started; cleared by reconcile on completion'
        AFTER archival_mode;

ALTER TABLE whp.email_archival_events
    MODIFY COLUMN event_type
        ENUM('enabled','disabled','paused','resumed','failed','deleted','soft_disabled','purged','password_reset','import_started','import_completed')
        NOT NULL;
