-- Migration pour les DM enrichis - Veza Chat Server
-- Ajoute la table dm_conversations pour séparer les DM des salons

BEGIN;

-- Table pour les conversations DM enrichies
CREATE TABLE IF NOT EXISTS dm_conversations (
    id BIGSERIAL PRIMARY KEY,
    uuid UUID UNIQUE NOT NULL DEFAULT gen_random_uuid(),
    user1_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,
    user2_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,
    is_blocked BOOLEAN NOT NULL DEFAULT FALSE,
    blocked_by UUID REFERENCES users(id) ON DELETE SET NULL,
    created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(),
    updated_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(),
    
    -- Contraintes
    CONSTRAINT dm_conversations_different_users CHECK (user1_id != user2_id),
    CONSTRAINT dm_conversations_ordered_users CHECK (user1_id < user2_id),
    CONSTRAINT dm_conversations_unique_pair UNIQUE (user1_id, user2_id)
);

-- Index pour les performances
CREATE INDEX IF NOT EXISTS idx_dm_conversations_user1 ON dm_conversations(user1_id);
CREATE INDEX IF NOT EXISTS idx_dm_conversations_user2 ON dm_conversations(user2_id);
CREATE INDEX IF NOT EXISTS idx_dm_conversations_updated_at ON dm_conversations(updated_at DESC);
CREATE INDEX IF NOT EXISTS idx_dm_conversations_uuid ON dm_conversations(uuid);

-- Trigger pour mettre à jour updated_at automatiquement
CREATE OR REPLACE FUNCTION update_dm_conversations_updated_at()
RETURNS TRIGGER AS $$
BEGIN
    NEW.updated_at = NOW();
    RETURN NEW;
END;
$$ LANGUAGE plpgsql;

CREATE TRIGGER trigger_dm_conversations_updated_at
    BEFORE UPDATE ON dm_conversations
    FOR EACH ROW
    EXECUTE FUNCTION update_dm_conversations_updated_at();

-- Migration des données DM supprimée car les colonnes from_user, to_user, room n'existent pas
-- dans le schéma de base. La table dm_conversations est créée vide et sera peuplée
-- par l'application lors de la création de nouveaux DM.

-- Log de la migration supprimé car la table audit_logs n'existe pas dans le schéma de base

COMMIT;

-- Vérifications post-migration
DO $$
DECLARE
    dm_conversations_count INTEGER;
    migrated_messages_count INTEGER;
BEGIN
    SELECT COUNT(*) INTO dm_conversations_count FROM dm_conversations;
    SELECT COUNT(*) INTO migrated_messages_count FROM messages WHERE conversation_id IN (SELECT uuid FROM dm_conversations);
    
    RAISE NOTICE 'Migration DM enrichis terminée:';
    RAISE NOTICE '  - % conversations DM créées', dm_conversations_count;
    RAISE NOTICE '  - % messages DM migrés', migrated_messages_count;
    
    IF dm_conversations_count = 0 THEN
        RAISE NOTICE '  ⚠️  Aucune conversation DM trouvée (normal si pas de DM existants)';
    ELSE
        RAISE NOTICE '  ✅ Migration réussie';
    END IF;
END $$; 