CREATE TABLE IF NOT EXISTS shared.usuario (
	"id" INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY,
	"uuid" UUID NOT NULL UNIQUE,
	"is_active" SMALLINT NOT NULL DEFAULT 1,
	"is_root" SMALLINT NOT NULL DEFAULT 0,
	"nome_completo" VARCHAR(150) NOT NULL,
	"nome_usuario" VARCHAR(50) NOT NULL UNIQUE,
	"email" VARCHAR(255) NOT NULL UNIQUE,
	"senha_hash" VARCHAR(255) NOT NULL,
	"created_at" TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
	"updated_at" TIMESTAMP DEFAULT NULL,
	"deleted_at" TIMESTAMP DEFAULT NULL,
	PRIMARY KEY("id")
);

CREATE TABLE IF NOT EXISTS shared.empresa (
	"id" INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY,
	"uuid" UUID NOT NULL UNIQUE,
	"is_active" SMALLINT NOT NULL DEFAULT 1,
	"nome_empresarial" VARCHAR(150) NOT NULL,
	"nome_fantasia" VARCHAR(60) DEFAULT NULL,
	"tipo" VARCHAR(50) NOT NULL DEFAULT 'MATRIZ',
	"matriz_id" INTEGER DEFAULT NULL,
	"document_cnpj" CHAR(14) NOT NULL UNIQUE,
	"document_ie" CHAR(14) DEFAULT NULL,
	"document_im" CHAR(14) DEFAULT NULL,
	"regime_tributario" CHAR(1) NOT NULL,
	"end_cep" VARCHAR(8) NOT NULL,
	"end_ibge" VARCHAR(8) NOT NULL,
	"end_logradouro" VARCHAR(150) NOT NULL,
	"end_numero" VARCHAR(20) NOT NULL,
	"end_complemento" VARCHAR(50) DEFAULT NULL,
	"end_bairro" VARCHAR(50) NOT NULL,
	"end_cidade" VARCHAR(150) NOT NULL,
	"end_uf" CHAR(2) NOT NULL,
	"created_at" TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
	"updated_at" TIMESTAMP DEFAULT NULL,
	"deleted_at" TIMESTAMP DEFAULT NULL,
	PRIMARY KEY("id")
);

CREATE TABLE IF NOT EXISTS shared.usuario_empresa (
	"usuario_id" INTEGER NOT NULL,
	"empresa_id" INTEGER NOT NULL,
	PRIMARY KEY("usuario_id", "empresa_id")
);

CREATE TABLE IF NOT EXISTS shared.usuario_sessao (
	"id" INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY,
	"uuid" UUID NOT NULL UNIQUE,
	"usuario_id" INTEGER NOT NULL,
	"user_agent" TEXT DEFAULT NULL,
	"ip_address" VARCHAR(45) DEFAULT NULL,
	"created_at" TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
	"revoked_at" TIMESTAMP DEFAULT NULL,
	PRIMARY KEY("id")
);

CREATE TABLE IF NOT EXISTS shared.contato (
	"id" INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY,
	"uuid" UUID NOT NULL UNIQUE,
	"empresa_id" INTEGER NOT NULL,
	"is_active" SMALLINT NOT NULL DEFAULT 1,
	"tipo" VARCHAR(50) NOT NULL,
	"nome_empresarial" VARCHAR(150) NOT NULL,
	"nome_fantasia" VARCHAR(60) DEFAULT NULL,
	"personalidade" CHAR(2) NOT NULL DEFAULT 'PJ',
	"document_cpf" CHAR(11) DEFAULT NULL,
	"document_cnpj" CHAR(14) DEFAULT NULL,
	"regime_tributario" CHAR(1) DEFAULT NULL,
	"contribuinte_icms" CHAR(1) DEFAULT NULL,
	"orgao_publico" VARCHAR(9) NOT NULL DEFAULT 'NAO',
	"document_ie" CHAR(14) DEFAULT NULL,
	"document_im" CHAR(14) DEFAULT NULL,
	"document_is" CHAR(9) DEFAULT NULL,
	"end_pais" VARCHAR(255) DEFAULT NULL,
	"end_cep" VARCHAR(8) NOT NULL,
	"end_ibge" VARCHAR(8) DEFAULT NULL,
	"end_logradouro" VARCHAR(150) NOT NULL,
	"end_numero" VARCHAR(20) NOT NULL,
	"end_complemento" VARCHAR(50) DEFAULT NULL,
	"end_bairro" VARCHAR(50) NOT NULL,
	"end_cidade" VARCHAR(150) NOT NULL,
	"end_uf" CHAR(2) NOT NULL,
	"info_email" VARCHAR(255) DEFAULT NULL,
	"info_email_nfe" VARCHAR(80) DEFAULT NULL,
	"info_observacao" TEXT DEFAULT NULL,
	"info_telefone" VARCHAR(11) DEFAULT NULL,
	"info_uso_consumo_ibs_cbs" CHAR(1) NOT NULL DEFAULT '0',
	"created_at" TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
	"updated_at" TIMESTAMP DEFAULT NULL,
	"deleted_at" TIMESTAMP DEFAULT NULL,
	PRIMARY KEY("id")
);

ALTER TABLE shared.usuario_empresa ADD FOREIGN KEY("usuario_id") REFERENCES shared.usuario("id") ON UPDATE NO ACTION ON DELETE NO ACTION;
ALTER TABLE shared.usuario_empresa ADD FOREIGN KEY("empresa_id") REFERENCES shared.empresa("id") ON UPDATE NO ACTION ON DELETE NO ACTION;
ALTER TABLE shared.usuario_sessao ADD FOREIGN KEY("usuario_id") REFERENCES shared.usuario("id") ON UPDATE NO ACTION ON DELETE NO ACTION;
ALTER TABLE shared.contato ADD FOREIGN KEY("empresa_id") REFERENCES shared.empresa("id") ON UPDATE NO ACTION ON DELETE NO ACTION;