-- Compatibilidade para desenvolvimento isolado. -- Em produção estas tabelas pertencem ao Elo Pro e NÃO devem ser recriadas se já existirem. -- Não há coluna de senha, hash, refresh token ou sessão paralela. -- gen_random_uuid() faz parte do núcleo do PostgreSQL 13+. do $$ begin create role app_user nologin nosuperuser nobypassrls; exception when duplicate_object then null; end $$; create table if not exists public.profiles ( id uuid primary key, display_name text not null, is_superadmin boolean not null default false, created_at timestamptz not null default now() ); create table if not exists public.workspaces ( id uuid primary key default gen_random_uuid(), name text not null, created_at timestamptz not null default now() ); create table if not exists public.workspace_members ( workspace_id uuid not null references public.workspaces (id) on delete cascade, user_id uuid not null references public.profiles (id) on delete cascade, role text not null check (role in ('owner', 'admin', 'gestor', 'agent')), created_at timestamptz not null default now(), primary key (workspace_id, user_id) ); create table if not exists public.workspace_module_flags ( workspace_id uuid not null references public.workspaces (id) on delete cascade, module_key text not null, enabled boolean not null default false, updated_at timestamptz not null default now(), primary key (workspace_id, module_key) ); create table if not exists public.audit_log ( id uuid primary key default gen_random_uuid(), workspace_id uuid references public.workspaces (id) on delete cascade, actor_id uuid references public.profiles (id), action text not null, resource_type text, resource_id text, result text not null check (result in ('success', 'denied', 'error')), metadata jsonb not null default '{}'::jsonb, created_at timestamptz not null default now() ); create index if not exists audit_log_workspace_idx on public.audit_log (workspace_id, created_at desc);