<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}
class WaicInstallerDbUpdater {
	public static function runUpdate( $current_version ) {
		if ($current_version && version_compare($current_version, '1.1.1', '<')) {
			WaicDb::query( "ALTER TABLE `@__tasks` CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci");
			WaicDb::query( "ALTER TABLE `@__posts_create` CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci");
		}
		
		if ( WaicDb::get( "SELECT 1 FROM `@__modules` WHERE code='postsfields'", 'one' ) != 1 ) {
			WaicDb::query( "INSERT INTO `@__modules` (id, code, active, type_id, label) VALUES (NULL, 'postsfields', 1, 1, 'PostsFields');" );
		}
		if ( WaicDb::get( "SELECT 1 FROM `@__modules` WHERE code='chatbots'", 'one' ) != 1 ) {
			WaicDb::query( "INSERT INTO `@__modules` (id, code, active, type_id, label) VALUES (NULL, 'chatbots', 1, 1, 'Chatbots');" );
		}
		if ( WaicDb::get( "SELECT 1 FROM `@__modules` WHERE code='promo'", 'one' ) != 1 ) {
			WaicDb::query( "INSERT INTO `@__modules` (id, code, active, type_id, label) VALUES (NULL, 'promo', 1, 1, 'Promo');" );
		}
		if ( WaicDb::get( "SELECT 1 FROM `@__modules` WHERE code='magictext'", 'one' ) != 1 ) {
			WaicDb::query( "INSERT INTO `@__modules` (id, code, active, type_id, label) VALUES (NULL, 'magictext', 1, 1, 'Magictext');" );
		}
		if ( WaicDb::get( "SELECT 1 FROM `@__modules` WHERE code='mcp'", 'one' ) != 1 ) {
			WaicDb::query( "INSERT INTO `@__modules` (id, code, active, type_id, label) VALUES (NULL, 'mcp', 1, 1, 'MCP');" );
		}
		if ( WaicDb::get( "SELECT 1 FROM `@__modules` WHERE code='forms'", 'one' ) != 1 ) {
			WaicDb::query( "INSERT INTO `@__modules` (id, code, active, type_id, label) VALUES (NULL, 'forms', 1, 1, 'Forms');" );
		}
		if ( WaicDb::get( "SELECT 1 FROM `@__modules` WHERE code='workflow'", 'one' ) != 1 ) {
			WaicDb::query( "INSERT INTO `@__modules` (id, code, active, type_id, label) VALUES (NULL, 'workflow', 1, 1, 'Workflow');" );
		}
		if ( ! WaicDb::existsTableColumn( '@__workspace', 'flag' ) ) {
			WaicDb::query( 'ALTER TABLE `@__workspace` ADD COLUMN `flag` INT NOT NULL DEFAULT 0 AFTER `value`' );
			WaicDb::query( "ALTER TABLE `@__workspace` ADD COLUMN `timeout` INT NOT NULL DEFAULT 0 AFTER `flag`" );
		}
		if ( WaicDb::get( "SELECT 1 FROM `@__workspace` WHERE name='flow'", 'one' ) != 1 ) {
			WaicDb::query( "INSERT INTO `@__workspace` (id, name, value, flag, timeout) VALUES (11, 'flow', 0, 0, 0);" );
		}
		if ( WaicDb::get( "SELECT 1 FROM `@__workspace` WHERE name='launch_bot'", 'one' ) != 1 ) {
			WaicDb::query( "INSERT INTO `@__workspace` (id, name, value, flag, timeout) VALUES (21, 'launch_bot', 0, 0, 0);" );
		}
		if ( ! WaicDb::existsTableColumn( '@__tasks', 'cycle' ) ) {
			WaicDb::query( 'ALTER TABLE `@__tasks` ADD COLUMN `cycle` INT NOT NULL DEFAULT 0 AFTER `steps`' );
			WaicDb::query( "ALTER TABLE `@__tasks` ADD COLUMN `message` VARCHAR(250) DEFAULT '' AFTER `cycle`" );
		}
		if ( ! WaicDb::existsTableColumn( '@__tasks', 'title' ) ) {
			WaicDb::query( "ALTER TABLE `@__tasks` ADD COLUMN `title` VARCHAR(250) DEFAULT '' AFTER `author`" );
		}
		if ( ! WaicDb::existsTableColumn( '@__tasks', 'tokens' ) ) {
			WaicDb::query( "ALTER TABLE `@__tasks` ADD COLUMN `tokens` BIGINT NOT NULL DEFAULT 0 AFTER `message`" );
		}
		if ( ! WaicDb::existsTableColumn( '@__tasks', 'mode' ) ) {
			WaicDb::query( "ALTER TABLE `@__tasks` ADD COLUMN `mode` VARCHAR(24) DEFAULT '' AFTER `tokens`" );
			WaicDb::query( "ALTER TABLE `@__tasks` ADD COLUMN `obj_id` BIGINT NOT NULL DEFAULT 0 AFTER `mode`" );
		}
		if ( ! WaicDb::existsTableColumn( '@__tasks', 'recalc' ) ) {
			WaicDb::query( "ALTER TABLE `@__tasks` ADD COLUMN `recalc` TINYINT(1) NOT NULL DEFAULT 0 AFTER `status`" );
		}
		
		if ( ! WaicDb::existsTableColumn( '@__posts_create', 'added' ) ) {
			WaicDb::query( 'ALTER TABLE `@__posts_create` ADD COLUMN `added` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP AFTER `post_id`' );
			WaicDb::query( "ALTER TABLE `@__posts_create` ADD COLUMN `uniq` VARCHAR(32) NULL AFTER `added`" );
		}
		if ( ! WaicDb::existsTableColumn( '@__history', 'feature' ) ) {
			WaicDb::query( "ALTER TABLE `@__history` ADD COLUMN `feature` VARCHAR(24) NOT NULL AFTER `task_id`" );
		}
		if ( ! WaicDb::existsTableColumn( '@__history', 'engine' ) ) {
			WaicDb::query( "ALTER TABLE `@__history` ADD COLUMN `engine` VARCHAR(20) DEFAULT '' AFTER `ip`" );
		}
		if ( WaicDb::get( "SELECT 1 FROM `@__tasks` WHERE feature='magictext'", 'one' ) != 1 ) {
			WaicDb::query( "INSERT INTO `@__tasks` (id, feature, title, author, status) VALUES (NULL, 'magictext', 'Magic Text', 0, 4);");
		}
		if ( ! WaicDb::existsTableColumn( '@__chatlogs', 'status' ) ) {
			WaicDb::query( "ALTER TABLE `@__chatlogs` ADD COLUMN `status` TINYINT(1) NOT NULL DEFAULT 0 AFTER `file`" );
		}
		
		if ( WaicDb::get( "SELECT 1 FROM `@__tasks` WHERE feature='template'", 'one' ) != 1 ) {
			$json = '{"nodes":[{"id":"1","type":"trigger","position":{"x":350,"y":200},"data":{"dragged":true,"type":"trigger","category":"wp","error":false,"code":"wp_user_register","label":"New user registered","settings":{"login":"","name":"","email":"","role":"","capability":""}}},{"id":"2","type":"logic","position":{"x":535,"y":200},"data":{"dragged":true,"type":"logic","category":"un","code":"un_branch","label":"Branch","settings":{"name":"IF","criteria":"{{node#1.display_name}}","operator":"is_known"},"error":false}},{"id":"3","type":"action","position":{"x":719.11372505269,"y":99.933325895769},"data":{"dragged":true,"type":"action","category":"wp","error":false,"code":"wp_send_email","label":"Send Email","settings":{"to":"{{node#1.user_email}}","from":"max@aiwuplugin.com","from_name":"Max from AIWU","subject":"Welcome to our WordPress community!","body":"Hey {{node#1.display_name}}!\\nThanks for joining! We’re really happy to have you on board.\\nYour account is ready — you can log in anytime and start exploring.\\n\\nIf you have any questions, just reply to this email — we’re here to help.\\nSee you around!\\n\\n— The AIWU Team"}}},{"id":"4","type":"action","position":{"x":724.9120303178,"y":286.61233136753},"data":{"dragged":true,"type":"action","category":"wp","error":false,"code":"wp_send_email","label":"Send Email","settings":{"to":"{{node#1.user_email}}","from":"max@aiwuplugin.com","from_name":"Max form AIWU","subject":"Welcome to our WordPress community!","body":"Hey there!\\nThanks for joining! We’re really happy to have you on board.\\nYour account is ready — you can log in anytime and start exploring.\\n\\nIf you have any questions, just reply to this email — we’re here to help.\\nSee you around!\\n\\n— The AIWU Team"}}}],"edges":[{"id":"1","source":"1","target":"2","sourceHandle":"output-right","targetHandle":"input-left","type":"default"},{"id":"2","source":"2","target":"3","sourceHandle":"output-then","targetHandle":"input-left","type":"default"},{"id":"3","source":"2","target":"4","sourceHandle":"output-else","targetHandle":"input-left","type":"default"}],"viewport":{"x":-70.95266505528,"y":-38.855946890096,"zoom":1.1892071068141},"settings":"","version":"1.0.0"}';
			WaicDb::query( "INSERT INTO `@__tasks` (id, feature, title, mode, message, params) VALUES (NULL, 'template', 'Personalized Welcome Emails', 1, 'Automatically send personalized welcome emails to new users when they register on your WordPress site.','" . addslashes($json) . "');");
			$json = '{"nodes":[{"id":"1","type":"trigger","position":{"x":350,"y":200},"data":{"dragged":true,"type":"trigger","category":"sy","code":"sy_manual","label":"Manually","settings":[],"error":false}},{"id":"2","type":"logic","position":{"x":535,"y":200},"data":{"dragged":true,"type":"logic","category":"lp","error":false,"code":"lp_posts","label":"Search Posts","settings":{"name":"","ids":"","title":"","body":"","date_mode":"","categories":"","tags":"","status":"publish","author":""}}},{"id":"3","type":"logic","position":{"x":709,"y":134},"data":{"dragged":true,"type":"logic","category":"un","code":"un_branch","label":"Branch","settings":{"name":"IF","criteria":"{{node#2.post_excerpt}}","operator":"is_unknown"},"error":false}},{"id":"4","type":"action","position":{"x":890,"y":46},"data":{"dragged":true,"type":"action","category":"ai","error":false,"code":"ai_generate_text","label":"Generate Text","settings":{"name":"Open AI - Generate Text","model":"gpt-4o","tokens":"4096","temperature":"0.7","prompt":"Write a short excerpt up to 130 character based on the post title: {{node#2.post_title}}"}}},{"id":"5","type":"action","position":{"x":1090,"y":45.253725775965},"data":{"dragged":true,"type":"action","category":"wp","error":false,"code":"wp_update_post","label":"Update Post","settings":{"id":"{{node#2.post_ID}}","title":"","body":"","excerpt":"{{node#4.content}}","status":"","author":""}}},{"id":"6","type":"action","position":{"x":726.50732078504,"y":322.88980344863},"data":{"dragged":true,"type":"action","category":"wp","error":false,"code":"wp_send_email","label":"Send Email","settings":{"to":"max@aiwuplugin.com","from":"max@aiwuplugin.com","from_name":"Max","subject":"Workflow completed","body":"Hi Max\\n\\nThe meta description generation workflow is complete - please check it out.\\n\\nThanks"}}}],"edges":[{"id":"1","source":"1","target":"2","sourceHandle":"output-right","targetHandle":"input-left","type":"default"},{"id":"2","source":"2","target":"3","sourceHandle":"output-then","targetHandle":"input-left","type":"default"},{"id":"3","source":"3","target":"4","sourceHandle":"output-then","targetHandle":"input-left","type":"default"},{"id":"4","source":"4","target":"5","sourceHandle":"output-right","targetHandle":"input-left","type":"default"},{"id":"5","source":"2","target":"6","sourceHandle":"output-else","targetHandle":"input-left","type":"default"}],"viewport":{"x":176.23629455824,"y":70.813761773201,"zoom":0.76962215630794},"settings":"","version":"1.0.0"}';
			WaicDb::query( "INSERT INTO `@__tasks` (id, feature, title, mode, message, params) VALUES (NULL, 'template', 'Generate Missing Post Excerpts', 2, 'This workflow scans your content, identifies posts without excerpts, and uses AI to create engaging 150-character summaries based on post titles.','" . addslashes($json) . "');");
			$json = '{"nodes":[{"id":"1","type":"trigger","position":{"x":350,"y":200},"data":{"dragged":true,"type":"trigger","category":"sy","code":"sy_manual","label":"Manually","settings":[],"error":false}},{"id":"2","type":"logic","position":{"x":535,"y":200},"data":{"dragged":true,"type":"logic","category":"lp","error":false,"code":"lp_posts","label":"Search Posts","settings":{"name":"","ids":"","title":"","body":"","date_mode":"","categories":"","tags":"","status":"publish","author":""}}},{"id":"3","type":"logic","position":{"x":714.93187267115,"y":123},"data":{"dragged":true,"type":"logic","category":"un","code":"un_branch","label":"Branch","settings":{"name":"IF","criteria":"{{node#2.post_image}}","operator":"is_unknown"},"error":false}},{"id":"4","type":"action","position":{"x":896,"y":69.666666666667},"data":{"dragged":true,"type":"action","category":"ai","error":false,"code":"ai_generate_image","label":"Open AI Generate Image","settings":{"name":"Generate Image","model":"dall-e-3","orientation":"horizontal","prompt":"Create a high-quality featured image for the article that visually interprets its central themes and ideas, based on the provided details: \\n\\n- Article Title: {{node#2.post_title}}\\n\\nThe image should embody the main concepts and mood of the article without including any text, ensuring it complements the content effectively. This visual representation should enhance the article appeal and provide deeper insight into its themes."}}},{"id":"6","type":"action","position":{"x":1269.3333333333,"y":68.333333333333},"data":{"dragged":true,"type":"action","category":"wp","error":false,"code":"wp_update_post_image","label":"Update Post Featured image","settings":{"id":"{{node#2.post_ID}}","image":"{{node#4.image_id}}","alt":"{{node#8.content}}"}}},{"id":"8","type":"action","position":{"x":1089.3333333333,"y":69},"data":{"dragged":true,"type":"action","category":"ai","error":false,"code":"ai_generate_text","label":"Open AI Generate Text","settings":{"name":"Generate Alt Text","model":"gpt-4o","tokens":"300","temperature":0.7,"prompt":"We created an image for an article about the topic &quot;{{node#2.post_title}}&quot;. \\n\\nPlease generate an Alt text for this image. \\nThe Alt text must be between 10-70 characters long. Your response should contain only the Alt text, with no additional text or explanations."}}}],"edges":[{"id":"1","source":"1","target":"2","sourceHandle":"output-right","targetHandle":"input-left","type":"default"},{"id":"2","source":"2","target":"3","sourceHandle":"output-then","targetHandle":"input-left","type":"default"},{"id":"3","source":"3","target":"4","sourceHandle":"output-then","targetHandle":"input-left","type":"default"},{"id":"6","source":"4","target":"8","sourceHandle":"output-right","targetHandle":"input-left","type":"default"},{"id":"7","source":"8","target":"6","sourceHandle":"output-right","targetHandle":"input-left","type":"default"}],"viewport":{"x":-961.82371544285,"y":65.956273933069,"zoom":1.5},"settings":"","version":"1.0.0"}';
			WaicDb::query( "INSERT INTO `@__tasks` (id, feature, title, mode, message, params) VALUES (NULL, 'template', 'Generate Missing Featured Images', 3, 'Automatically generate AI-powered featured images and alt text for all posts missing visuals. Perfect for bulk content optimization.','" . addslashes($json) . "');");
			$json = '{"nodes":[{"id":"1","type":"trigger","position":{"x":332,"y":198},"data":{"dragged":true,"type":"trigger","category":"wc","error":false,"code":"wc_new_order","label":"New Order Created","settings":{"status":"","total":"","customer":"","products":"","categories":"","tags":""}}},{"id":"7","type":"logic","position":{"x":628.33333333333,"y":196},"data":{"dragged":true,"type":"logic","category":"un","code":"un_branch","label":"Branch","settings":{"name":"is not completed","criteria":"{{node#1.order_status}}","operator":"does_not_equal","value":"Completed","compare":"text"},"error":false}},{"id":"8","type":"logic","position":{"x":491,"y":198.66666666667},"data":{"dragged":true,"type":"logic","category":"un","code":"un_delay","label":"Delay","settings":{"mode":"amount","days":"","hours":"1","minutes":"0"},"error":false}},{"id":"9","type":"action","position":{"x":782,"y":126.66666666667},"data":{"dragged":true,"type":"action","category":"wp","error":false,"code":"wp_send_email","label":"Send Email","settings":{"to":"{{node#1.user_email}}","from":"max@aiwuplugin.com","from_name":"Max from AIWU","subject":"You left something in your cart 🛒","body":"Hey {{node#1.display_name}}! 👋\\n\\nWe noticed you didn&#039;t finish your order. Your cart is waiting for you:\\n\\n{{node#1.order_products}}\\n\\nTotal: {{node#1.order_total}}\\n\\nThese items are popular and might sell out soon! \\n\\n👉 Complete your order now:\\nhttps://aiwuplugin.com/cart\\n\\nQuestions? Just hit reply – we&#039;re here to help!\\n\\nCheers,\\nMax"}}},{"id":"10","type":"logic","position":{"x":922.91879759531,"y":125.17125070133},"data":{"dragged":true,"type":"logic","category":"un","code":"un_delay","label":"Delay","settings":{"mode":"amount","days":"1","hours":"0","minutes":"0"},"error":false}},{"id":"12","type":"logic","position":{"x":1092.5480638794,"y":124.5582176804},"data":{"dragged":true,"type":"logic","category":"un","code":"un_branch","label":"Branch","settings":{"name":"Still Pending Payment","criteria":"","operator":"equals","value":"","compare":"text"},"error":false}},{"id":"18","type":"action","position":{"x":1257.6187652117,"y":26.537738448272},"data":{"dragged":true,"type":"action","category":"wp","error":false,"code":"wp_send_email","label":"Send Email","settings":{"to":"{{node#1.user_email}}","from":"max@aiwuplugin.com","from_name":"Max from AIWU","subject":"Reminder: You left something in your cart 🛒","body":"Hi {{node#1.display_name}}, still thinking about it? 🤔\\n\\nYour cart is still here, and honestly – we really think you&#039;ll love these items.\\n\\nSo here&#039;s a little nudge: 20% OFF just for you.\\n\\n{{node#1.order_products}}\\n\\nTotal: {{node#1.order_total}}\\n\\n💰 Use code: AIWU20 at checkout\\n\\nThis deal expires in 24 hours, so don&#039;t wait too long!\\n\\nComplete your order:\\nhttps://aiwuplugin.com/cart\\n\\nAny questions? Hit reply.\\n\\nMax\\nAIWU Team"}}}],"edges":[{"id":"6","source":"1","target":"8","sourceHandle":"output-right","targetHandle":"input-left","type":"default"},{"id":"7","source":"8","target":"7","sourceHandle":"output-right","targetHandle":"input-left","type":"default"},{"id":"8","source":"7","target":"9","sourceHandle":"output-then","targetHandle":"input-left","type":"default"},{"id":"9","source":"9","target":"10","sourceHandle":"output-right","targetHandle":"input-left","type":"default"},{"id":"10","source":"10","target":"12","sourceHandle":"output-right","targetHandle":"input-left","type":"default"},{"id":"11","source":"12","target":"18","sourceHandle":"output-then","targetHandle":"input-left","type":"default"}],"viewport":{"x":-311.49041249201,"y":8.5837701174823,"zoom":1.25},"settings":"","version":"1.0.0"}';
			WaicDb::query( "INSERT INTO `@__tasks` (id, feature, title, mode, message, params) VALUES (NULL, 'template', 'Abandoned Cart Reminder Emails', 4, 'Recover lost sales automatically! Send timed cart reminders with discount offers to customers who abandon checkout.','" . addslashes($json) . "');");
			$json = '{"nodes":[{"id":"1","type":"trigger","position":{"x":350,"y":200},"data":{"dragged":true,"type":"trigger","category":"wc","error":false,"code":"wc_new_order","label":"New Order Created","settings":{"status":["completed"],"total":"","customer":"first","products":"","categories":"","tags":""}}},{"id":"3","type":"action","position":{"x":530,"y":200},"data":{"dragged":true,"type":"action","category":"wp","error":false,"code":"wp_send_email","label":"Send Email","settings":{"to":"{{node#1.user_email}}","from":"max@aiwuplugin.com","from_name":"Max from AIWU","subject":"Thank you for your first order! 🎉","body":"Hey {{node#1.billing_first_name}} 👋\\n\\nWe&#039;re absolutely thrilled to have you as a customer! Your order has been confirmed and is on its way.\\n\\n🎁 As a thank you for choosing us, here&#039;s a special gift: use code WELCOME15 on your next purchase for 15% off!\\n\\n📦 Order Details:\\n{{node#1.order_ID}}\\n\\nBest Regards,\\nAdmin"}}}],"edges":[{"id":"2","source":"1","target":"3","sourceHandle":"output-right","targetHandle":"input-left","type":"default"}],"viewport":{"x":-99.940278981879,"y":-73.739280322291,"zoom":1.5},"settings":"","version":"1.0.0"}';
			WaicDb::query( "INSERT INTO `@__tasks` (id, feature, title, mode, message, params) VALUES (NULL, 'template', 'Thank You Email – First Time Customer', 5, 'Automate thank you emails for first-time WooCommerce customers. Include order details and welcome discount to boost repeat purchases.','" . addslashes($json) . "');");
		}
	}
}
