
-- ---------------------------------------------------------------------------
-- pjStripe plugin - options migration (CGYachtListing: two payment surfaces)
--   * o_stripe_* keys are used by BOTH the listing-payment surface and the
--     front-booking surface (single Stripe account).
--   * o_allow_stripe + payment method label drive the booking checkout.
--   * the enum ALTERs add 'stripe' to the bookings tables.
-- ---------------------------------------------------------------------------

START TRANSACTION;

-- Idempotency ----------------------------------------------------------------
DELETE FROM `options` WHERE `key` IN ('o_stripe_public_key', 'o_stripe_secret_key', 'o_stripe_cancel_url', 'o_allow_stripe');
DELETE FROM `multi_lang` WHERE `model` = 'pjField' AND `field` = 'title' AND `foreign_id` IN (
	SELECT `id` FROM `fields` WHERE `key` IN (
		'opt_o_stripe_public_key', 'opt_o_stripe_secret_key', 'opt_o_stripe_cancel_url', 'opt_o_allow_stripe',
		'plugin_stripe_payment_title', 'plugin_stripe_redirecting',
		'payment_methods_ARRAY_stripe', 'payment_plugin_messages_ARRAY_stripe'
	)
);
DELETE FROM `fields` WHERE `key` IN (
	'opt_o_stripe_public_key', 'opt_o_stripe_secret_key', 'opt_o_stripe_cancel_url', 'opt_o_allow_stripe',
	'plugin_stripe_payment_title', 'plugin_stripe_redirecting',
	'payment_methods_ARRAY_stripe', 'payment_plugin_messages_ARRAY_stripe'
);

-- Make room: move the offline methods below, so the Stripe gateway group sits
-- together right after the Authorize.net block (mirrors the PayPal/Authorize groups).
UPDATE `options` SET `order` = 24 WHERE `tab_id` = 3 AND `key` = 'o_allow_cash';
UPDATE `options` SET `order` = 25 WHERE `tab_id` = 3 AND `key` = 'o_allow_creditcard';
UPDATE `options` SET `order` = 26 WHERE `tab_id` = 3 AND `key` = 'o_allow_bank';
UPDATE `options` SET `order` = 27 WHERE `tab_id` = 3 AND `key` = 'o_bank_account';

-- Stripe gateway group (contiguous, tab_id 3). cancel_url is optional/hidden.
INSERT INTO `options` (`foreign_id`, `key`, `tab_id`, `value`, `label`, `type`, `order`, `is_visible`, `style`) VALUES
(1, 'o_allow_stripe', 3, 'Yes|No::No', 'Yes|No', 'enum', 19, 1, NULL),
(1, 'o_stripe_public_key', 3, '', NULL, 'string', 20, 1, NULL),
(1, 'o_stripe_secret_key', 3, '', NULL, 'string', 21, 1, NULL),
(1, 'o_stripe_cancel_url', 3, '', NULL, 'string', 22, 0, NULL);

-- Admin labels ---------------------------------------------------------------
INSERT INTO `fields` VALUES (NULL, 'opt_o_allow_stripe', 'backend', 'Plugin / Stripe / Allow payments with Stripe', 'plugin', NULL);
SET @id := (SELECT LAST_INSERT_ID());
INSERT INTO `multi_lang` VALUES (NULL, @id, 'pjField', '1', 'title', 'Allow Stripe payments', 'plugin'),
(NULL, @id, 'pjField', '2', 'title', 'Allow Stripe payments', 'plugin'),
(NULL, @id, 'pjField', '3', 'title', 'Allow Stripe payments', 'plugin');

INSERT INTO `fields` VALUES (NULL, 'opt_o_stripe_public_key', 'backend', 'Plugin / Stripe / Publishable key', 'plugin', NULL);
SET @id := (SELECT LAST_INSERT_ID());
INSERT INTO `multi_lang` VALUES (NULL, @id, 'pjField', '1', 'title', 'Stripe publishable key', 'plugin'),
(NULL, @id, 'pjField', '2', 'title', 'Stripe publishable key', 'plugin'),
(NULL, @id, 'pjField', '3', 'title', 'Stripe publishable key', 'plugin');

INSERT INTO `fields` VALUES (NULL, 'opt_o_stripe_secret_key', 'backend', 'Plugin / Stripe / Secret key', 'plugin', NULL);
SET @id := (SELECT LAST_INSERT_ID());
INSERT INTO `multi_lang` VALUES (NULL, @id, 'pjField', '1', 'title', 'Stripe secret key', 'plugin'),
(NULL, @id, 'pjField', '2', 'title', 'Stripe secret key', 'plugin'),
(NULL, @id, 'pjField', '3', 'title', 'Stripe secret key', 'plugin');

INSERT INTO `fields` VALUES (NULL, 'opt_o_stripe_cancel_url', 'backend', 'Plugin / Stripe / Cancel page', 'plugin', NULL);
SET @id := (SELECT LAST_INSERT_ID());
INSERT INTO `multi_lang` VALUES (NULL, @id, 'pjField', '1', 'title', 'Cancel page', 'plugin'),
(NULL, @id, 'pjField', '2', 'title', 'Cancel page', 'plugin'),
(NULL, @id, 'pjField', '3', 'title', 'Cancel page', 'plugin');

-- UI labels ------------------------------------------------------------------
INSERT INTO `fields` VALUES (NULL, 'plugin_stripe_payment_title', 'backend', 'Plugin / Stripe / Stripe payment', 'plugin', NULL);
SET @id := (SELECT LAST_INSERT_ID());
INSERT INTO `multi_lang` VALUES (NULL, @id, 'pjField', '1', 'title', 'Stripe payment', 'plugin'),
(NULL, @id, 'pjField', '2', 'title', 'Stripe payment', 'plugin'),
(NULL, @id, 'pjField', '3', 'title', 'Stripe payment', 'plugin');

INSERT INTO `fields` VALUES (NULL, 'plugin_stripe_redirecting', 'backend', 'Plugin / Stripe / Redirecting', 'plugin', NULL);
SET @id := (SELECT LAST_INSERT_ID());
INSERT INTO `multi_lang` VALUES (NULL, @id, 'pjField', '1', 'title', 'Redirecting to Stripe...', 'plugin'),
(NULL, @id, 'pjField', '2', 'title', 'Redirecting to Stripe...', 'plugin'),
(NULL, @id, 'pjField', '3', 'title', 'Redirecting to Stripe...', 'plugin');

-- Booking payment-method label (drives the front checkout dropdown) + message -
INSERT INTO `fields` VALUES (NULL, 'payment_methods_ARRAY_stripe', 'arrays', 'payment_methods_ARRAY_stripe', 'plugin', NULL);
SET @id := (SELECT LAST_INSERT_ID());
INSERT INTO `multi_lang` VALUES (NULL, @id, 'pjField', '1', 'title', 'Stripe', 'plugin'),
(NULL, @id, 'pjField', '2', 'title', 'Stripe', 'plugin'),
(NULL, @id, 'pjField', '3', 'title', 'Stripe', 'plugin');

INSERT INTO `fields` VALUES (NULL, 'payment_plugin_messages_ARRAY_stripe', 'arrays', 'payment_plugin_messages_ARRAY_stripe', 'plugin', NULL);
SET @id := (SELECT LAST_INSERT_ID());
INSERT INTO `multi_lang` VALUES (NULL, @id, 'pjField', '1', 'title', 'Your booking is saved. Redirecting to Stripe Checkout...', 'plugin'),
(NULL, @id, 'pjField', '2', 'title', 'Your booking is saved. Redirecting to Stripe Checkout...', 'plugin'),
(NULL, @id, 'pjField', '3', 'title', 'Your booking is saved. Redirecting to Stripe Checkout...', 'plugin');

-- Add 'stripe' to the bookings payment_method enums (prefix applied on install)
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;

COMMIT;
