<?php

/**
 * Fired during plugin activation
 *
 * @link       https://registrationformbuilder.com
 * @since      1.0.0
 *
 * @package    Vedrixa_Forms
 * @subpackage Vedrixa_Forms/includes
 */

/**
 * Fired during plugin activation.
 *
 * This class defines all code necessary to run during the plugin's activation.
 *
 * @since      1.0.0
 * @package    Vedrixa_Forms
 * @subpackage Vedrixa_Forms/includes
 * @author     Vedrixa <support@registrationformbuilder.com>
 */
class Registration_Form_Builder_Activator {

	/**
	 * Short Description. (use period)
	 *
	 * Long Description.
	 *
	 * @since    1.0.0
	 */
    public function activate( $network_wide = false ) {
        global $wpdb;

        if ( is_multisite() && $network_wide ) {
            // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching
            $blog_ids = $wpdb->get_col( "SELECT blog_id FROM $wpdb->blogs" );
            foreach ( $blog_ids as $blog_id ) {
                switch_to_blog( $blog_id );
                $this->create_table();
                restore_current_blog();
            }
        } else {
            $this->create_table();
        }

    }

    public function create_table() {
        global $wpdb;
        if ( version_compare( get_bloginfo( 'version' ), '6.1' ) < 0 ) {
                require_once ABSPATH . 'wp-includes/wp-db.php';
        } else {
                require_once ABSPATH . 'wp-includes/class-wpdb.php';
        }
        require_once ABSPATH . 'wp-admin/includes/upgrade.php';
        $charset_collate = $wpdb->get_charset_collate();
        $table_name      = $this->get_db_table_name( 'FORMS' );
        $sql             = "CREATE TABLE IF NOT EXISTS $table_name (
        `id` int(11) NOT NULL AUTO_INCREMENT,
        `name` varchar(255) NOT NULL,
        `desc` longtext DEFAULT NULL,
        `type` int(11) NOT NULL DEFAULT '1',
        `user_role` varchar(255) DEFAULT NULL,
        `redirect` varchar(255) NOT NULL,
        `redirect_to` varchar(255) DEFAULT NULL,
        `meta` longtext,
        `form_options` longtext,
        PRIMARY KEY (`id`)
        )$charset_collate;";
        $result          = dbDelta( $sql );
        if ( is_array( $result ) && !empty( $result ) ) {
            foreach ( $result as $db_message ) {
                if ( stripos( $db_message, 'Error' ) !== false ) {
                    break;
                }
            }
        }

        $table_name = $this->get_db_table_name( 'PAYPAL_LOG' );
        $sql        = "CREATE TABLE IF NOT EXISTS $table_name (
        `id` int(11) NOT NULL AUTO_INCREMENT,
        `txn_id` varchar(600) NOT NULL,
        `log` longtext NOT NULL,
        `posted_date` datetime NOT NULL,
        `form_id` int(11) NOT NULL,
        `status` varchar(255) NOT NULL,
        `invoice` varchar(255) NOT NULL,
        `amount` int(11) NOT NULL,
        `currency` varchar(255) NOT NULL,
        `pay_processor` varchar(255) NOT NULL,
        `pay_type` varchar(255) NOT NULL,
        `uid` int(11) NOT NULL,
        `meta` longtext DEFAULT NULL,
        PRIMARY KEY (`id`))$charset_collate;";
        dbDelta( $sql );

        $table_name = $this->get_db_table_name( 'EMAIL_TMPL' );
        $sql        = "CREATE TABLE IF NOT EXISTS $table_name (
        `id` int(11) NOT NULL AUTO_INCREMENT,
        `tmpl_name` varchar(600) NOT NULL,
        `email_subject` varchar(255) NOT NULL,
        `email_body` longtext DEFAULT NULL,
        PRIMARY KEY (`id`))$charset_collate;";
        dbDelta( $sql );

        $table_name = $this->get_db_table_name( 'SUBMISSIONS' );
        $sql        = "CREATE TABLE IF NOT EXISTS $table_name (
        `id` int(11) NOT NULL AUTO_INCREMENT,
        `form_id` int(11) NOT NULL,
        `email` varchar(255) NOT NULL,
        `data` longtext DEFAULT NULL,
        `submitted_on` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
        `unique_token` varchar(250) DEFAULT NULL,
        PRIMARY KEY (`id`))$charset_collate;";
		dbDelta( $sql );
    }

    public function get_db_table_name( $identifier ) {
        global $wpdb;
        $plugin_prefix = $wpdb->prefix . 'efb_';

        switch ( $identifier ) {
            case 'FORMS':
                $table_name = $plugin_prefix . 'forms';
                break;
            case 'WP_OPTION':
                $table_name = $wpdb->prefix . 'options';
                break;

            case 'PAYPAL_LOG':
                $table_name = $plugin_prefix . 'paypal_log';
                break;

            case 'EMAIL_TMPL':
                $table_name = $plugin_prefix . 'email_templates';
                break;
            case 'SUBMISSIONS':
                $table_name = $plugin_prefix . 'submissions';
                break;
            default:
                $classname = "WPEFB_Helper_$identifier";
                if ( class_exists( $classname ) ) {
                    $externalclass = new $classname();
                    $table_name    = $externalclass->get_db_table_name( $identifier );
                } else {
					return false; }
        }
        return $table_name;
    }


    public function get_db_table_unique_field_name( $identifier ) {

        switch ( $identifier ) {
            case 'FORMS':
                $unique_field_name = 'id';
                break;
            case 'WP_OPTION':
                $unique_field_name = 'option_id';
                break;

            case 'PAYPAL_LOG':
                $unique_field_name = 'id';
                break;
            case 'EMAIL_TMPL':
                $unique_field_name = 'id';
                break;

            case 'SUBMISSIONS':
                $unique_field_name = 'id';
                break;
            default:
                $classname = "WPEFB_Helper_$identifier";
                if ( class_exists( $classname ) ) {
                    $externalclass         = new $classname();
                        $unique_field_name = $externalclass->get_db_table_unique_field_name( $identifier );
                } else {
					return false; }
        }
        return $unique_field_name;
    }

    public function get_db_table_field_type( $identifier, $field ) {
        $functionname = 'get_field_format_type_' . $identifier;
        if ( method_exists( 'Registration_Form_Builder_Activator', $functionname ) ) {
            $format = $this->$functionname( $field );
        } else {
            $classname = "WPEFB_Helper_$identifier";
            if ( class_exists( $classname ) ) {
                $externalclass = new $classname();
                $format        = $externalclass->get_db_table_field_type( $identifier, $field );
            } else {
                return false;
            }
        }
        return $format;
    }



    // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.MethodNameInvalid
    public function get_field_format_type_EMAIL_TMPL( $field ) {
        switch ( $field ) {
            case 'id':
                $format = '%d';
                break;
            default:
                $format = '%s';
        }
        return $format;
    }

    // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.MethodNameInvalid
    public function get_field_format_type_PAYPAL_LOG( $field ) {
        switch ( $field ) {
            case 'id':
                $format = '%d';
                break;
            case 'form_id':
                $format = '%d';
                break;
            case 'amount':
                $format = '%d';
                break;
            case 'uid':
                $format = '%d';
                break;
            default:
                $format = '%s';
		}
        return $format;
    }

    // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.MethodNameInvalid
    public function get_field_format_type_FORMS( $field ) {
        switch ( $field ) {
            case 'id':
                $format = '%d';
                break;
            case 'type':
                $format = '%d';
                break;
            default:
                $format = '%s';
        }
        return $format;
    }

    // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.MethodNameInvalid
    public function get_field_format_type_SUBMISSIONS( $field ) {
        switch ( $field ) {
            case 'id':
                $format = '%d';
                break;
            case 'form_id':
                $format = '%d';
                break;
            default:
                $format = '%s';
        }
        return $format;
    }


}
