USE db_abos_v0.1;

-- =============================================================================================
-- seed_abos_inquiry_form.sql — the intake form behind /abos's "Get started" CTA.
--
-- ABOS itself has no self-service plan/checkout API (unlike Go Digital's separate platform):
-- dim_subscription_plans holds only one placeholder Enterprise/£0 row, and onboarding is done
-- manually by staff via abos-web's SaaS Admin console. So the ABOS landing page's "order" flow
-- is a lead-capture form (same generic dim_web_forms/fact_web_form_submissions engine every
-- other TechMAC form already uses, routed into CRM by CrmDealFromWebService) rather than a real
-- order, matching what's actually built.
--
-- Safe to re-run: WHERE NOT EXISTS seed guards.
-- =============================================================================================

SET @tenant_id := 1;
SET @techmac_business_id := (SELECT id FROM businesses WHERE tenant_id = @tenant_id AND name = 'TechMAC' LIMIT 1);
SET @techmac_site_id := (SELECT id FROM dim_web_sites WHERE tenant_id = @tenant_id AND name = 'TechMAC' LIMIT 1);

INSERT INTO dim_web_forms (tenant_id, business_id, site_id, form_key, name, success_message, is_active, is_deleted)
SELECT @tenant_id, @techmac_business_id, @techmac_site_id,
       'abos-inquiry', 'ABOS Discovery Request',
       'Thanks. A TechMAC architect will review your business and follow up with a tailored ABOS proposal within one business day.',
       1, 0
WHERE @techmac_site_id IS NOT NULL AND NOT EXISTS (
    SELECT 1 FROM dim_web_forms WHERE site_id = @techmac_site_id AND form_key = 'abos-inquiry'
);

SET @abos_form_id := (SELECT id FROM dim_web_forms WHERE site_id = @techmac_site_id AND form_key = 'abos-inquiry');

INSERT INTO dim_web_form_fields (form_id, field_key, label, field_type, is_required, sort_order, config_json)
SELECT * FROM (
    SELECT @abos_form_id AS form_id, 'name' AS field_key, 'Name' AS label, 'text' AS field_type, 1 AS is_required, 1 AS sort_order, NULL AS config_json UNION ALL
    SELECT @abos_form_id, 'email', 'Email', 'email', 1, 2, NULL UNION ALL
    SELECT @abos_form_id, 'company', 'Company', 'text', 1, 3, NULL UNION ALL
    SELECT @abos_form_id, 'phone', 'Phone', 'text', 0, 4, NULL UNION ALL
    SELECT @abos_form_id, 'team_size', 'Team size', 'select', 1, 5, '{"options": ["1-10", "11-50", "51-200", "200+"]}' UNION ALL
    SELECT @abos_form_id, 'modules_of_interest', 'Which modules interest you? (CRM, Finance, HR, Projects, Procurement, Real Estate, Legal, Communication, Web/CMS)', 'textarea', 0, 6, NULL UNION ALL
    SELECT @abos_form_id, 'current_systems', 'What are you using today (spreadsheets, other software)?', 'textarea', 0, 7, NULL
) seed
WHERE @abos_form_id IS NOT NULL AND NOT EXISTS (
    SELECT 1 FROM dim_web_form_fields WHERE form_id = @abos_form_id
);
