DROP TABLE IF EXISTS instruments;
DROP TABLE IF EXISTS params;
DROP TABLE IF EXISTS sockets;
DROP TABLE IF EXISTS instrument_param;
DROP TABLE IF EXISTS instrument_socket;
DROP TABLE IF EXISTS patches;
DROP TABLE IF EXISTS patch_param;
DROP TABLE IF EXISTS patch_socket;
DROP TABLE IF EXISTS snippets;

CREATE TABLE instruments (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    created TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    name TEXT NOT NULL,
    slug TEXT NOT NULL,
    description TEXT NOT NULL,
    panel TEXT NOT NULL
);

CREATE TABLE params (
    param_name TEXT NOT NULL PRIMARY KEY
);

CREATE TABLE sockets (
    socket_name TEXT NOT NULL PRIMARY KEY
);

CREATE TABLE instrument_param (
    instrument TEXT NOT NULL,
    param_name TEXT NOT NULL,
    FOREIGN KEY (instrument) REFERENCES instruments(slug),
    PRIMARY KEY (instrument, param_name)
 );

 CREATE TABLE instrument_socket (
    instrument TEXT NOT NULL,
    socket_name TEXT NOT NULL,
    FOREIGN KEY (instrument) REFERENCES instruments(slug),
    PRIMARY KEY (instrument, socket_name)
 );

CREATE TABLE patches (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    created TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    name TEXT NOT NULL,
    slug TEXT NOT NULL,
    description TEXT NOT NULL,
    cables TEXT NOT NULL,
    instrument TEXT NOT NULL,
    FOREIGN KEY (instrument) REFERENCES instruments(slug)
);

CREATE TABLE patch_param (
    patch_id INTEGER NOT NULL,
    param_name TEXT NOT NULL,
    value TEXT NOT NULL,
    PRIMARY KEY (patch_id, param_name)
);

CREATE TABLE patch_socket (
    patch_id INTEGER NOT NULL,
    socket_name TEXT NOT NULL,
    value TEXT NOT NULL,
    PRIMARY KEY (patch_id, socket_name)
);

CREATE TABLE snippets (
    filename TEXT NOT NULL,
    instrument TEXT NOT NULL,
    patch_name TEXT NOT NULL,
    description TEXT NOT NULL,
    created TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);

