-- =====================================================================
-- seed-business-rules.sql — example business_rules rows for a tenant
--
-- No rule builder UI exists (by design — see Phase 2 scope). This file
-- is both a working example and the reference for the trigger_value /
-- action_value JSON contract, documented in full in
-- src/Rules/BusinessRuleEngine.php's class docblock.
--
-- Usage:
--   1. Set @tenant_id below to a real tenant id.
--   2. Edit/remove rows to match what you actually want.
--   3. Run: mysql -h <db_host> -u <user> -p ai_front_desk < seed-business-rules.sql
--
-- Reminder: priority is ascending (lower number = evaluated/fires
-- first); ALL active matching rules for a trigger fire, not just the
-- first match — except transfer_call, which can only execute once per
-- evaluation pass no matter how many rules match (BusinessRuleEngine
-- enforces this, not the data).
-- =====================================================================

USE ai_front_desk;

SET @tenant_id = 2; -- change me

-- Uncomment to replace this tenant's existing rules instead of
-- appending to them:
-- DELETE FROM business_rules WHERE tenant_id = @tenant_id;

INSERT INTO business_rules
    (tenant_id, name, trigger_type, trigger_value, action_type, action_value, priority, is_active, created_at, updated_at)
VALUES
-- 1. Caller raises a billing/warranty complaint mid-conversation -> get
--    a human on the line. Deliberately NOT overlapping with the AI's
--    own built-in emergency transfer (gas smell, no heat, flooding,
--    electrical hazard — see SystemPromptBuilder) — this rule exists
--    for a DIFFERENT category the AI isn't already handling on its own:
--    a caller who's upset about a past job, wants a refund, or is
--    disputing a warranty. Swap these keywords for whatever actually
--    comes up on your calls.
(@tenant_id, 'Billing/warranty escalation', 'keyword',
 JSON_OBJECT('keywords', JSON_ARRAY('refund', 'warranty', 'cancel my service', 'overcharged'), 'match', 'any'),
 'transfer_call', JSON_OBJECT('reason', 'Caller raised a billing or warranty concern'),
 10, 1, NOW(), NOW()),

-- 2. Caller's intent is "emergency" (no heat, no AC in extreme weather,
--    gas smell, etc.) -> also open a ticket for the office to review
--    later, in addition to whatever transfer already happens (either
--    from the AI's own transferCall or another rule).
(@tenant_id, 'Emergency intent ticket', 'intent_detected',
 JSON_OBJECT('intent', 'emergency'),
 'create_ticket', JSON_OBJECT('subject', 'Emergency HVAC call', 'priority', 'urgent'),
 20, 1, NOW(), NOW()),

-- 3. Call comes in outside business hours -> send a text (ONLY actually
--    sends if the contact already has sms_opt_in = 1 — most after-hours
--    callers won't yet, so in practice this mostly no-ops until a
--    caller has opted in on an earlier call; that's expected, not a bug).
(@tenant_id, 'After-hours text', 'office_hours',
 JSON_OBJECT('when', 'outside'),
 'send_sms', JSON_OBJECT('body', 'Thanks for calling {business_name} — we are currently closed. If this is a heating or cooling emergency, please call our emergency line. Otherwise, we will follow up during business hours.'),
 30, 1, NOW(), NOW());
