-- ============================================================================
-- Chat System: conversations, participants, and messages
-- ============================================================================

CREATE TABLE IF NOT EXISTS conversations (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
    updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

CREATE TABLE IF NOT EXISTS conversation_participants (
    conversation_id UUID NOT NULL REFERENCES conversations(id) ON DELETE CASCADE,
    user_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,
    joined_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
    PRIMARY KEY (conversation_id, user_id)
);

CREATE TABLE IF NOT EXISTS messages (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    conversation_id UUID NOT NULL REFERENCES conversations(id) ON DELETE CASCADE,
    sender_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,
    content TEXT NOT NULL,
    created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
    updated_at TIMESTAMPTZ,
    deleted_at TIMESTAMPTZ
);

CREATE INDEX IF NOT EXISTS idx_messages_conversation_id ON messages(conversation_id);
CREATE INDEX IF NOT EXISTS idx_messages_created_at ON messages(created_at);
CREATE INDEX IF NOT EXISTS idx_conversation_participants_user_id ON conversation_participants(user_id);

CREATE INDEX IF NOT EXISTS idx_messages_conversation_created ON messages(conversation_id, created_at DESC);

-- Seed conversations between superadmin and other users
INSERT INTO conversations (id, created_at) VALUES
    ('c0000000-0000-0000-0000-000000000001', NOW() - INTERVAL '2 hours'),
    ('c0000000-0000-0000-0000-000000000002', NOW() - INTERVAL '1 hour'),
    ('c0000000-0000-0000-0000-000000000003', NOW() - INTERVAL '30 minutes')
ON CONFLICT DO NOTHING;

-- Add participants (superadmin + each sales/dev user)
INSERT INTO conversation_participants (conversation_id, user_id) VALUES
    ('c0000000-0000-0000-0000-000000000001', '00000000-0000-0000-0000-000000000001'),
    ('c0000000-0000-0000-0000-000000000001', '00000000-0000-0000-0000-000000000003'),
    ('c0000000-0000-0000-0000-000000000002', '00000000-0000-0000-0000-000000000001'),
    ('c0000000-0000-0000-0000-000000000002', '00000000-0000-0000-0000-000000000004'),
    ('c0000000-0000-0000-0000-000000000003', '00000000-0000-0000-0000-000000000001'),
    ('c0000000-0000-0000-0000-000000000003', '00000000-0000-0000-0000-000000000002')
ON CONFLICT DO NOTHING;

-- Seed some messages
INSERT INTO messages (id, conversation_id, sender_id, content, created_at) VALUES
    ('00000000-0000-0000-0000-000000000101', 'c0000000-0000-0000-0000-000000000001',
     '00000000-0000-0000-0000-000000000003', 'Hey! Just finalized the TechCorp deal.',
     NOW() - INTERVAL '2 hours'),
    ('00000000-0000-0000-0000-000000000102', 'c0000000-0000-0000-0000-000000000001',
     '00000000-0000-0000-0000-000000000001', 'Great work! What were the final terms?',
     NOW() - INTERVAL '105 minutes'),
    ('00000000-0000-0000-0000-000000000103', 'c0000000-0000-0000-0000-000000000001',
     '00000000-0000-0000-0000-000000000003', '3-year enterprise license, 15% discount. They signed this morning.',
     NOW() - INTERVAL '100 minutes'),
    ('00000000-0000-0000-0000-000000000104', 'c0000000-0000-0000-0000-000000000002',
     '00000000-0000-0000-0000-000000000004', 'The API integration tests are passing on staging.',
     NOW() - INTERVAL '50 minutes'),
    ('00000000-0000-0000-0000-000000000105', 'c0000000-0000-0000-0000-000000000002',
     '00000000-0000-0000-0000-000000000001', 'Great, when can we deploy to production?',
     NOW() - INTERVAL '45 minutes'),
    ('00000000-0000-0000-0000-000000000106', 'c0000000-0000-0000-0000-000000000002',
     '00000000-0000-0000-0000-000000000004', 'Tomorrow morning after final review.',
     NOW() - INTERVAL '40 minutes'),
    ('00000000-0000-0000-0000-000000000107', 'c0000000-0000-0000-0000-000000000003',
     '00000000-0000-0000-0000-000000000002', 'New report shows 23% increase in lead conversion this quarter.',
     NOW() - INTERVAL '25 minutes'),
    ('00000000-0000-0000-0000-000000000108', 'c0000000-0000-0000-0000-000000000003',
     '00000000-0000-0000-0000-000000000001', 'That is excellent! Let us discuss in the next team meeting.',
     NOW() - INTERVAL '20 minutes')
ON CONFLICT DO NOTHING;

-- Update conversation timestamps
UPDATE conversations SET updated_at = NOW() - INTERVAL '20 minutes' WHERE id = 'c0000000-0000-0000-0000-000000000003';
UPDATE conversations SET updated_at = NOW() - INTERVAL '40 minutes' WHERE id = 'c0000000-0000-0000-0000-000000000002';
UPDATE conversations SET updated_at = NOW() - INTERVAL '100 minutes' WHERE id = 'c0000000-0000-0000-0000-000000000001';
