-- Add migration script here

PRAGMA foreign_keys = ON;

CREATE TABLE users (
  id INTEGER PRIMARY KEY,
  name TEXT UNIQUE NOT NULL
);

CREATE TABLE channels (
  id INTEGER PRIMARY KEY,
  url TEXT UNIQUE NOT NULL,
  last_fetched INTEGER
);

CREATE TABLE items (
  id INTEGER PRIMARY KEY,
  channel_id INTEGER NOT NULL,
  guid TEXT NOT NULL,
  fetched_at INTEGER NOT NULL,
  UNIQUE(channel_id, guid),
  FOREIGN KEY (channel_id) REFERENCES channels(id)
);

CREATE TABLE feeds (
  id INTEGER PRIMARY KEY,
  user_id INTEGER NOT NULL,
  FOREIGN KEY (user_id) REFERENCES users(id)
);

CREATE TABLE feed_channels (
  feed_id INTEGER NOT NULL,
  channel_id INTEGER NOT NULL,
  -- Decay settings will go here
  PRIMARY KEY (feed_id, channel_id),
  FOREIGN KEY (feed_id) REFERENCES feeds(id),
  FOREIGN KEY (channel_id) REFERENCES channels(id)
);

CREATE TABLE feed_items (
  item_id INTEGER NOT NULL,
  feed_id INTEGER NOT NULL,
  score INTEGER NOT NULL,
  archived BOOLEAN DEFAULT FALSE,
  PRIMARY KEY (item_id, feed_id),
  FOREIGN KEY (feed_id) REFERENCES feeds(id),
  FOREIGN KEY (item_id) REFERENCES items(id)
);
CREATE INDEX idx_feed_items_score
ON feed_items(feed_id, archived, score DESC);
