
-- vtubers table
CREATE TABLE api.vtubers (
  id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
  display_name TEXT NOT NULL,
  chaturbate TEXT,
  twitter TEXT,
  patreon TEXT,
  twitch TEXT,
  tiktok TEXT,
  onlyfans TEXT,
  youtube TEXT,
  linktree TEXT,
  carrd TEXT,
  fansly TEXT,
  pornhub TEXT,
  discord TEXT,
  reddit TEXT,
  throne TEXT,
  instagram TEXT,
  facebook TEXT,
  merch TEXT,
  slug TEXT NOT NULL,
  description1 TEXT,
  description2 TEXT,
  image TEXT NOT NULL,
  theme_color VARCHAR(7) NOT NULL,
  image_blur TEXT DEFAULT 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAABmJLR0QA/wD/AP+gvaeTAAAADUlEQVQImWMwtf//HwAEkwJzh0T9qwAAAABJRU5ErkJggg==',
  fansly_id TEXT,
  chaturbate_id TEXT,
  twitter_id TEXT
  -- F.Y.I., relations as follows
  -- toys (one-to-many)
  -- vods (one-to-many)
  -- streams (one-to-many)
);
GRANT all ON api.vtubers TO automation;
GRANT SELECT ON api.vtubers TO web_anon;


-- streams table
CREATE TABLE api.streams (
  id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
  url TEXT NOT NULL,
  platform_notification_type TEXT,
  date timestamp(6) without time zone,
  created_at timestamp(6) without time zone,
  vtuber uuid,
  FOREIGN KEY (vtuber) REFERENCES api.vtubers(id),
  tweet TEXT,
  archive_status TEXT,
  is_chaturbate_stream BOOLEAN,
  is_fansly_stream BOOLEAN
);
GRANT all ON api.streams TO automation;
GRANT SELECT ON api.streams TO web_anon;

-- toys table
CREATE TABLE api.toys (
  id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
  -- relation. one toy to many tags
  -- relation. one toy to many vtubers
  make TEXT NOT NULL,
  model TEXT NOT NULL,
  image TEXT NOT NULL DEFAULT 'https://futureporn-b2.b-cdn.net/default-thumbnail.webp'
);
GRANT all ON api.toys TO automation;
GRANT SELECT ON api.toys TO web_anon;


-- tags table
CREATE TABLE api.tags (
  id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
  name TEXT NOT NULL UNIQUE,
  toy_id uuid,
  FOREIGN KEY (toy_id) REFERENCES api.toys
);
GRANT all ON api.tags TO automation;
GRANT SELECT ON api.tags TO web_anon;

-- toys-tags junction table
CREATE TABLE api.toys_tags(
  id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
  toy_id uuid,
  tag_id uuid,
  CONSTRAINT fk_toys FOREIGN KEY(toy_id) REFERENCES api.toys(id),
  CONSTRAINT fk_tags FOREIGN KEY(tag_id) REFERENCES api.tags(id)
);
GRANT all ON api.toys_tags TO automation;
GRANT SELECT ON api.toys_tags TO web_anon;

-- tags-vods junction table
-- toys-vtubers junction table
CREATE TABLE api.toys_vtubers(
  id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
  toy_id uuid,
  vtuber_id uuid,
  CONSTRAINT fk_toys FOREIGN KEY(toy_id) REFERENCES api.toys(id),
  CONSTRAINT fk_vtubers FOREIGN KEY(vtuber_id) REFERENCES api.vtubers(id)
);
GRANT all ON api.toys_vtubers TO automation;
GRANT SELECT ON api.toys_vtubers TO web_anon;




-- vods table
CREATE TABLE api.vods (
  id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
  stream_id uuid NOT NULL,
  FOREIGN KEY (stream_id) REFERENCES api.streams(id),
  video_cid TEXT UNIQUE,
  CONSTRAINT check_video_cid CHECK (video_cid ~ 'Qm[1-9A-HJ-NP-Za-km-z]{44,}|b[A-Za-z2-7]{58,}|B[A-Z2-7]{58,}|z[1-9A-HJ-NP-Za-km-z]{48,}|F[0-9A-F]{50,}'),
  announce_title TEXT,
  announce_url TEXT,
  note TEXT,
  date timestamp(6) without time zone,
  spoilers TEXT,
  title TEXT,
  uploader uuid,
  mux_asset_id TEXT,
  mux_playback_id TEXT,
  s3_key TEXT,
  s3_id TEXT,
  thumbnail TEXT
);
GRANT all ON api.vods TO automation;
GRANT SELECT ON api.vods TO web_anon;


-- tags-vods junction table
CREATE TABLE api.tags_vods(
  id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
  tag_id uuid,
  vod_id uuid,
  CONSTRAINT fk_tags FOREIGN KEY(tag_id) REFERENCES api.tags(id),
  CONSTRAINT fk_vods FOREIGN KEY(vod_id) REFERENCES api.vods(id)
);
GRANT all ON api.tags_vods TO automation;
GRANT SELECT ON api.tags_vods TO web_anon;
