-- ===============================================================
-- Status do Usuário
-- ===============================================================
CREATE TABLE IF NOT EXISTS public.usuario_status (
    id BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
    descricao VARCHAR(100) NOT NULL
);

INSERT INTO public.usuario_status (id, descricao) VALUES
(1, 'Ativo'),
(2, 'Inativo'),
(3, 'Pendente'),
(4, 'Suspenso');

SELECT setval(pg_get_serial_sequence('public.usuario_status','id'), (SELECT MAX(id) FROM public.usuario_status));

-- ===============================================================
-- Usuário
-- ===============================================================
CREATE TABLE IF NOT EXISTS public.usuario (
    id BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
    uuid UUID NOT NULL UNIQUE,
    status_id BIGINT NOT NULL DEFAULT 1 REFERENCES public.usuario_status(id) ON UPDATE CASCADE ON DELETE CASCADE,
    is_root SMALLINT NOT NULL DEFAULT 0,
    nome_completo VARCHAR(255) NOT NULL,
    nome_usuario VARCHAR(50) NOT NULL UNIQUE,
    email VARCHAR(255) NOT NULL UNIQUE,
    senha_hash VARCHAR(255) NOT NULL,
    need_update_password SMALLINT NOT NULL DEFAULT 1,
    days_to_expire_password BIGINT NOT NULL DEFAULT 60,
    last_password_update TIMESTAMP DEFAULT NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT NULL,
    deleted_at TIMESTAMP DEFAULT NULL
);

INSERT INTO public.usuario (id, uuid, status_id, is_root, nome_completo, nome_usuario, email, senha_hash, need_update_password, days_to_expire_password, last_password_update) VALUES
(1, '0199f8dd-3a65-7c11-bdf2-ba3294f4c28e', 1, 1, 'Claudecio Martins', 'cjunior', 'contato@claudecio.is-a.dev', '$argon2id$v=19$m=65536,t=4,p=1$cFFXL3VVREREM1R5Szg1Nw$K6u1cUbTCeTMDHT3iP93uRtMfc5PXX6NpC1C2I5peHc', -1, -1, NULL);

SELECT setval(pg_get_serial_sequence('public.usuario','id'), (SELECT MAX(id) FROM public.usuario));

-- ===============================================================
-- Token Acesso Usuário
-- ===============================================================
CREATE TABLE IF NOT EXISTS public.usuario_token (
    id BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
    usuario_id BIGINT NOT NULL REFERENCES public.usuario(id) ON UPDATE CASCADE ON DELETE RESTRICT,
    usuario_ip VARCHAR(45) NOT NULL,
    usuario_user_agent TEXT NOT NULL,
    token TEXT NOT NULL UNIQUE,
    is_revoked SMALLINT NOT NULL DEFAULT 0,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    expires_at TIMESTAMP NOT NULL,
    revoked_at TIMESTAMP DEFAULT NULL
);

-- ===============================================================
-- Relacionamento USUARIO <-> EMPRESA Para quais empresas o usuario tem acesso
-- ===============================================================
CREATE TABLE IF NOT EXISTS public.usuario_empresa (
    empresa_id BIGINT NOT NULL REFERENCES public.empresa(id) ON UPDATE CASCADE ON DELETE CASCADE,
    usuario_id BIGINT NOT NULL REFERENCES public.usuario(id) ON UPDATE CASCADE ON DELETE CASCADE,
    PRIMARY KEY (empresa_id, usuario_id)
);