START TRANSACTION;

INSERT INTO `tblplan_features` (`name`, `codename`, `description`, `type`, `added_on`, `is_highlight`) VALUES ('Wisetack Consumer Financing', 'wisetack_consumer_financing_allowed', 'Offer financing to your customers.', 'allow', NOW(), 1);
SET @allow_request_changes_id = LAST_INSERT_ID();
INSERT INTO `tblplan_features_value` (`plans_id`, `plans_features_id`, `value`, `other_data`, `added_on`) VALUES ('9', @allow_request_changes_id, '0', '{"outside_message":"Wisetack Consumer Financing is a pro feature. In order to enable this, Upgrade to PRO.","inside_message":"Wisetack Consumer Financing is a pro feature. In order to enable this, Upgrade to PRO","tag_color":"#eb0e0e","tag_title":"UPGRADE","link":"https://my.contractorplus.app/","count":"0"}', UTC_TIMESTAMP());
INSERT INTO `tblplan_features_value` (`plans_id`, `plans_features_id`, `value`, `added_on`) VALUES ('10', @allow_request_changes_id, '1', UTC_TIMESTAMP());
INSERT INTO `tblplan_features_value` (`plans_id`, `plans_features_id`, `value`, `added_on`) VALUES ('11', @allow_request_changes_id, '1',  UTC_TIMESTAMP());
INSERT INTO `tblplan_features_value` (`plans_id`, `plans_features_id`, `value`, `added_on`) VALUES ('12', @allow_request_changes_id, '1', UTC_TIMESTAMP());
INSERT INTO `tblplan_features_value` (`plans_id`, `plans_features_id`, `value`, `other_data`, `added_on`) VALUES ('13', @allow_request_changes_id, '0', '{"outside_message":"Wisetack Consumer Financing is a pro feature. In order to enable this, Upgrade to PRO.","inside_message":"Wisetack Consumer Financing is a pro feature. In order to enable this, Upgrade to PRO","tag_color":"#eb0e0e","tag_title":"UPGRADE","link":"https://my.contractorplus.app/","count":"0"}', UTC_TIMESTAMP());
INSERT INTO `tblplan_features_value` (`plans_id`, `plans_features_id`, `value`, `added_on`) VALUES ('14', @allow_request_changes_id, '1', UTC_TIMESTAMP());
INSERT INTO `tblplan_features_value` (`plans_id`, `plans_features_id`, `value`, `added_on`) VALUES ('15', @allow_request_changes_id, '1', UTC_TIMESTAMP());

UPDATE tblstaff SET refresh_permissions = 1;

CREATE TABLE `tblwisetack_transactions` (
  `id` int(50) UNSIGNED NOT NULL,
  `tenant_id` int(50) NOT NULL,
  `rel_id` int(50) DEFAULT NULL,
  `rel_type` varchar(100) DEFAULT NULL,
  `client_id` int(50) DEFAULT NULL,
  `transaction_id` varchar(255) DEFAULT NULL,
  `payment_link` varchar(255) DEFAULT NULL,
  `init_token` varchar(100) DEFAULT NULL,
  `requested_amount` decimal(15,2) DEFAULT NULL,
  `approved_amount` decimal(15,2) DEFAULT NULL,
  `max_eligible_amount` decimal(15,2) DEFAULT NULL,
  `loan_status` varchar(200) DEFAULT NULL,
  `created_at` datetime NOT NULL DEFAULT current_timestamp(),
  `updated_at` datetime NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
ALTER TABLE `tblwisetack_transactions`
  ADD PRIMARY KEY (`id`),
  ADD KEY `transaction_id` (`transaction_id`),
  ADD KEY `rel_id` (`rel_id`),
  ADD KEY `loan_status` (`loan_status`),
  ADD KEY `client_id` (`client_id`);
  
ALTER TABLE `tblwisetack_transactions`
  MODIFY `id` int(50) UNSIGNED NOT NULL AUTO_INCREMENT;

ALTER TABLE `tblwisetack_transactions` ADD INDEX(`tenant_id`);
ALTER TABLE `tblwisetack_transactions` ADD INDEX(`rel_id`);
  
ALTER TABLE `tblwisetack_transactions` ADD `processing_fee` DECIMAL(10,2) NULL AFTER `max_eligible_amount`;

ALTER TABLE `tblinvoice_payment_refunds` CHANGE `mode` `mode` ENUM('wepay','tilled','manual','wisetack') CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL;

COMMIT;
