-- Persist Microsoft tenant admin consent for migration OAuth app registration.
CREATE TABLE IF NOT EXISTS migration_microsoft_admin_consents (
    tenant_id TEXT NOT NULL,
    client_id TEXT NOT NULL,
    project_id UUID REFERENCES migration_projects(id) ON DELETE SET NULL,
    granted BOOLEAN NOT NULL DEFAULT false,
    error_code TEXT NOT NULL DEFAULT '',
    error_description TEXT NOT NULL DEFAULT '',
    consented_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
    updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
    PRIMARY KEY (tenant_id, client_id)
);

CREATE INDEX IF NOT EXISTS idx_migration_ms_admin_consents_project
    ON migration_microsoft_admin_consents(project_id);

ALTER TABLE migration_projects
    ADD COLUMN IF NOT EXISTS microsoft_tenant_id TEXT NOT NULL DEFAULT '',
    ADD COLUMN IF NOT EXISTS microsoft_admin_consent_at TIMESTAMPTZ,
    ADD COLUMN IF NOT EXISTS microsoft_admin_consent_error TEXT NOT NULL DEFAULT '';
