-- ============================================================================
-- Migration V6: Tables Communication (Conversations + Messages)
-- Date: 2026-03-16
-- Description: Création du système de messagerie UnionFlow
-- ============================================================================

-- Table conversations
CREATE TABLE IF NOT EXISTS conversations (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    name VARCHAR(255) NOT NULL,
    description VARCHAR(1000),
    type VARCHAR(20) NOT NULL CHECK (type IN ('INDIVIDUAL', 'GROUP', 'BROADCAST', 'ANNOUNCEMENT')),
    organisation_id UUID,
    avatar_url VARCHAR(500),
    is_muted BOOLEAN NOT NULL DEFAULT FALSE,
    is_pinned BOOLEAN NOT NULL DEFAULT FALSE,
    is_archived BOOLEAN NOT NULL DEFAULT FALSE,
    metadata TEXT,
    updated_at TIMESTAMP,

    -- Colonnes BaseEntity
    date_creation TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    date_modification TIMESTAMP,
    cree_par VARCHAR(255),
    modifie_par VARCHAR(255),
    actif BOOLEAN DEFAULT TRUE,
    version INTEGER NOT NULL DEFAULT 0,

    CONSTRAINT fk_conversation_organisation FOREIGN KEY (organisation_id)
        REFERENCES organisations(id) ON DELETE SET NULL
);

-- Index pour conversations
CREATE INDEX IF NOT EXISTS idx_conversation_organisation ON conversations(organisation_id);
CREATE INDEX IF NOT EXISTS idx_conversation_type ON conversations(type);
CREATE INDEX IF NOT EXISTS idx_conversation_archived ON conversations(is_archived);
CREATE INDEX IF NOT EXISTS idx_conversation_created ON conversations(date_creation);

-- Table de jointure conversation_participants (many-to-many)
CREATE TABLE IF NOT EXISTS conversation_participants (
    conversation_id UUID NOT NULL,
    membre_id UUID NOT NULL,
    joined_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,

    PRIMARY KEY (conversation_id, membre_id),

    CONSTRAINT fk_conv_participant_conversation FOREIGN KEY (conversation_id)
        REFERENCES conversations(id) ON DELETE CASCADE,
    CONSTRAINT fk_conv_participant_membre FOREIGN KEY (membre_id)
        REFERENCES utilisateurs(id) ON DELETE CASCADE
);

-- Index pour conversation_participants
CREATE INDEX IF NOT EXISTS idx_conv_participants_membre ON conversation_participants(membre_id);
CREATE INDEX IF NOT EXISTS idx_conv_participants_conversation ON conversation_participants(conversation_id);

-- Table messages
CREATE TABLE IF NOT EXISTS messages (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    conversation_id UUID NOT NULL,
    sender_id UUID NOT NULL,
    sender_name VARCHAR(255) NOT NULL,
    sender_avatar VARCHAR(500),
    content TEXT NOT NULL,
    type VARCHAR(20) NOT NULL CHECK (type IN ('INDIVIDUAL', 'BROADCAST', 'TARGETED', 'SYSTEM')),
    status VARCHAR(20) NOT NULL CHECK (status IN ('SENT', 'DELIVERED', 'READ', 'FAILED')),
    priority VARCHAR(20) NOT NULL DEFAULT 'NORMAL' CHECK (priority IN ('NORMAL', 'HIGH', 'URGENT')),
    recipient_ids VARCHAR(2000),
    recipient_roles VARCHAR(500),
    organisation_id UUID,
    read_at TIMESTAMP,
    metadata TEXT,
    attachments VARCHAR(2000),
    is_edited BOOLEAN NOT NULL DEFAULT FALSE,
    edited_at TIMESTAMP,
    is_deleted BOOLEAN NOT NULL DEFAULT FALSE,

    -- Colonnes BaseEntity
    date_creation TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    date_modification TIMESTAMP,
    cree_par VARCHAR(255),
    modifie_par VARCHAR(255),
    actif BOOLEAN DEFAULT TRUE,
    version INTEGER NOT NULL DEFAULT 0,

    CONSTRAINT fk_message_conversation FOREIGN KEY (conversation_id)
        REFERENCES conversations(id) ON DELETE CASCADE,
    CONSTRAINT fk_message_sender FOREIGN KEY (sender_id)
        REFERENCES utilisateurs(id) ON DELETE SET NULL,
    CONSTRAINT fk_message_organisation FOREIGN KEY (organisation_id)
        REFERENCES organisations(id) ON DELETE SET NULL
);

-- Index pour messages
CREATE INDEX IF NOT EXISTS idx_message_conversation ON messages(conversation_id);
CREATE INDEX IF NOT EXISTS idx_message_sender ON messages(sender_id);
CREATE INDEX IF NOT EXISTS idx_message_organisation ON messages(organisation_id);
CREATE INDEX IF NOT EXISTS idx_message_status ON messages(status);
CREATE INDEX IF NOT EXISTS idx_message_created ON messages(date_creation);
CREATE INDEX IF NOT EXISTS idx_message_deleted ON messages(is_deleted);

-- Commentaires
COMMENT ON TABLE conversations IS 'Conversations (fils de discussion) du système de messagerie';
COMMENT ON TABLE conversation_participants IS 'Association many-to-many entre conversations et membres participants';
COMMENT ON TABLE messages IS 'Messages individuels dans les conversations';

COMMENT ON COLUMN conversations.type IS 'Type: INDIVIDUAL (1-1), GROUP (groupe), BROADCAST (diffusion), ANNOUNCEMENT (annonces)';
COMMENT ON COLUMN messages.status IS 'Statut: SENT (envoyé), DELIVERED (livré), READ (lu), FAILED (échec)';
COMMENT ON COLUMN messages.priority IS 'Priorité: NORMAL, HIGH (élevée), URGENT (urgente)';
