CREATE TABLE live_streams (
  id VARCHAR(64) PRIMARY KEY DEFAULT uuid_generate_v4(),
  slug VARCHAR(512) NOT NULL UNIQUE,
  title TEXT,
  description TEXT,
  social_description TEXT,
  listed BOOLEAN NOT NULL DEFAULT FALSE,
  starts_at TIMESTAMP,
  ends_at TIMESTAMP,
  img_url VARCHAR(1024),
  img_width INTEGER,
  img_height INTEGER,
  youtube_code VARCHAR(1024),
  eventbriteid VARCHAR(1024),
  event_type VARCHAR(100),
  event_brite_registration_url VARCHAR(1024),
  allow_in BOOLEAN NOT NULL DEFAULT FALSE,
  white_label_player BOOLEAN NOT NULL DEFAULT TRUE,
  user_id VARCHAR(64) REFERENCES users(id) ON DELETE SET NULL,
  band_id VARCHAR(64) REFERENCES bands(id) ON DELETE SET NULL,
  created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);



CREATE TABLE event_brite_order_uploads  (
  id VARCHAR(64) PRIMARY KEY DEFAULT uuid_generate_v4(),
  upload_file_name VARCHAR(500) NOT NULL,
  created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);

CREATE TABLE event_brite_orders  (
  id VARCHAR(64) PRIMARY KEY DEFAULT uuid_generate_v4(),
  live_stream_id VARCHAR(64) REFERENCES live_streams(id) ON DELETE CASCADE,
  event_brite_order_upload_id VARCHAR(64) REFERENCES event_brite_order_uploads(id) ON DELETE CASCADE,
  event_name VARCHAR(100) NOT NULL,
  order_id VARCHAR(100) NOT NULL UNIQUE,
  ticket_count INTEGER,
  ticket_type VARCHAR(100),
  first_name VARCHAR(100),
  last_name VARCHAR(100),
  email VARCHAR(200),
  created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  times_claimed INTEGER NOT NULL DEFAULT 0
);

CREATE INDEX msuh_record_id_for_feeds ON music_sessions_user_history USING btree (music_session_id);
CREATE INDEX band_id_for_feeds ON music_sessions USING btree (band_id);
CREATE INDEX band_id_2_for_feeds ON recordings USING btree (band_id);
CREATE EXTENSION pg_trgm;
CREATE INDEX trgm_idx_users_email ON users USING gin(email gin_trgm_ops);