-- =============================================================================
-- fix_nsp_foundation_leads_not_deals.sql — route NSP Foundation's "connect" form
-- submissions to a CRM Lead instead of an Opportunity/Deal.
--
-- seed_nsp_foundation_business_and_site.sql originally seeded a
-- 'web_service_request' dim_deal_templates row for NSP's tenant, so every form
-- submission was creating a fact_opportunities row via
-- CrmDealFromWebService.createDealFromFormSubmission. PublicWebService.submitForm
-- now falls back to CrmLeadFromWebService.createLeadFromFormSubmission (creating a
-- fact_leads row) whenever that method finds no matching deal template for the
-- tenant — so removing NSP's template row here is what switches its form
-- submissions over to Lead capture, matching how a nonprofit inquiry should be
-- tracked (qualify first, not "Deal" with a dollar value).
--
-- Same NAME-based business resolution as the other NSP migration files (see
-- seed_nsp_foundation_business_and_site.sql's header for why).
--
-- Idempotent: DELETE on a natural key, safe to re-run (no-op the second time).
-- Deploy via apply-migrations.sh (default DB_NAME=stacie_Aggie_v1.0).
-- =============================================================================

SET @business_id := (SELECT id FROM businesses WHERE name = 'NSP Foundation' AND is_deleted = 0 ORDER BY id LIMIT 1);
SET @tenant_id := (SELECT tenant_id FROM businesses WHERE id = @business_id LIMIT 1);

DELETE FROM dim_deal_templates
WHERE tenant_id = @tenant_id AND template_key = 'web_service_request';
