-- ============================================================ -- Tabelas da Automação (rode no Supabase → SQL Editor) -- ============================================================ -- O app acessa o Supabase com a chave anon (server-side). Mantemos RLS -- desabilitado nestas tabelas para o engine ler/escrever, igual ao restante -- do app (tabela agent_campaigns). -- -- ⚠️ SEGURANÇA: app_config guarda o token da Meta. Com RLS off + chave anon, -- quem tiver a anon key consegue ler. Para uma ferramenta privada de um único -- dono está ok; para travar de vez, prefira definir META_SYSTEM_TOKEN por env -- (token de System User) e NÃO depender do token salvo aqui. -- Regras de automação ---------------------------------------- create table if not exists automation_rules ( id uuid primary key default gen_random_uuid(), name text not null, enabled boolean not null default true, metric text not null, operator text not null, threshold numeric not null, min_spend numeric not null default 0, min_impressions integer not null default 0, action text not null, action_value numeric, cooldown_hours integer not null default 24, created_at timestamptz not null default now(), updated_at timestamptz not null default now() ); -- Logs de atividade do engine -------------------------------- create table if not exists automation_logs ( id uuid primary key default gen_random_uuid(), rule_id text, rule_name text, campaign_id text not null, campaign_name text, action text, details jsonb, status text, created_at timestamptz not null default now() ); create index if not exists automation_logs_created_idx on automation_logs (created_at desc); -- Config chave/valor (token da Meta, etc.) ------------------- create table if not exists app_config ( key text primary key, value text, updated_at timestamptz not null default now() ); -- Mantém o acesso via chave anon (igual ao resto do app). alter table automation_rules disable row level security; alter table automation_logs disable row level security; alter table app_config disable row level security;