import 'preferences.dart';

CREATE TABLE users (
    id INTEGER NOT NULL PRIMARY KEY,
    name TEXT NOT NULL,
    biography TEXT,
    preferences TEXT MAPPED BY `const PreferencesConverter()`,
    profile_picture BLOB
);

CREATE INDEX users_name ON users (name);

CREATE TABLE follows (
    followed INTEGER NOT NULL REFERENCES users (id),
    follower INTEGER NOT NULL REFERENCES users (id),
    PRIMARY KEY (followed, follower)
);

CREATE VIEW popular_users AS
  SELECT * FROM users
    ORDER BY (SELECT count(*) FROM follows WHERE followed = users.id);
