-- Migration: Ajout de la table pour les tirages au sort du gros lot
-- Date: 2025-11-13

-- Table pour stocker les tirages au sort
CREATE TABLE IF NOT EXISTS grand_prize_draws (
  id UUID PRIMARY KEY DEFAULT gen_random_uuid(),

  -- Informations du tirage
  draw_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  conducted_by UUID NOT NULL REFERENCES users(id), -- Admin qui a lancé le tirage

  -- Gagnant
  winner_id UUID NOT NULL REFERENCES users(id),
  winner_email VARCHAR(255) NOT NULL,
  winner_name VARCHAR(255) NOT NULL,

  -- Détails du lot
  prize_name VARCHAR(255) NOT NULL,
  prize_value VARCHAR(100),

  -- Participants
  total_participants INTEGER NOT NULL,
  eligible_participants INTEGER NOT NULL,

  -- Critères d'éligibilité utilisés
  criteria JSONB, -- Stocke les critères du tirage

  -- Statut
  status VARCHAR(50) DEFAULT 'COMPLETED', -- COMPLETED, NOTIFIED, CLAIMED
  notified_at TIMESTAMP,
  claimed_at TIMESTAMP,

  -- Metadata
  notes TEXT,
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,

  CONSTRAINT unique_grand_prize_draw UNIQUE (draw_date)
);

-- Index pour les recherches
CREATE INDEX IF NOT EXISTS idx_grand_prize_draws_winner ON grand_prize_draws(winner_id);
CREATE INDEX IF NOT EXISTS idx_grand_prize_draws_date ON grand_prize_draws(draw_date);
CREATE INDEX IF NOT EXISTS idx_grand_prize_draws_status ON grand_prize_draws(status);

-- Commentaires
COMMENT ON TABLE grand_prize_draws IS 'Stocke les tirages au sort du gros lot final';
COMMENT ON COLUMN grand_prize_draws.conducted_by IS 'Admin qui a effectué le tirage';
COMMENT ON COLUMN grand_prize_draws.criteria IS 'Critères d''éligibilité en JSON (ex: {minTickets: 1, verified: true})';
COMMENT ON COLUMN grand_prize_draws.status IS 'COMPLETED: tirage effectué, NOTIFIED: gagnant notifié, CLAIMED: lot récupéré';
