CREATE TABLE items (
    inode           BIGINT PRIMARY KEY,
    name            TEXT NOT NULL,
    original_path   TEXT NOT NULL,
    local_path      TEXT NOT NULL,
    file_type       TEXT NOT NULL CHECK (file_type IN ('directory', 'file')),
    hash            BIGINT NOT NULL
);

CREATE TABLE music_metadata (
    inode               BIGINT PRIMARY KEY REFERENCES items(inode) ON DELETE CASCADE,
    track_title         TEXT NOT NULL,
    album               TEXT NOT NULL,
    track_number        INTEGER NOT NULL,
    header              BYTEA NOT NULL,
    real_audio_start    BIGINT NOT NULL
);

CREATE TABLE music_metadata_artists (
    inode   BIGINT NOT NULL REFERENCES music_metadata(inode) ON DELETE CASCADE,
    artist  TEXT NOT NULL,
    PRIMARY KEY (inode, artist)
);

CREATE TABLE music_metadata_other_tags (
    inode       BIGINT NOT NULL REFERENCES music_metadata(inode) ON DELETE CASCADE,
    position    INTEGER NOT NULL,
    tag         TEXT NOT NULL,
    PRIMARY KEY (inode, position)
);

CREATE TABLE music_metadata_pictures (
    inode           BIGINT NOT NULL REFERENCES music_metadata(inode) ON DELETE CASCADE,
    position        INTEGER NOT NULL,
    block_header    BYTEA NOT NULL,
    data_offset     BIGINT NOT NULL,
    data_length     BIGINT NOT NULL,
    PRIMARY KEY (inode, position)
);
