
-- ---------------------------------------------------------------------------
-- pjStripe plugin - integration migration
-- Adds the Stripe options, the payment-method entry and extends the
-- bookings payment_method enum. Mirrors the pjPaypal update style.
-- ---------------------------------------------------------------------------

START TRANSACTION;

-- 1) Extend the payment_method enums so 'stripe' can be stored ----------------
ALTER TABLE `bookings` MODIFY COLUMN `payment_method`
	enum('paypal','authorize','stripe','creditcard','cash','bank') DEFAULT NULL;
ALTER TABLE `bookings_payments` MODIFY COLUMN `payment_method`
	enum('paypal','authorize','stripe','creditcard','bank','cash') DEFAULT NULL;

-- 2) Idempotency: drop previous rows if the migration is re-run --------------
DELETE FROM `options` WHERE `key` IN ('o_allow_stripe', 'o_stripe_public_key', 'o_stripe_private_key');
DELETE FROM `multi_lang` WHERE `model` = 'pjField' AND `field` = 'title' AND `foreign_id` IN (
	SELECT `id` FROM `fields` WHERE `key` IN (
		'opt_o_allow_stripe', 'opt_o_stripe_public_key', 'opt_o_stripe_private_key', 'payment_methods_ARRAY_stripe'
	)
);
DELETE FROM `fields` WHERE `key` IN (
	'opt_o_allow_stripe', 'opt_o_stripe_public_key', 'opt_o_stripe_private_key', 'payment_methods_ARRAY_stripe'
);

-- 3) Options (Payments tab = 2), slotted after the bank option (23) -----------
INSERT INTO `options` (`foreign_id`, `key`, `tab_id`, `value`, `label`, `type`, `order`, `is_visible`, `style`) VALUES
(1, 'o_allow_stripe', 2, 'Yes|No::No', 'Yes|No', 'enum', 24, 1, NULL),
(1, 'o_stripe_public_key', 2, '', NULL, 'string', 25, 1, NULL),
(1, 'o_stripe_private_key', 2, '', NULL, 'string', 26, 1, NULL);

-- 4) Admin labels for those options ------------------------------------------
INSERT INTO `fields` (`id`, `key`, `type`, `label`, `source`, `modified`) VALUES
(NULL, 'opt_o_allow_stripe', 'backend', 'Plugin / Stripe / Allow payments with Stripe', 'plugin', NOW());
SET @id := (SELECT LAST_INSERT_ID());
INSERT INTO `multi_lang` (`id`, `foreign_id`, `model`, `locale`, `field`, `content`, `source`) VALUES
(NULL, @id, 'pjField', '::LOCALE::', 'title', 'Allow Stripe payments', 'plugin');

INSERT INTO `fields` (`id`, `key`, `type`, `label`, `source`, `modified`) VALUES
(NULL, 'opt_o_stripe_public_key', 'backend', 'Plugin / Stripe / Publishable key', 'plugin', NOW());
SET @id := (SELECT LAST_INSERT_ID());
INSERT INTO `multi_lang` (`id`, `foreign_id`, `model`, `locale`, `field`, `content`, `source`) VALUES
(NULL, @id, 'pjField', '::LOCALE::', 'title', 'Stripe publishable key', 'plugin');

INSERT INTO `fields` (`id`, `key`, `type`, `label`, `source`, `modified`) VALUES
(NULL, 'opt_o_stripe_private_key', 'backend', 'Plugin / Stripe / Secret key', 'plugin', NOW());
SET @id := (SELECT LAST_INSERT_ID());
INSERT INTO `multi_lang` (`id`, `foreign_id`, `model`, `locale`, `field`, `content`, `source`) VALUES
(NULL, @id, 'pjField', '::LOCALE::', 'title', 'Stripe secret key', 'plugin');

-- 5) Register 'stripe' as a selectable payment method ------------------------
INSERT INTO `fields` (`id`, `key`, `type`, `label`, `source`, `modified`) VALUES
(NULL, 'payment_methods_ARRAY_stripe', 'arrays', 'payment_methods_ARRAY_stripe', 'plugin', NOW());
SET @id := (SELECT LAST_INSERT_ID());
INSERT INTO `multi_lang` (`id`, `foreign_id`, `model`, `locale`, `field`, `content`, `source`) VALUES
(NULL, @id, 'pjField', '::LOCALE::', 'title', 'Stripe', 'plugin');

COMMIT;
