-- =====================================================
-- SAMPLE DATA FOR TABLOS SYSTEM
-- =====================================================

-- Sample tablos data
INSERT INTO tablos (id, name, description, color, owner_id, is_public) VALUES
('550e8400-e29b-41d4-a716-446655440001', 'Projet Alpha', 'Développement de la nouvelle application mobile', 'bg-blue-500', auth.uid(), false),
('550e8400-e29b-41d4-a716-446655440002', 'Marketing Q4', 'Campagnes marketing pour le quatrième trimestre 2024', 'bg-green-500', auth.uid(), true),
('550e8400-e29b-41d4-a716-446655440003', 'Équipe Dev', 'Coordination et suivi de l''équipe de développement', 'bg-purple-500', auth.uid(), false),
('550e8400-e29b-41d4-a716-446655440004', 'Budget 2024', 'Planification et suivi budgétaire pour l''année 2024', 'bg-red-500', auth.uid(), false),
('550e8400-e29b-41d4-a716-446655440005', 'Roadmap Produit', 'Feuille de route et évolution du produit', 'bg-yellow-500', auth.uid(), true),
('550e8400-e29b-41d4-a716-446655440006', 'Support Client', 'Gestion et suivi du support client', 'bg-indigo-500', auth.uid(), false);

-- Sample boards for each tablo
INSERT INTO tablo_boards (tablo_id, name, type, description, position, created_by) VALUES
-- Projet Alpha boards
('550e8400-e29b-41d4-a716-446655440001', 'Développement', 'kanban', 'Suivi des tâches de développement', 0, auth.uid()),
('550e8400-e29b-41d4-a716-446655440001', 'Planning', 'calendar', 'Calendrier du projet', 1, auth.uid()),
('550e8400-e29b-41d4-a716-446655440001', 'Discussion', 'chat', 'Chat de l''équipe projet', 2, auth.uid()),

-- Marketing Q4 boards
('550e8400-e29b-41d4-a716-446655440002', 'Campagnes', 'kanban', 'Suivi des campagnes marketing', 0, auth.uid()),
('550e8400-e29b-41d4-a716-446655440002', 'Calendrier Editorial', 'calendar', 'Planning des publications', 1, auth.uid()),

-- Équipe Dev boards
('550e8400-e29b-41d4-a716-446655440003', 'Sprint Board', 'kanban', 'Tableau de bord du sprint actuel', 0, auth.uid()),
('550e8400-e29b-41d4-a716-446655440003', 'Backlog', 'table', 'Backlog produit', 1, auth.uid());

-- Sample lists for Kanban boards
INSERT INTO tablo_lists (board_id, name, position, color) VALUES
-- For Projet Alpha - Développement board
((SELECT id FROM tablo_boards WHERE name = 'Développement' AND tablo_id = '550e8400-e29b-41d4-a716-446655440001'), 'À faire', 0, 'bg-gray-200'),
((SELECT id FROM tablo_boards WHERE name = 'Développement' AND tablo_id = '550e8400-e29b-41d4-a716-446655440001'), 'En cours', 1, 'bg-blue-200'),
((SELECT id FROM tablo_boards WHERE name = 'Développement' AND tablo_id = '550e8400-e29b-41d4-a716-446655440001'), 'En test', 2, 'bg-yellow-200'),
((SELECT id FROM tablo_boards WHERE name = 'Développement' AND tablo_id = '550e8400-e29b-41d4-a716-446655440001'), 'Terminé', 3, 'bg-green-200'),

-- For Marketing Q4 - Campagnes board
((SELECT id FROM tablo_boards WHERE name = 'Campagnes' AND tablo_id = '550e8400-e29b-41d4-a716-446655440002'), 'Idées', 0, 'bg-purple-200'),
((SELECT id FROM tablo_boards WHERE name = 'Campagnes' AND tablo_id = '550e8400-e29b-41d4-a716-446655440002'), 'En préparation', 1, 'bg-orange-200'),
((SELECT id FROM tablo_boards WHERE name = 'Campagnes' AND tablo_id = '550e8400-e29b-41d4-a716-446655440002'), 'En cours', 2, 'bg-blue-200'),
((SELECT id FROM tablo_boards WHERE name = 'Campagnes' AND tablo_id = '550e8400-e29b-41d4-a716-446655440002'), 'Terminées', 3, 'bg-green-200');

-- Sample cards
INSERT INTO tablo_cards (list_id, title, description, position, priority, due_date, created_by) VALUES
-- Cards for "À faire" list
((SELECT id FROM tablo_lists WHERE name = 'À faire' LIMIT 1), 'Créer l''interface utilisateur', 'Développer les écrans principaux de l''application mobile', 0, 'high', NOW() + INTERVAL '1 week', auth.uid()),
((SELECT id FROM tablo_lists WHERE name = 'À faire' LIMIT 1), 'Intégration API backend', 'Connecter l''application aux services backend', 1, 'medium', NOW() + INTERVAL '2 weeks', auth.uid()),
((SELECT id FROM tablo_lists WHERE name = 'À faire' LIMIT 1), 'Tests unitaires', 'Écrire les tests pour les composants critiques', 2, 'medium', NOW() + INTERVAL '3 weeks', auth.uid()),

-- Cards for "En cours" list
((SELECT id FROM tablo_lists WHERE name = 'En cours' LIMIT 1), 'Configuration base de données', 'Mise en place de la structure de données', 0, 'high', NOW() + INTERVAL '3 days', auth.uid()),
((SELECT id FROM tablo_lists WHERE name = 'En cours' LIMIT 1), 'Authentification utilisateur', 'Système de login/logout', 1, 'high', NOW() + INTERVAL '5 days', auth.uid());

-- Sample chat channels
INSERT INTO tablo_chat_channels (tablo_id, name, type, description, created_by) VALUES
('550e8400-e29b-41d4-a716-446655440001', 'général', 'public', 'Discussion générale du projet Alpha', auth.uid()),
('550e8400-e29b-41d4-a716-446655440001', 'dev-team', 'private', 'Canal privé pour l''équipe de développement', auth.uid()),
('550e8400-e29b-41d4-a716-446655440002', 'marketing-general', 'public', 'Discussion générale marketing', auth.uid()),
('550e8400-e29b-41d4-a716-446655440003', 'daily-standup', 'public', 'Daily standup de l''équipe dev', auth.uid());

-- Sample chat messages
INSERT INTO tablo_chat_messages (channel_id, user_id, content, message_type) VALUES
((SELECT id FROM tablo_chat_channels WHERE name = 'général' LIMIT 1), auth.uid(), 'Bonjour l''équipe ! Prêts pour le sprint ?', 'text'),
((SELECT id FROM tablo_chat_channels WHERE name = 'général' LIMIT 1), auth.uid(), 'Oui, j''ai terminé la configuration de l''environnement', 'text'),
((SELECT id FROM tablo_chat_channels WHERE name = 'dev-team' LIMIT 1), auth.uid(), 'Le build est cassé sur la branche develop', 'text'),
((SELECT id FROM tablo_chat_channels WHERE name = 'marketing-general' LIMIT 1), auth.uid(), 'Nouvelle campagne lancée ce matin !', 'text');

-- =====================================================
-- USEFUL QUERIES FOR TABLOS SYSTEM
-- =====================================================

-- 1. Get all tablos for a user (owned or member of)
/*
SELECT DISTINCT t.*, tm.role, tm.permissions
FROM tablos t
LEFT JOIN tablo_members tm ON t.id = tm.tablo_id AND tm.user_id = auth.uid()
WHERE t.owner_id = auth.uid() 
   OR tm.user_id = auth.uid() 
   OR t.is_public = true
ORDER BY t.updated_at DESC;
*/

-- 2. Get tablo with all its boards and lists
/*
SELECT 
    t.name as tablo_name,
    t.description as tablo_description,
    b.name as board_name,
    b.type as board_type,
    l.name as list_name,
    l.position as list_position
FROM tablos t
LEFT JOIN tablo_boards b ON t.id = b.tablo_id
LEFT JOIN tablo_lists l ON b.id = l.board_id
WHERE t.id = 'your-tablo-id'
ORDER BY b.position, l.position;
*/

-- 3. Get cards with assignees for a specific board
/*
SELECT 
    c.title,
    c.description,
    c.priority,
    c.due_date,
    l.name as list_name,
    c.assignees,
    c.labels
FROM tablo_cards c
JOIN tablo_lists l ON c.list_id = l.id
JOIN tablo_boards b ON l.board_id = b.id
WHERE b.id = 'your-board-id'
ORDER BY l.position, c.position;
*/

-- 4. Get recent activity for a tablo
/*
SELECT 
    ta.action,
    ta.entity_type,
    ta.details,
    ta.created_at,
    p.full_name as user_name
FROM tablo_activities ta
JOIN profiles p ON ta.user_id = p.id
WHERE ta.tablo_id = 'your-tablo-id'
ORDER BY ta.created_at DESC
LIMIT 20;
*/

-- 5. Get chat messages for a channel with user info
/*
SELECT 
    tcm.content,
    tcm.message_type,
    tcm.created_at,
    p.full_name as sender_name,
    p.avatar_url
FROM tablo_chat_messages tcm
JOIN profiles p ON tcm.user_id = p.id
WHERE tcm.channel_id = 'your-channel-id'
ORDER BY tcm.created_at ASC;
*/

-- 6. Get overdue cards across all user's tablos
/*
SELECT 
    c.title,
    c.due_date,
    c.priority,
    t.name as tablo_name,
    b.name as board_name,
    l.name as list_name
FROM tablo_cards c
JOIN tablo_lists l ON c.list_id = l.id
JOIN tablo_boards b ON l.board_id = b.id
JOIN tablos t ON b.tablo_id = t.id
LEFT JOIN tablo_members tm ON t.id = tm.tablo_id
WHERE (t.owner_id = auth.uid() OR tm.user_id = auth.uid())
  AND c.due_date < NOW()
  AND c.due_date IS NOT NULL
ORDER BY c.due_date ASC;
*/

-- 7. Get member statistics for a tablo
/*
SELECT 
    COUNT(*) as total_members,
    COUNT(CASE WHEN tm.role = 'owner' THEN 1 END) as owners,
    COUNT(CASE WHEN tm.role = 'admin' THEN 1 END) as admins,
    COUNT(CASE WHEN tm.role = 'member' THEN 1 END) as members,
    COUNT(CASE WHEN tm.role = 'viewer' THEN 1 END) as viewers
FROM tablo_members tm
WHERE tm.tablo_id = 'your-tablo-id';
*/

-- 8. Search cards by content
/*
SELECT 
    c.title,
    c.description,
    t.name as tablo_name,
    b.name as board_name,
    l.name as list_name,
    ts_rank(to_tsvector('french', c.title || ' ' || COALESCE(c.description, '')), 
            plainto_tsquery('french', 'search-term')) as rank
FROM tablo_cards c
JOIN tablo_lists l ON c.list_id = l.id
JOIN tablo_boards b ON l.board_id = b.id
JOIN tablos t ON b.tablo_id = t.id
LEFT JOIN tablo_members tm ON t.id = tm.tablo_id
WHERE (t.owner_id = auth.uid() OR tm.user_id = auth.uid())
  AND to_tsvector('french', c.title || ' ' || COALESCE(c.description, '')) 
      @@ plainto_tsquery('french', 'search-term')
ORDER BY rank DESC;
*/

-- =====================================================
-- VIEWS FOR COMMON QUERIES
-- =====================================================

-- View for user's tablos with member info
CREATE VIEW user_tablos AS
SELECT DISTINCT 
    t.*,
    COALESCE(tm.role, 'owner') as user_role,
    COALESCE(tm.permissions, '{"read": true, "write": true, "admin": true}'::jsonb) as user_permissions,
    (SELECT COUNT(*) FROM tablo_members WHERE tablo_id = t.id) as member_count
FROM tablos t
LEFT JOIN tablo_members tm ON t.id = tm.tablo_id AND tm.user_id = auth.uid()
WHERE t.owner_id = auth.uid() 
   OR tm.user_id = auth.uid() 
   OR t.is_public = true;

-- View for tablo structure (boards, lists, cards count)
CREATE VIEW tablo_structure AS
SELECT 
    t.id as tablo_id,
    t.name as tablo_name,
    COUNT(DISTINCT b.id) as boards_count,
    COUNT(DISTINCT l.id) as lists_count,
    COUNT(DISTINCT c.id) as cards_count
FROM tablos t
LEFT JOIN tablo_boards b ON t.id = b.tablo_id
LEFT JOIN tablo_lists l ON b.id = l.board_id
LEFT JOIN tablo_cards c ON l.id = c.list_id
GROUP BY t.id, t.name;

-- View for recent activities across all user tablos
CREATE VIEW user_recent_activities AS
SELECT 
    ta.*,
    t.name as tablo_name,
    p.full_name as user_name
FROM tablo_activities ta
JOIN tablos t ON ta.tablo_id = t.id
JOIN profiles p ON ta.user_id = p.id
LEFT JOIN tablo_members tm ON t.id = tm.tablo_id AND tm.user_id = auth.uid()
WHERE t.owner_id = auth.uid() OR tm.user_id = auth.uid()
ORDER BY ta.created_at DESC; 