CREATE TABLE certificates (
    certificate_id INT AUTO_INCREMENT PRIMARY KEY,
    user_id INT NOT NULL,
    valid_from DATE NOT NULL,
    valid_to DATE NOT NULL,
);
CREATE TABLE certificate_organizations (
    certificate_id INT,
    organization_id INT,
    PRIMARY KEY (certificate_id, organization_id)
);
CREATE TABLE oauth_clients (
    oapplication_id INT AUTO_INCREMENT PRIMARY KEY,
    oauth_namespace VARCHAR(255) NOT NULL,
	oauth_clientid VARCHAR(255) DEFAULT NULL,
	oauth_clientsecret VARCHAR(255) DEFAULT NULL,
	redirect_uri VARCHAR(255) DEFAULT NULL
);
INSERT INTO oauth_clients (oauth_namespace) VALUES ('ANAF');

CREATE TABLE oauth_tokens (
    token_id INT AUTO_INCREMENT PRIMARY KEY,
    certificate_id INT NOT NULL,
    oapplication_id INT NOT NULL,
	user_id INT DEFAULT NULL,
    token VARCHAR(255) NOT NULL,
    refresh_token VARCHAR(255) NOT NULL,
    expires_in INT NOT NULL,
    expiration_date TIMESTAMP NOT NULL,
    auth_requested_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    code_for_exchange VARCHAR(255) NOT NULL
);

CREATE TABLE certificate_extra_users (
	certificate_id INT,
	user_id INT,
	PRIMARY KEY (certificate_id, user_id)
);