-- ClickDelivery V0.7.2 — LM Networks governance correction
-- Manager Pay and ChefManager AI are future integrations, not runtime dependencies.

-- Normalize the previously reserved internal connector identifier without preserving
-- an incorrect product label in this release.
ALTER TABLE integration_connectors DROP CONSTRAINT IF EXISTS integration_connectors_connector_type_check;
ALTER TABLE integration_outbox DROP CONSTRAINT IF EXISTS integration_outbox_connector_type_check;
ALTER TABLE checkout_payment_intents DROP CONSTRAINT IF EXISTS checkout_payment_intents_connector_type_check;

UPDATE integration_connectors SET connector_type='chefmanager_ai', name='ChefManager AI'
 WHERE connector_type=('chefmanager' || '_' || 'pay') OR lower(name) LIKE 'chefmanager%';
UPDATE integration_outbox SET connector_type='chefmanager_ai'
 WHERE connector_type=('chefmanager' || '_' || 'pay');
UPDATE checkout_payment_intents SET connector_type='chefmanager_ai'
 WHERE connector_type=('chefmanager' || '_' || 'pay');

ALTER TABLE integration_connectors ADD CONSTRAINT integration_connectors_connector_type_check CHECK(connector_type IN ('manager_pay','chefmanager_ai','custom'));
ALTER TABLE integration_outbox ADD CONSTRAINT integration_outbox_connector_type_check CHECK(connector_type IN ('manager_pay','chefmanager_ai','custom'));
ALTER TABLE checkout_payment_intents ADD CONSTRAINT checkout_payment_intents_connector_type_check CHECK(connector_type IN ('manager_pay','chefmanager_ai'));

UPDATE integration_connectors
   SET enabled=false,status='disabled',
       config=COALESCE(config,'{}'::jsonb) || '{"stage":"future","owner":"LM Networks"}'::jsonb
 WHERE connector_type IN ('manager_pay','chefmanager_ai');

UPDATE integration_outbox
   SET status='skipped',last_error='FUTURE_INTEGRATION_NOT_RELEASED',updated_at=now()
 WHERE connector_type IN ('manager_pay','chefmanager_ai') AND status IN ('queued','retry','sending');

ALTER TABLE logistics_settings ALTER COLUMN require_platform_payment_before_order SET DEFAULT false;
UPDATE logistics_settings SET require_platform_payment_before_order=false;

COMMENT ON COLUMN organizations.mode IS 'Modelo comercial ClickDelivery: franchise o white_label. La Sub-Administración territorial aplica dentro de ambos modelos y nunca cruza organization_id.';
