-- =============================================================================
-- aggie_assistant_seed.sql - switches on Aggie, the in-app AI assistant, for this deployment's account and
-- gives her a standing brief about running its day-to-day operations. Team Chat needs no switch: its tables come
-- from add_internal_chat.sql and the page is open to every signed-in user.
-- Aggie answers questions about the app and does tasks as the signed-in person (changes always wait for that
-- person's approval). She also needs an AI model key: add it in Agents Management > API keys.
-- Idempotent: an existing setting row (edited in Agents Management > AI Agents) is never overwritten.
-- Requires seed_larcare_business_and_site.sql (to find the account) and add_ai_agent_controls.sql first.
-- =============================================================================
SET @site_id := (SELECT id FROM dim_web_sites WHERE public_site_key = 'pSlfeq9OCm5MvZyoFguIK7jy0uz2aKShJkxZxTSWXwzPOpYJ' AND is_deleted = 0 LIMIT 1);
SET @tenant_id := (SELECT tenant_id FROM dim_web_sites WHERE id = @site_id LIMIT 1);

INSERT INTO fact_ai_agent_setting (tenant_id, agent_code, is_enabled, model_name, extra_instructions, record_history, retention_days, max_steps, access_mode)
SELECT @tenant_id, 'SARA', 1, NULL,
       'You help the team run this organisation''s day-to-day operations. The top bar shows which business is selected; always say which business you are reporting on, and ask before assuming one when the answer differs by business. You can look across CRM (leads, deals, clients), invoices and finance, HR and payroll, projects and tasks, inventory and assets, documents, mail, Team Chat and the LarCare website and its forms. When asked what needs attention, check the live data first (overdue invoices, open leads and deals, tasks due, expiring licences, unread requests) and then suggest the most useful next steps in order of urgency. Prefer short answers with names and numbers over general advice.',
       1, NULL, NULL, 'ALL'
FROM DUAL
WHERE @tenant_id IS NOT NULL
  AND NOT EXISTS (SELECT 1 FROM fact_ai_agent_setting WHERE tenant_id = @tenant_id AND agent_code = 'SARA');
