-- so we can default uuid fields using uuid_generate_v4. nice to have.
CREATE EXTENSION "uuid-ossp";

CREATE TABLE users (
    id VARCHAR(64) PRIMARY KEY DEFAULT uuid_generate_v4(),
    name VARCHAR(50) NOT NULL,
    email VARCHAR(255) NOT NULL UNIQUE,
    remember_token VARCHAR(255) UNIQUE,
    password_digest VARCHAR(255) NOT NULL,
    admin boolean NOT NULL DEFAULT false,
    created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);

CREATE INDEX remember_token_idx ON users(remember_token);

-- adhoc jam sessions
CREATE TABLE jam_sessions (
    id VARCHAR(64) PRIMARY KEY DEFAULT uuid_generate_v4(),
    name VARCHAR(8000),
    public boolean NOT NULL DEFAULT true,
    user_id VARCHAR(64) NOT NULL REFERENCES users(id),
    created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);

-- users currently in the jam session
CREATE TABLE jam_session_members (
    id VARCHAR(64) PRIMARY KEY DEFAULT uuid_generate_v4(),
    user_id VARCHAR(64) REFERENCES users(id) ON DELETE CASCADE,
    jam_session_id VARCHAR(64) REFERENCES jam_sessions(id) ON DELETE CASCADE,
    created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);

ALTER TABLE jam_session_members ADD CONSTRAINT user_jam_session_uniqkey UNIQUE (user_id, jam_session_id);

-- create a utility method to truncate all tables

CREATE OR REPLACE FUNCTION truncate_tables() RETURNS void AS $$
DECLARE
    statements CURSOR FOR
        SELECT tablename FROM pg_tables
        WHERE schemaname = 'public';
BEGIN
    FOR stmt IN statements LOOP
        EXECUTE 'TRUNCATE TABLE ' || quote_ident(stmt.tablename) || ' CASCADE;';
    END LOOP;
END;
$$ LANGUAGE plpgsql;

-- create a utility that bootstraps a user
CREATE OR REPLACE FUNCTION bootstrap_users() RETURNS void AS $$
DECLARE

BEGIN
	-- non guid used for test user.  test user won't be in actual database, but '1' is much easier to script that 242423-42-4-24234
	-- password is jam123
	INSERT INTO users (id, name, email, remember_token, password_digest) VALUES ('1', 'test', 'test@jamkazam.com', 'NQubl-z16Em94tnSdofObw', '$2a$10$QyaNTLVX5DAaJ.JL21kDWeUQqdh3Qh7JQbdRgE82x1Cib7HWNcHXC');
END;
$$ LANGUAGE plpgsql;

select bootstrap_users();

