CREATE TABLE artists (
  id SERIAL PRIMARY KEY,
  name VARCHAR UNIQUE NOT NULL
);

CREATE INDEX artists_name_idx ON artists(name);

CREATE TABLE albums (
  id SERIAL PRIMARY KEY,
  name VARCHAR NOT NULL,
  year INTEGER,
  track_total INTEGER
);

CREATE INDEX albums_name_idx ON albums(name);

CREATE TABLE media_files (
  id SERIAL PRIMARY KEY,
  name VARCHAR NOT NULL,
  disc_number INTEGER NOT NULL DEFAULT 1,
  track_number INTEGER,
  play_count INTEGER NOT NULL DEFAULT 0,
  file_path VARCHAR NOT NULL,
  file_hash VARCHAR NOT NULL,
  meta_info_source VARCHAR NOT NULL, -- 'tag' or 'filesystem'
  date_added TIMESTAMP NOT NULL DEFAULT NOW(),
  last_played TIMESTAMP,
  comment VARCHAR DEFAULT ''
);

CREATE INDEX media_files_name_idx ON media_files(name);

CREATE TABLE artists_media_files (
  artist_id INTEGER NOT NULL REFERENCES artists(id) ON DELETE CASCADE,
  media_file_id INTEGER NOT NULL REFERENCES media_files(id) ON DELETE CASCADE,
  PRIMARY KEY (artist_id, media_file_id)
);

CREATE TABLE albums_media_files (
  album_id INTEGER NOT NULL REFERENCES albums(id) ON DELETE CASCADE,
  media_file_id INTEGER NOT NULL REFERENCES media_files(id) ON DELETE CASCADE,
  PRIMARY KEY (album_id, media_file_id)
);

CREATE TABLE tags (
  id SERIAL PRIMARY KEY,
  name VARCHAR UNIQUE NOT NULL
);

CREATE TABLE playlists (
  id SERIAL PRIMARY KEY,
  user_created BOOLEAN NOT NULL DEFAULT FALSE,
  last_used TIMESTAMP NOT NULL DEFAULT NOW(),
  name VARCHAR NOT NULL,
  mod_count INTEGER NOT NULL DEFAULT 0,
  media_file_count INTEGER NOT NULL DEFAULT 0,
  copied_from_id INTEGER REFERENCES playlists(id) DEFAULT NULL,
  created_at TIMESTAMP NOT NULL DEFAULT NOW()
);

CREATE TABLE playlists_media_files (
  playlist_id INTEGER NOT NULL REFERENCES playlists(id) ON DELETE CASCADE,
  media_file_id INTEGER NOT NULL REFERENCES media_files(id) ON DELETE CASCADE,
  position INTEGER NOT NULL DEFAULT 0,
  PRIMARY KEY (playlist_id, media_file_id, position)
);

CREATE TABLE bookmarks (
  id SERIAL PRIMARY KEY,
  name VARCHAR,
  user_created BOOLEAN NOT NULL DEFAULT FALSE,
  playlist_id INTEGER NOT NULL REFERENCES playlists(id) ON DELETE CASCADE,
  media_file_id INTEGER NOT NULL REFERENCES media_files(id) ON DELETE CASCADE,
  play_index INTEGER NOT NULL,
  created_at TIMESTAMP NOT NULL DEFAULT NOW()
);

CREATE INDEX bookmarks_playlist_id_idx ON bookmarks (playlist_id);
CREATE INDEX bookmarks_media_file_id_idx ON bookmarks (media_file_id);

CREATE TABLE media_files_tags (
  media_file_id INTEGER REFERENCES media_files(id) ON DELETE CASCADE,
  tag_id INTEGER REFERENCES tags(id) ON DELETE CASCADE,
  PRIMARY KEY (media_file_id, tag_id)
);

CREATE TABLE images (
  id SERIAL PRIMARY KEY,
  url VARCHAR
);

CREATE TABLE artists_images (
  artist_id INTEGER REFERENCES artists (id) ON DELETE CASCADE,
  image_id INTEGER REFERENCES images (id) ON DELETE CASCADE,
  PRIMARY KEY (artist_id, image_id)
);

CREATE TABLE albums_images (
  album_id INTEGER REFERENCES albums (id) ON DELETE CASCADE,
  image_id INTEGER REFERENCES images (id) ON DELETE CASCADE,
  PRIMARY KEY (album_id, image_id)
);

CREATE TABLE artists_albums (
  artist_id INTEGER NOT NULL REFERENCES artists (id) ON DELETE CASCADE,
  album_id INTEGER NOT NULL REFERENCES albums (id) ON DELETE CASCADE,
  PRIMARY KEY (artist_id, album_id)
);
