--
-- Artists (should be applicable to all art)
--
CREATE TABLE IF NOT EXISTS artists (
    id text NOT NULL,
    name text,
    website text
);
ALTER TABLE artists ADD CONSTRAINT artists_pk PRIMARY KEY (id);

--
-- Music releases
--
CREATE TABLE IF NOT EXISTS musicreleases (
    id character varying(64) NOT NULL,
    title text NOT NULL,
    description text,
    type text,
    release_date DATE NOT NULL,
    artwork text,
    buyname text,
    buylink text
);
ALTER TABLE musicreleases ADD CONSTRAINT musicreleases_pk PRIMARY KEY (id);

--
-- Music links (external platform links under a release)
--
CREATE TABLE IF NOT EXISTS musiclinks (
    release character varying(64) NOT NULL,
    name text NOT NULL,
    url text
);
ALTER TABLE musiclinks ADD CONSTRAINT musiclinks_pk PRIMARY KEY (release, name);

--
-- Music credits (artist credits under a release)
--
CREATE TABLE IF NOT EXISTS musiccredits (
    release character varying(64) NOT NULL,
    artist text NOT NULL,
    role text,
    is_primary boolean
);
ALTER TABLE musiccredits ADD CONSTRAINT musiccredits_pk PRIMARY KEY (release, artist);

--
-- Music tracks (tracks under a release)
--
CREATE TABLE IF NOT EXISTS musictracks (
    release character varying(64) NOT NULL,
    number integer NOT NULL,
    title text NOT NULL,
    description text,
    lyrics text,
    preview_url text
);
ALTER TABLE musictracks ADD CONSTRAINT musictracks_pk PRIMARY KEY (release, number);

--
-- Foreign keys
--

ALTER TABLE musiccredits ADD CONSTRAINT IF NOT EXISTS musiccredits_artist_fk FOREIGN KEY (artist) REFERENCES artists(id) ON DELETE CASCADE ON UPDATE CASCADE;

ALTER TABLE musiccredits ADD CONSTRAINT IF NOT EXISTS musiccredits_release_fk FOREIGN KEY (release) REFERENCES musicreleases(id) ON DELETE CASCADE;

ALTER TABLE musiclinks ADD CONSTRAINT IF NOT EXISTS musiclinks_release_fk FOREIGN KEY (release) REFERENCES musicreleases(id) ON UPDATE CASCADE ON DELETE CASCADE;

ALTER TABLE musictracks ADD CONSTRAINT IF NOT EXISTS musictracks_release_fk FOREIGN KEY (release) REFERENCES musicreleases(id) ON DELETE CASCADE;
