<?php
defined('ABSPATH') || die("Nice try");


register_activation_hook(VFORM_PLUGIN_FILE, function(){
	global $wpdb;

	$table_name = $wpdb->prefix . 'vform';
	$charset_collate = $wpdb->get_charset_collate();

	$sql = "CREATE TABLE $table_name (
		id INT NOT NULL AUTO_INCREMENT,
		formname VARCHAR(200) NOT NULL,
		formdescription VARCHAR(1000) NOT NULL,
		formbody LONGTEXT NOT NULL,
		status VARCHAR(50) NOT NULL,
		confirmation VARCHAR(200) DEFAULT NULL,
		confirmation_value MEDIUMTEXT DEFAULT NULL,
		notification_mode VARCHAR(100) DEFAULT NULL,
		send_to VARCHAR(200) DEFAULT NULL,
		email_subject VARCHAR(500) DEFAULT NULL,
		from_name VARCHAR(200) DEFAULT NULL,
		from_email VARCHAR(200) DEFAULT NULL,
		reply_to VARCHAR(200) DEFAULT NULL,
		message LONGTEXT DEFAULT NULL,
		security_type VARCHAR(50) DEFAULT NULL,
		rec_site_key VARCHAR(300) DEFAULT NULL,
		rec_secret_key VARCHAR(300) DEFAULT NULL,
		hcap_site_key VARCHAR(300) DEFAULT NULL,
		hcap_secret_key VARCHAR(300) DEFAULT NULL,
		no_duplicate VARCHAR(50) DEFAULT NULL,
		google_sheet MEDIUMTEXT DEFAULT NULL,
		google_sheet_desti VARCHAR(100) DEFAULT NULL,
		webhook_url MEDIUMTEXT DEFAULT NULL,
		webhook_auth VARCHAR(100) DEFAULT NULL,
		webhook_auth_type VARCHAR(300) DEFAULT NULL,
		webhook_auth_key VARCHAR(300) DEFAULT NULL,
		webhook_auth_secret VARCHAR(300) DEFAULT NULL,
		applicode VARCHAR(100) DEFAULT 'BR',
		citizcode VARCHAR(100) DEFAULT 'US',
		applinumber INT DEFAULT 1,
		datesubmit TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
		PRIMARY KEY  (id)
	) $charset_collate;";



	// notification
	$table_namenotifi = $wpdb->prefix . 'vform_notifications';
	$charset_collate_notifi = $wpdb->get_charset_collate();

	$sql_noti = "CREATE TABLE $table_namenotifi (
		id INT(11) NOT NULL AUTO_INCREMENT,
		formid INT NOT NULL,
		action_name VARCHAR(200) NOT NULL,
		send_to_email VARCHAR(2000) NOT NULL,
		from_name VARCHAR(50) NOT NULL,
		from_email VARCHAR(200) DEFAULT NULL,
		subject VARCHAR(400) DEFAULT NULL,
		message MEDIUMTEXT DEFAULT NULL,
		reply_to VARCHAR(200) DEFAULT NULL,
		mode VARCHAR(200) DEFAULT NULL,
		dropdown VARCHAR(50) DEFAULT NULL,
		attachment_ids VARCHAR(300) DEFAULT NULL,
		created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
		PRIMARY KEY (id)
	) $charset_collate_notifi;";



  // secondtable

  $table_name2 = $wpdb->prefix . 'vform_userinput';
	$charset_collate2 = $wpdb->get_charset_collate();

	$sql2 = "CREATE TABLE $table_name2 (
		id INT(11) NOT NULL AUTO_INCREMENT,
		formid VARCHAR(100) NOT NULL,
		maindatabody MEDIUMTEXT NOT NULL,
		ip VARCHAR(100) DEFAULT NULL,
		browser VARCHAR(200) DEFAULT NULL,
		currentdate VARCHAR(100) DEFAULT NULL,
		timezone VARCHAR(100) DEFAULT NULL,
		currentdate_part2 VARCHAR(100) DEFAULT NULL,
		usertimetakes VARCHAR(100) DEFAULT NULL,
		application_id VARCHAR(100) DEFAULT NULL,
		access_token VARCHAR(100) DEFAULT NULL,
		viewed TINYINT(1) DEFAULT 0,
		newlead TINYINT(1) DEFAULT 0,
		PRIMARY KEY (id)
	) $charset_collate2;";


	// thirdtable

	$table_name3 = $wpdb->prefix . 'vfsubscr';
	$charset_collate3 = $wpdb->get_charset_collate();

	$sql3 = "CREATE TABLE $table_name3 (
		id int(11) NOT NULL AUTO_INCREMENT,
		subscription int(11) NOT NULL,
        template_key varchar(300) DEFAULT NULL, 
        emailid varchar(300) DEFAULT NULL, 
		PRIMARY KEY  (id)
	) $charset_collate3;";



	// forthtable

	$table_name4 = $wpdb->prefix . 'vform_fields';
	$charset_collate4 = $wpdb->get_charset_collate();

	$sql4 = "CREATE TABLE $table_name4 (
		id int(11) NOT NULL AUTO_INCREMENT,
		formid int(11) NOT NULL,
		ip varchar(200) NULL,
		type enum('view','start') DEFAULT 'view',
		created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
		PRIMARY KEY  (id)
	) $charset_collate4;";

	// conditional form logic
	$table_name5 = $wpdb->prefix . 'vform_conditional_logic';
    $charset_collate5 = $wpdb->get_charset_collate();

    $sql5 = "CREATE TABLE $table_name5 (
        id BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
        logic_name VARCHAR(255) NOT NULL,
        conditions TEXT NOT NULL,
        hidden_fields TEXT NOT NULL,
		form_id VARCHAR(100) NOT NULL,
        PRIMARY KEY (id)
    ) $charset_collate5;";
	// conditional form logic

	// smtp email
	$table_name6 = $wpdb->prefix . 'vform_email_accounts';
    $charset_collate6 = $wpdb->get_charset_collate();

	$sql6 = "CREATE TABLE $table_name6 (
		id BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
		account_type ENUM('smtp', 'api') NOT NULL DEFAULT 'smtp',
		service_name VARCHAR(100) DEFAULT NULL,
		smtp_host VARCHAR(255) DEFAULT NULL,
		smtp_port INT DEFAULT NULL,
		smtp_username VARCHAR(255) DEFAULT NULL,
		smtp_password VARCHAR(255) DEFAULT NULL,
		smtp_encryption ENUM('tls', 'ssl', 'none') DEFAULT NULL,	
		api_key TEXT DEFAULT NULL,
		api_domain VARCHAR(255) DEFAULT NULL,	
		from_email VARCHAR(255) NOT NULL,
		from_name VARCHAR(255) DEFAULT NULL,	
		is_active TINYINT(1) DEFAULT 1,
		created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
		updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
		PRIMARY KEY (id)
	) $charset_collate6;";
	// stmp email

	// automation
	$table_name7 = $wpdb->prefix . 'vform_automations';
    $charset_collate7 = $wpdb->get_charset_collate();

	$sql7 = "CREATE TABLE $table_name7 (
		id INT(11) NOT NULL AUTO_INCREMENT,
        form_id INT(11) NOT NULL,
        sequence_name VARCHAR(255) NOT NULL,
		user_email VARCHAR(300) NOT NULL,
        emails LONGTEXT NOT NULL,
        created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
        PRIMARY KEY (id)
	) $charset_collate7;";
	// automation


	require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
  	dbDelta( $sql );
	dbDelta( $sql_noti );
	dbDelta( $sql2 );
	dbDelta( $sql3 );
	dbDelta( $sql4 );
	dbDelta( $sql5 );
	dbDelta( $sql6 );
	dbDelta( $sql7 );
});

