USE whp;

-- =====================================================================
-- Bichon user provisioning + 14-day grace-period soft delete
-- =====================================================================
-- Adds:
--   1. email_accounts.bichon_user_id
--      Bichon RBAC user id (POST /api/v1/users response.id) so the
--      customer can log into the archive WebUI with their email
--      address as the username.
--   2. email_accounts.archival_purge_at
--      Soft-delete deadline. When archival is disabled by the customer
--      we keep the Bichon ACCOUNT (and its archived mail) for 14 days
--      in case the customer disabled accidentally; the daily
--      reconciliation cron purges the Bichon account once
--      archival_purge_at <= NOW().
--   3. email_archival_events.event_type ENUM additions:
--      - 'soft_disabled'   — customer disabled archival; user removed,
--                            account+data retained until purge_at
--      - 'purged'          — grace period elapsed, Bichon account
--                            (and archived data) deleted by cron
--      - 'password_reset'  — customer reset their Bichon login password
--
-- Idempotent: ADD COLUMN IF NOT EXISTS / MODIFY COLUMN are safe to re-run.
-- =====================================================================

ALTER TABLE email_accounts
    ADD COLUMN IF NOT EXISTS bichon_user_id     VARCHAR(64) DEFAULT NULL
        COMMENT 'Bichon RBAC user id (POST /api/v1/users response.id) so the customer can log into the archive WebUI',
    ADD COLUMN IF NOT EXISTS archival_purge_at  TIMESTAMP NULL DEFAULT NULL
        COMMENT 'After this time, the soft-disabled Bichon account should be deleted by the reconciliation cron';

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