-- ============================================
-- Script pour créer des tickets de test
-- ============================================
-- Ce script crée 10 tickets de test avec différents statuts
-- Exécutez ce script dans votre base de données backend

-- PRÉREQUIS :
-- 1. Vous devez avoir au moins 1 utilisateur dans la table users
-- 2. Vous devez avoir au moins 1 prix dans la table prizes

-- ============================================
-- Étape 1 : Vérifier que vous avez des utilisateurs et des prix
-- ============================================

SELECT 'Nombre d''utilisateurs :' as info, COUNT(*) as count FROM users;
SELECT 'Nombre de prix :' as info, COUNT(*) as count FROM prizes;

-- Si le résultat est 0, vous devez d'abord créer des utilisateurs et des prix !

-- ============================================
-- Étape 2 : Créer des tickets de test
-- ============================================

-- IMPORTANT : Adaptez ce script selon votre schéma de base de données
-- Les noms de colonnes peuvent varier (ex: user_id vs userId)

-- Ticket 1 : En attente
INSERT INTO tickets (code, user_id, prize_id, status, played_at, created_at, updated_at)
SELECT
  'TEST-PENDING-001',
  (SELECT id FROM users ORDER BY RANDOM() LIMIT 1),
  (SELECT id FROM prizes ORDER BY RANDOM() LIMIT 1),
  'PENDING',
  NOW() - INTERVAL '2 days',
  NOW(),
  NOW()
WHERE EXISTS (SELECT 1 FROM users) AND EXISTS (SELECT 1 FROM prizes);

-- Ticket 2 : Validé
INSERT INTO tickets (code, user_id, prize_id, status, played_at, validated_at, created_at, updated_at)
SELECT
  'TEST-VALIDATED-002',
  (SELECT id FROM users ORDER BY RANDOM() LIMIT 1),
  (SELECT id FROM prizes ORDER BY RANDOM() LIMIT 1),
  'VALIDATED',
  NOW() - INTERVAL '3 days',
  NOW() - INTERVAL '1 day',
  NOW(),
  NOW()
WHERE EXISTS (SELECT 1 FROM users) AND EXISTS (SELECT 1 FROM prizes);

-- Ticket 3 : Rejeté
INSERT INTO tickets (code, user_id, prize_id, status, played_at, rejection_reason, created_at, updated_at)
SELECT
  'TEST-REJECTED-003',
  (SELECT id FROM users ORDER BY RANDOM() LIMIT 1),
  (SELECT id FROM prizes ORDER BY RANDOM() LIMIT 1),
  'REJECTED',
  NOW() - INTERVAL '4 days',
  'Ticket illisible',
  NOW(),
  NOW()
WHERE EXISTS (SELECT 1 FROM users) AND EXISTS (SELECT 1 FROM prizes);

-- Ticket 4 : Récupéré
INSERT INTO tickets (code, user_id, prize_id, status, played_at, validated_at, created_at, updated_at)
SELECT
  'TEST-CLAIMED-004',
  (SELECT id FROM users ORDER BY RANDOM() LIMIT 1),
  (SELECT id FROM prizes ORDER BY RANDOM() LIMIT 1),
  'CLAIMED',
  NOW() - INTERVAL '5 days',
  NOW() - INTERVAL '2 days',
  NOW(),
  NOW()
WHERE EXISTS (SELECT 1 FROM users) AND EXISTS (SELECT 1 FROM prizes);

-- Ticket 5 : En attente
INSERT INTO tickets (code, user_id, prize_id, status, played_at, created_at, updated_at)
SELECT
  'TEST-PENDING-005',
  (SELECT id FROM users ORDER BY RANDOM() LIMIT 1),
  (SELECT id FROM prizes ORDER BY RANDOM() LIMIT 1),
  'PENDING',
  NOW() - INTERVAL '1 day',
  NOW(),
  NOW()
WHERE EXISTS (SELECT 1 FROM users) AND EXISTS (SELECT 1 FROM prizes);

-- Ticket 6 : Validé
INSERT INTO tickets (code, user_id, prize_id, status, played_at, validated_at, created_at, updated_at)
SELECT
  'TEST-VALIDATED-006',
  (SELECT id FROM users ORDER BY RANDOM() LIMIT 1),
  (SELECT id FROM prizes ORDER BY RANDOM() LIMIT 1),
  'VALIDATED',
  NOW() - INTERVAL '6 days',
  NOW() - INTERVAL '3 days',
  NOW(),
  NOW()
WHERE EXISTS (SELECT 1 FROM users) AND EXISTS (SELECT 1 FROM prizes);

-- Ticket 7 : En attente
INSERT INTO tickets (code, user_id, prize_id, status, played_at, created_at, updated_at)
SELECT
  'TEST-PENDING-007',
  (SELECT id FROM users ORDER BY RANDOM() LIMIT 1),
  (SELECT id FROM prizes ORDER BY RANDOM() LIMIT 1),
  'PENDING',
  NOW(),
  NOW(),
  NOW()
WHERE EXISTS (SELECT 1 FROM users) AND EXISTS (SELECT 1 FROM prizes);

-- Ticket 8 : Validé
INSERT INTO tickets (code, user_id, prize_id, status, played_at, validated_at, created_at, updated_at)
SELECT
  'TEST-VALIDATED-008',
  (SELECT id FROM users ORDER BY RANDOM() LIMIT 1),
  (SELECT id FROM prizes ORDER BY RANDOM() LIMIT 1),
  'VALIDATED',
  NOW() - INTERVAL '7 days',
  NOW() - INTERVAL '4 days',
  NOW(),
  NOW()
WHERE EXISTS (SELECT 1 FROM users) AND EXISTS (SELECT 1 FROM prizes);

-- Ticket 9 : Récupéré
INSERT INTO tickets (code, user_id, prize_id, status, played_at, validated_at, created_at, updated_at)
SELECT
  'TEST-CLAIMED-009',
  (SELECT id FROM users ORDER BY RANDOM() LIMIT 1),
  (SELECT id FROM prizes ORDER BY RANDOM() LIMIT 1),
  'CLAIMED',
  NOW() - INTERVAL '8 days',
  NOW() - INTERVAL '5 days',
  NOW(),
  NOW()
WHERE EXISTS (SELECT 1 FROM users) AND EXISTS (SELECT 1 FROM prizes);

-- Ticket 10 : En attente
INSERT INTO tickets (code, user_id, prize_id, status, played_at, created_at, updated_at)
SELECT
  'TEST-PENDING-010',
  (SELECT id FROM users ORDER BY RANDOM() LIMIT 1),
  (SELECT id FROM prizes ORDER BY RANDOM() LIMIT 1),
  'PENDING',
  NOW() - INTERVAL '12 hours',
  NOW(),
  NOW()
WHERE EXISTS (SELECT 1 FROM users) AND EXISTS (SELECT 1 FROM prizes);

-- ============================================
-- Étape 3 : Vérifier que les tickets ont été créés
-- ============================================

SELECT
  'Tickets créés avec succès !' as message,
  COUNT(*) as total_tickets
FROM tickets
WHERE code LIKE 'TEST-%';

-- Afficher les tickets créés
SELECT
  code,
  status,
  played_at,
  created_at
FROM tickets
WHERE code LIKE 'TEST-%'
ORDER BY created_at DESC;

-- ============================================
-- NOTES IMPORTANTES
-- ============================================

-- Si vous utilisez UUID au lieu d'auto-increment :
-- Ajoutez la colonne id avec gen_random_uuid() ou uuid_generate_v4()

-- Exemple avec UUID :
/*
INSERT INTO tickets (id, code, user_id, prize_id, status, played_at, created_at, updated_at)
SELECT
  gen_random_uuid(),
  'TEST-UUID-001',
  (SELECT id FROM users ORDER BY RANDOM() LIMIT 1),
  (SELECT id FROM prizes ORDER BY RANDOM() LIMIT 1),
  'PENDING',
  NOW(),
  NOW(),
  NOW()
WHERE EXISTS (SELECT 1 FROM users) AND EXISTS (SELECT 1 FROM prizes);
*/

-- Si vos colonnes utilisent camelCase (userId au lieu de user_id) :
-- Utilisez des guillemets doubles : "userId", "prizeId", etc.

-- ============================================
-- Pour supprimer tous les tickets de test :
-- ============================================

-- DELETE FROM tickets WHERE code LIKE 'TEST-%';
