CREATE TABLE connections (
    id VARCHAR(64) PRIMARY KEY DEFAULT uuid_generate_v4(),
    user_id VARCHAR(64) REFERENCES users(id) ON DELETE CASCADE,
    client_id VARCHAR(64) NOT NULL UNIQUE,
    created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);

-- friendships
CREATE TABLE friendships (
    id VARCHAR(64) PRIMARY KEY DEFAULT uuid_generate_v4(),
    user_id VARCHAR(64) REFERENCES users(id) ON DELETE CASCADE,
    friend_id VARCHAR(64) REFERENCES users(id) ON DELETE CASCADE,
    created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);

ALTER TABLE friendships ADD CONSTRAINT user_friend_uniqkey UNIQUE (user_id, friend_id);

-- friend requests
CREATE TABLE friend_requests (
    id VARCHAR(64) PRIMARY KEY DEFAULT uuid_generate_v4(),
    user_id VARCHAR(64) REFERENCES users(id) ON DELETE CASCADE,
    friend_id VARCHAR(64) REFERENCES users(id) ON DELETE CASCADE,
    accepted BOOLEAN,
    created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);

ALTER TABLE friend_requests ADD CONSTRAINT user_friend_request_uniqkey UNIQUE (user_id, friend_id);

-- bands
CREATE TABLE bands (
    id VARCHAR(64) PRIMARY KEY DEFAULT uuid_generate_v4(),
    name VARCHAR(1024) NOT NULL,
    website VARCHAR(4000) NULL,
    biography VARCHAR(4000) NOT NULL,
    created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);

-- genres
CREATE TABLE genres (
    id VARCHAR(64) PRIMARY KEY DEFAULT uuid_generate_v4(),
    description VARCHAR(1024) NOT NULL,
    created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);

-- band -> genre mapping
CREATE TABLE bands_genres (
    id VARCHAR(64) PRIMARY KEY DEFAULT uuid_generate_v4(),
    band_id VARCHAR(64) NOT NULL REFERENCES bands(id) ON DELETE CASCADE,
    genre_id VARCHAR(64) NOT NULL REFERENCES genres(id) ON DELETE CASCADE,
    created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);

ALTER TABLE bands_genres ADD CONSTRAINT band_genre_uniqkey UNIQUE (band_id, genre_id);

-- musician -> band mapping
CREATE TABLE bands_musicians (
    id VARCHAR(64) PRIMARY KEY DEFAULT uuid_generate_v4(),
    band_id VARCHAR(64) NOT NULL REFERENCES bands(id) ON DELETE CASCADE,
    user_id VARCHAR(64) NOT NULL REFERENCES users(id) ON DELETE CASCADE,
    admin BOOLEAN NOT NULL DEFAULT false,
    created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);

ALTER TABLE bands_musicians ADD CONSTRAINT band_musician_uniqkey UNIQUE (band_id, user_id);

-- instruments
CREATE TABLE instruments (
    id VARCHAR(64) PRIMARY KEY DEFAULT uuid_generate_v4(),
    description VARCHAR(1024) NOT NULL,
    created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);

-- musician -> instrument mapping
CREATE TABLE musicians_instruments (
    id VARCHAR(64) PRIMARY KEY DEFAULT uuid_generate_v4(),
    user_id VARCHAR(64) NOT NULL REFERENCES users(id) ON DELETE CASCADE,
    instrument_id VARCHAR(64) NOT NULL REFERENCES instruments(id) ON DELETE CASCADE,
    proficiency_level SMALLINT NOT NULL,
    priority SMALLINT NOT NULL DEFAULT 1,
    created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);

ALTER TABLE musicians_instruments ADD CONSTRAINT musician_instrument_uniqkey UNIQUE (user_id, instrument_id);

-- add musician flag to users table
ALTER TABLE users ADD COLUMN musician BOOLEAN NOT NULL DEFAULT false;
