CREATE TABLE game_plays (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    game_id UUID NOT NULL,
    customer_id UUID NOT NULL,
    prize_id UUID,
    token_used INTEGER DEFAULT 0,
    random_seed VARCHAR(255),
    created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
    
    CONSTRAINT fk_game_plays_game
        FOREIGN KEY (game_id)
        REFERENCES games(id)
        ON DELETE CASCADE,
    
    CONSTRAINT fk_game_plays_customer
        FOREIGN KEY (customer_id)
        REFERENCES customers(id)
        ON DELETE CASCADE,
    
    CONSTRAINT fk_game_plays_prize
        FOREIGN KEY (prize_id)
        REFERENCES game_prizes(id)
        ON DELETE SET NULL,
    
    CONSTRAINT chk_game_plays_token_used_non_negative
        CHECK (token_used >= 0)
);

-- Create indexes
CREATE INDEX idx_game_plays_game_id ON game_plays(game_id);
CREATE INDEX idx_game_plays_customer_id ON game_plays(customer_id);
CREATE INDEX idx_game_plays_prize_id ON game_plays(prize_id);
CREATE INDEX idx_game_plays_created_at ON game_plays(created_at);
CREATE INDEX idx_game_plays_game_customer ON game_plays(game_id, customer_id);
