-- Dossiers Ultimail (virtuels) : organisation UI, pas miroir IMAP.
-- account_id NULL = dossier global utilisateur ; non NULL = dossier lié à un compte mail.
CREATE TABLE mail_unified_folders (
    id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
    user_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,
    account_id UUID NULL REFERENCES mail_accounts(id) ON DELETE CASCADE,
    parent_id UUID NULL REFERENCES mail_unified_folders(id) ON DELETE CASCADE,
    name TEXT NOT NULL,
    color TEXT NOT NULL DEFAULT '',
    sort_order INT NOT NULL DEFAULT 0,
    created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
    updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

CREATE INDEX idx_mail_unified_folders_user ON mail_unified_folders(user_id);
CREATE INDEX idx_mail_unified_folders_user_account ON mail_unified_folders(user_id, account_id);
CREATE INDEX idx_mail_unified_folders_parent ON mail_unified_folders(parent_id);
