<?php
/**
 * 2007-2023 PrestaShop
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Academic Free License (AFL 3.0)
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://opensource.org/licenses/afl-3.0.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@prestashop.com so we can send you a copy immediately.
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade PrestaShop to newer
 * versions in the future. If you wish to customize PrestaShop for your
 * needs please refer to http://www.prestashop.com for more information.
 *
 *  @author    PrestaShop SA <contact@prestashop.com>
 *  @copyright 2007-2023 PrestaShop SA
 *  @license   http://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.0)
 *  International Registered Trademark & Property of PrestaShop SA
 */
declare(strict_types=1);

namespace PrestaShop\Module\AgileMultisellerPro\Sql;

if (!defined('_PS_VERSION_')) {
    exit;
}

class SqlInstaller
{
    private $sql = [];
    private $usql = [];
    private $errors = [];

    public function __construct()
    {
        $collation_database = \Db::getInstance()->getValue('SELECT @@collation_database');

        $this->sql['sellerinfo'] = 'CREATE TABLE IF NOT EXISTS `' . _DB_PREFIX_ . 'sellerinfo` (
            `id_sellerinfo` bigint(11) AUTO_INCREMENT NOT NULL,
            `company` varchar(255) NULL,
            `id_country` int(10) unsigned NOT NULL,
            `id_state` int(10) unsigned DEFAULT NULL,
            `address1` varchar(128) DEFAULT NULL,
            `address2` varchar(128) DEFAULT NULL,
            `postcode` varchar(12) DEFAULT NULL,
            `city` varchar(64) DEFAULT NULL,
            `description` text,
            `phone` varchar(16) DEFAULT NULL,
            `fax` varchar(16) DEFAULT NULL,
            `latitude` double DEFAULT NULL,
            `longitude` double DEFAULT NULL,
            `active` TINYINT(1) NOT NULL DEFAULT 0,
            `date_add` datetime NOT NULL,
            `date_upd` datetime NOT NULL,
            `id_customer` bigint(11) NULL,
            `id_manufacturer` bigint(11) NULL,
            `id_supplier` bigint(11) NULL,
            `theme_name` varchar(255) NULL,
            `payment_collection` tinyint(1) NULL,
            `service_zipcodes` varchar(2000) NULL,
            `service_distance` float NULL,
            `dni` varchar(128) NULL,
            `id_shop` bigint(11) NULL,
            `id_category_default` bigint(11) NULL,
            `id_sellertype1` bigint(11) NULL,
            `id_sellertype2` bigint(11) NULL,
            `ams_custom_number1` float NULL,
            `ams_custom_number2` float NULL,
            `ams_custom_number3` float NULL,
            `ams_custom_number4` float NULL,
            `ams_custom_number5` float NULL,
            `ams_custom_number6` float NULL,
            `ams_custom_number7` float NULL,
            `ams_custom_number8` float NULL,
            `ams_custom_number9` float NULL,
            `ams_custom_number10` float NULL,
            `ams_custom_date1` date NULL,
            `ams_custom_date2` date NULL,
            `ams_custom_date3` date NULL,
            `ams_custom_date4` date NULL,
            `ams_custom_date5` date NULL,
            `ams_custom_string1` varchar(255) NULL,
            `ams_custom_string2` varchar(255) NULL,
            `ams_custom_string3` varchar(255) NULL,
            `ams_custom_string4` varchar(255) NULL,
            `ams_custom_string5` varchar(255) NULL,
            `ams_custom_string6` varchar(255) NULL,
            `ams_custom_string7` varchar(255) NULL,
            `ams_custom_string8` varchar(255) NULL,
            `ams_custom_string9` varchar(255) NULL,
            `ams_custom_string10` varchar(255) NULL,
            `ams_custom_string11` varchar(255) NULL,
            `ams_custom_string12` varchar(255) NULL,
            `ams_custom_string13` varchar(255) NULL,
            `ams_custom_string14` varchar(255) NULL,
            `ams_custom_string15` varchar(255) NULL,
            `ams_custom_text1` text NULL,
            `ams_custom_text2` text NULL,
            `ams_custom_text3` text NULL,
            `ams_custom_text4` text NULL,
            `ams_custom_text5` text NULL,
            `ams_custom_text6` text NULL,
            `ams_custom_text7` text NULL,
            `ams_custom_text8` text NULL,
            `ams_custom_text9` text NULL,
            `ams_custom_text10` text NULL,
            `ams_custom_html1` text NULL,
            `ams_custom_html2` text NULL,
            PRIMARY KEY (`id_sellerinfo`),
            KEY `id_country` (`id_country`),
            KEY `id_state` (`id_state`)
            ) COLLATE=\'' . $collation_database . '\' ENGINE=' . _MYSQL_ENGINE_ . ';';

        $this->usql['sellerinfo'] = 'DROP TABLE IF EXISTS `' . _DB_PREFIX_ . 'sellerinfo`;';

        $this->sql['sellerinfo_lang'] = 'CREATE TABLE IF NOT EXISTS `' . _DB_PREFIX_ . 'sellerinfo_lang` (
            `id_sellerinfo_lang` int(10) AUTO_INCREMENT NOT NULL,
            `id_sellerinfo` bigint(11),
            `id_lang` int(10) NULL,
            `company` varchar(255) NULL,
            `description` text,
            `address1` varchar(128) NULL,
            `address2` varchar(128) NULL,
            `city` varchar(64) NULL,
            `ams_custom_text1` text NULL,
            `ams_custom_text2` text NULL,
            `ams_custom_text3` text NULL,
            `ams_custom_text4` text NULL,
            `ams_custom_text5` text NULL,
            `ams_custom_text6` text NULL,
            `ams_custom_text7` text NULL,
            `ams_custom_text8` text NULL,
            `ams_custom_text9` text NULL,
            `ams_custom_text10` text NULL,
            `ams_custom_html1` text NULL,
            `ams_custom_html2` text NULL,
            PRIMARY KEY (`id_sellerinfo_lang`)
          ) COLLATE=\'' . $collation_database . '\' ENGINE=' . _MYSQL_ENGINE_ . ';';

        $this->usql['sellerinfo_lang'] = 'DROP TABLE IF EXISTS `' . _DB_PREFIX_ . 'sellerinfo_lang`;';

        $this->setCreateSellerType($collation_database);
        $this->setCreateSellerCategories($collation_database);
        $this->setCreateSellerOrders($collation_database);
        $this->setCreateSellerProduct($collation_database);
        $this->setCreateSellerShipping($collation_database);
        $this->setCreateSellerDelivery($collation_database);

        $this->setCreateComissions($collation_database);
        $this->setCreateSellerRange($collation_database);
        $this->setCreateSellerCartCarrier($collation_database);
        $this->setCreateLocation($collation_database);
        $this->setCreateMembership($collation_database);

        $this->setCreateRating($collation_database);

        $this->setCreateSellerAttachment($collation_database);
        $this->setCreateSellerOrderDetailInfo($collation_database);
        $this->setCreateImportMatch($collation_database);
    }

    protected function setCreateSellerPayment($collation_database)
    {
        // "agile_seller_paymentinfo" now this "sellerinfo_payment"
        // id_agile_seller_paymentinfo => id_sellerinfo_payment
        // id_seller => id_sellerinfo
        $this->sql['sellerinfo_payment'] = 'CREATE TABLE IF NOT EXISTS `' . _DB_PREFIX_ . 'sellerinfo_payment` (
                `id_sellerinfo_payment` bigint(11) AUTO_INCREMENT NOT NULL,
                `id_sellerinfo` bigint(11) NOT NULL,
                `module_name` varchar(255) NULL,
                `id_currency` int(10) NULL,
                `info1` varchar(255) NULL,
                `info2` varchar(255) NULL,
                `info3` varchar(255) NULL,
                `info4` varchar(255) NULL,
                `info5` varchar(255) NULL,
                `info6` varchar(255) NULL,
                `info7` varchar(255) NULL,
                `info8` varchar(255) NULL,
                `in_use` tinyint(1) DEFAULT 1,
                `date_add` datetime NOT NULL,
                PRIMARY KEY (`id_sellerinfo_payment`)
            ) COLLATE=\'' . $collation_database . '\' ENGINE=' . _MYSQL_ENGINE_ . ';';

        $this->usql['sellerinfo_payment'] = 'DROP TABLE IF EXISTS `' . _DB_PREFIX_ . 'sellerinfo_payment`;';
    }

    protected function setCreateSellerType($collation_database)
    {
        $this->sql['sellertype'] = 'CREATE TABLE IF NOT EXISTS `' . _DB_PREFIX_ . 'sellertype` (
                `id_sellertype` int(10) unsigned NOT NULL auto_increment,
                `date_add` datetime NOT NULL,
                PRIMARY KEY (`id_sellertype`)
            ) COLLATE=\'' . $collation_database . '\' ENGINE=' . _MYSQL_ENGINE_ . ';';

        $this->sql['sellertype_lang'] = 'CREATE TABLE IF NOT EXISTS `' . _DB_PREFIX_ . 'sellertype_lang` (
                `id_sellertype_lang` int(10) unsigned NOT NULL auto_increment,
                `id_sellertype` int(10),
                `id_lang` int(10) NOT NULL,
                `name` varchar(255) NOT NULL,
                PRIMARY KEY (`id_sellertype_lang`)
            ) COLLATE=\'' . $collation_database . '\' ENGINE=' . _MYSQL_ENGINE_ . ';';

        $this->usql['sellertype'] = 'DROP TABLE IF EXISTS `' . _DB_PREFIX_ . 'sellertype`;';
        $this->usql['sellertype_lang'] = 'DROP TABLE IF EXISTS `' . _DB_PREFIX_ . 'sellertype_lang`;';
    }

    protected function setCreateSellerCategories($collation_database)
    {
        // "category_owner" now this "sellerinfo_category"
        $this->sql['sellerinfo_category'] = 'CREATE TABLE IF NOT EXISTS `' . _DB_PREFIX_ . 'sellerinfo_category` (
                `id_sellerinfo` bigint(11) NOT NULL,
                `id_category` bigint(11) NOT NULL,
                PRIMARY KEY (`id_sellerinfo`, `id_category`)
            ) COLLATE=\'' . $collation_database . '\' ENGINE=' . _MYSQL_ENGINE_ . ';';

        $this->usql['sellerinfo_category'] = 'DROP TABLE IF EXISTS `' . _DB_PREFIX_ . 'sellerinfo_category`;';
    }

    protected function setCreateSellerOrders($collation_database)
    {
        // "order_owner" now this "sellerinfo_order"
        $this->sql['sellerinfo_order'] = 'CREATE TABLE IF NOT EXISTS `' . _DB_PREFIX_ . 'sellerinfo_order` (
                `id_sellerinfo_order` bigint(11) AUTO_INCREMENT NOT NULL,
                `id_sellerinfo` bigint(11) NOT NULL,
                `id_order` bigint(11) NOT NULL,
                `date_add` datetime NOT NULL,
                PRIMARY KEY (`id_sellerinfo_order`)
            ) COLLATE=\'' . $collation_database . '\' ENGINE=' . _MYSQL_ENGINE_ . ';';

        $this->usql['sellerinfo_order'] = 'DROP TABLE IF EXISTS `' . _DB_PREFIX_ . 'sellerinfo_order`;';
    }

    protected function setCreateSellerProduct($collation_database)
    {
        // "product_owner" now this "sellerinfo_product"
        $this->sql['sellerinfo_product'] = 'CREATE TABLE IF NOT EXISTS `' . _DB_PREFIX_ . 'sellerinfo_product` (
                `id_sellerinfo_product` bigint(11) AUTO_INCREMENT NOT NULL,
                `id_sellerinfo` bigint(11) NOT NULL,
                `id_product` bigint(11) NOT NULL,
                `approved` tinyint(1) DEFAULT 0,
                `date_add` datetime NOT NULL,
                PRIMARY KEY (`id_sellerinfo_product`)
            ) COLLATE=\'' . $collation_database . '\' ENGINE=' . _MYSQL_ENGINE_ . ';';

        $this->usql['sellerinfo_product'] = 'DROP TABLE IF EXISTS `' . _DB_PREFIX_ . 'sellerinfo_product`;';
    }

    protected function setCreateSellerShipping($collation_database)
    {
        $this->sql['sellerinfo_shipping'] = 'CREATE TABLE IF NOT EXISTS `' . _DB_PREFIX_ . 'sellerinfo_shipping` (
                `id_sellerinfo_shipping` bigint(11) AUTO_INCREMENT NOT NULL,
                `id_sellerinfo` bigint(11) NOT NULL,
                `id_carrier_reference` bigint(11) NOT NULL,
                `active` TINYINT(1) NOT NULL DEFAULT 1,
                `is_free` TINYINT(1) NOT NULL,
                `id_shipping_method` TINYINT(1) NOT NULL,
                PRIMARY KEY (`id_sellerinfo_shipping`)
            ) COLLATE=\'' . $collation_database . '\' ENGINE=' . _MYSQL_ENGINE_ . ';';

        $this->usql['sellerinfo_shipping'] = 'DROP TABLE IF EXISTS `' . _DB_PREFIX_ . 'sellerinfo_shipping`;';
    }

    protected function setCreateSellerDelivery($collation_database)
    {
        $this->sql['sellerinfo_delivery'] = 'CREATE TABLE IF NOT EXISTS `' . _DB_PREFIX_ . 'sellerinfo_delivery` (
                `id_sellerinfo_delivery` bigint(11) AUTO_INCREMENT NOT NULL,
                `id_sellerinfo` bigint(11) NOT NULL,
                `id_carrier_reference` bigint(11) NOT NULL,
                `id_shipping_method` TINYINT(1) NOT NULL,
                `id_zone` bigint(11) NOT NULL,
                `id_sellerinfo_range` bigint(11),
                `price` DECIMAL(20, 6) NOT NULL,
                PRIMARY KEY (`id_sellerinfo_delivery`),
                INDEX idx_id_sellerinfo (`id_sellerinfo`),
                INDEX idx_id_carrier_reference (`id_carrier_reference`),
                INDEX idx_id_shipping_method (`id_shipping_method`)
            ) COLLATE=\'' . $collation_database . '\' ENGINE=' . _MYSQL_ENGINE_ . ';';

        $this->usql['sellerinfo_delivery'] = 'DROP TABLE IF EXISTS `' . _DB_PREFIX_ . 'sellerinfo_delivery`;';
    }

    protected function setCreateComissions($collation_database)
    {
        $this->sql['sellerinfo_commission_base'] = 'CREATE TABLE IF NOT EXISTS `' . _DB_PREFIX_ . 'sellerinfo_commission_base` (
                `id_commission_base` bigint(11) AUTO_INCREMENT NOT NULL,
                `id_sellerinfo` bigint(11) NULL,
                `base_commission` float (16,2) NULL,
                `date_add` datetime NOT NULL,
                PRIMARY KEY (`id_commission_base`)
            ) COLLATE=\'' . $collation_database . '\' ENGINE=' . _MYSQL_ENGINE_ . ';';

        $this->sql['sellerinfo_commission_rate'] = 'CREATE TABLE IF NOT EXISTS `' . _DB_PREFIX_ . 'sellerinfo_commission_rate` (
                `id_commission_rate` bigint(11) AUTO_INCREMENT NOT NULL,
                `id_sellerinfo` bigint(11) NULL,
                `from_amount` float (16,2) NULL,
                `to_amount` float (16,2) NULL,
                `commission` float (16,2) NULL,
                `deduction_rate` float (16,2) NULL,
                `date_add` datetime NOT NULL,
                PRIMARY KEY (`id_commission_rate`)
            ) COLLATE=\'' . $collation_database . '\' ENGINE=' . _MYSQL_ENGINE_ . ';';

        $this->sql['sellerinfo_commission'] = 'CREATE TABLE IF NOT EXISTS `' . _DB_PREFIX_ . 'sellerinfo_commission` (
                `id_commission` bigint(11) AUTO_INCREMENT NOT NULL,
                `id_sellerinfo` bigint(11) NOT NULL,
                `id_order` bigint(11) NOT NULL,
                `id_currency` bigint(11) NOT NULL,
                `sales_amount` float (16,2) NULL,
                `base_commission` float (16,2) NULL,
                `range_commission` float (16,2) NULL,
                `seller_sales` float (16,2) NULL,
                `record_balance` float (16,2) NULL,
                `payment_txn_id` varchar(256) NULL,
                `record_type` int(10) NULL,
                `balance` float (16,2) NULL,
                `memo` varchar(2000) NULL,
                `deduction_amount` float (16,2) NULL,
                `date_add` datetime NOT NULL,
                PRIMARY KEY (`id_commission`)
            ) COLLATE=\'' . $collation_database . '\' ENGINE=' . _MYSQL_ENGINE_ . ';';

        $this->usql['sellerinfo_commission_base'] = 'DROP TABLE IF EXISTS `' . _DB_PREFIX_ . 'sellerinfo_commission_base`;';
        $this->usql['sellerinfo_commission_rate'] = 'DROP TABLE IF EXISTS `' . _DB_PREFIX_ . 'sellerinfo_commission_rate`;';
        $this->usql['sellerinfo_commission'] = 'DROP TABLE IF EXISTS `' . _DB_PREFIX_ . 'sellerinfo_commission`;';
    }

    protected function setCreateSellerRange($collation_database)
    {
        $this->sql['sellerinfo_range'] = 'CREATE TABLE IF NOT EXISTS `' . _DB_PREFIX_ . 'sellerinfo_range` (
                `id_sellerinfo_range` bigint(11) AUTO_INCREMENT NOT NULL,
                `id_sellerinfo` bigint(11) NOT NULL,
                `id_carrier_reference` bigint(11) NOT NULL,
                `id_shipping_method` TINYINT(1) NOT NULL,
                `delimiter1` DECIMAL(20, 6) NOT NULL,
                `delimiter2` DECIMAL(20, 6) NOT NULL,
                PRIMARY KEY (`id_sellerinfo_range`)
            ) COLLATE=\'' . $collation_database . '\' ENGINE=' . _MYSQL_ENGINE_ . ';';

        $this->usql['sellerinfo_range'] = 'DROP TABLE IF EXISTS `' . _DB_PREFIX_ . 'sellerinfo_range`;';
    }

    protected function setCreateSellerCartCarrier($collation_database)
    {
        $this->sql['sellerinfo_cart_carrier'] = 'CREATE TABLE IF NOT EXISTS `' . _DB_PREFIX_ . 'sellerinfo_cart_carrier` (
                `id_sellerinfo_cart_carrier` bigint(11) AUTO_INCREMENT NOT NULL,
                `id_sellerinfo` bigint(11) NOT NULL,
                `id_carrier_reference` bigint(11) NOT NULL,
                `id_cart` bigint(11) NOT NULL,
                `id_location_carrier` bigint(11) NULL,
                `weight` DECIMAL(20, 6) NULL,
                `shipping_cost` DECIMAL(20, 6) NULL,
                `tracking_number` varchar(255) NULL,
                PRIMARY KEY (`id_sellerinfo_cart_carrier`)
            ) COLLATE=\'' . $collation_database . '\' ENGINE=' . _MYSQL_ENGINE_ . ';';

        $this->usql['sellerinfo_cart_carrier'] = 'DROP TABLE IF EXISTS `' . _DB_PREFIX_ . 'sellerinfo_cart_carrier`;';
    }

    protected function setCreateLocation($collation_database)
    {
        $this->sql['sellerinfo_location_carrier'] = 'CREATE TABLE IF NOT EXISTS `' . _DB_PREFIX_ . 'sellerinfo_location_carrier` (
                `id_location_carrier` bigint(11) unsigned AUTO_INCREMENT NOT NULL,
                `id_sellerinfo` bigint(11) NOT NULL,
                `id_carrier_reference` int(11) unsigned NOT NULL,
                `email` varchar(255) NULL,
                `location` varchar(255) NULL,
                `id_country` int(10) unsigned NOT NULL,
                `id_state` int(10) unsigned DEFAULT NULL,
                `address1` varchar(128) NOT NULL,
                `address2` varchar(128) DEFAULT NULL,
                `postcode` varchar(12) DEFAULT NULL,
                `city` varchar(64) NOT NULL,
                `phone` varchar(16) DEFAULT NULL,
                `description` text,
                `latitude` double DEFAULT NULL,
                `longitude` double DEFAULT NULL,
                `active` tinyint(1) unsigned NOT NULL DEFAULT 1,
                `deleted` tinyint(1) unsigned NOT NULL DEFAULT 0,
                `date_add` datetime NOT NULL,
                `date_upd` datetime NOT NULL,
                PRIMARY KEY (`id_location_carrier`)
            ) COLLATE=\'' . $collation_database . '\' ENGINE=' . _MYSQL_ENGINE_ . ';';

        $this->sql['sellerinfo_location_cart'] = 'CREATE TABLE IF NOT EXISTS `' . _DB_PREFIX_ . 'sellerinfo_location_cart` (
                `id_location_cart` bigint(11) unsigned AUTO_INCREMENT NOT NULL,
                `id_cart` int(11) unsigned NOT NULL,
                `id_location_carrier` bigint(11) unsigned NOT NULL,
                `date_add` datetime NOT NULL,
                PRIMARY KEY (`id_location_cart`)
            ) COLLATE=\'' . $collation_database . '\' ENGINE=' . _MYSQL_ENGINE_ . ';';

        $this->usql['sellerinfo_location_carrier'] = 'DROP TABLE IF EXISTS `' . _DB_PREFIX_ . 'sellerinfo_location_carrier`;';
        $this->usql['sellerinfo_location_cart'] = 'DROP TABLE IF EXISTS `' . _DB_PREFIX_ . 'sellerinfo_location_cart`;';
    }

    protected function setCreateRating($collation_database)
    {
        $this->sql['sellerinfo_rating'] = 'CREATE TABLE IF NOT EXISTS `' . _DB_PREFIX_ . 'sellerinfo_rating` (
                `id_sellerinfo_rating` int(11) UNSIGNED AUTO_INCREMENT NOT NULL,
                `id_sellerinfo` bigint(11) UNSIGNED NOT NULL,
                `id_order` int(11) UNSIGNED NOT NULL,
                `id_lang` int(11) UNSIGNED NOT NULL,
                `id_customer` int(11) UNSIGNED NOT NULL,
                `customer` varchar(256) NOT NULL,
                `content` text NOT NULL,
                `grade` float(16,2) UNSIGNED NOT NULL,
                `ip_address` varchar(64) DEFAULT NULL,
                `response` text DEFAULT NULL,
                `date_add` datetime NOT NULL,
                `date_upd` datetime NOT NULL,
                PRIMARY KEY (`id_sellerinfo_rating`)
            ) COLLATE=\'' . $collation_database . '\' ENGINE=' . _MYSQL_ENGINE_ . ';';

        $this->sql['sellerinfo_rating_criterion'] = 'CREATE TABLE IF NOT EXISTS `' . _DB_PREFIX_ . 'sellerinfo_rating_criterion` (
                `id_sellerinfo_rating_criterion` int(11) UNSIGNED AUTO_INCREMENT NOT NULL,
                `date_add` datetime NOT NULL,
                PRIMARY KEY (`id_sellerinfo_rating_criterion`)
            ) COLLATE=\'' . $collation_database . '\' ENGINE=' . _MYSQL_ENGINE_ . ';';

        $this->sql['sellerinfo_rating_criterion_lang'] = 'CREATE TABLE IF NOT EXISTS `' . _DB_PREFIX_ . 'sellerinfo_rating_criterion_lang` (
                `id_sellerinfo_rating_criterion_lang` int(11) UNSIGNED AUTO_INCREMENT NOT NULL,
                `id_sellerinfo_rating_criterion` int(11) UNSIGNED NOT NULL,
                `id_lang` int(11) UNSIGNED NOT NULL,
                `name` varchar(256) NOT NULL,
                PRIMARY KEY (`id_sellerinfo_rating_criterion_lang`)
            ) COLLATE=\'' . $collation_database . '\' ENGINE=' . _MYSQL_ENGINE_ . ';';

        $this->sql['sellerinfo_rating_grade'] = 'CREATE TABLE IF NOT EXISTS `' . _DB_PREFIX_ . 'sellerinfo_rating_grade` (
                `id_sellerinfo_rating` int(11) UNSIGNED NOT NULL,
                `id_sellerinfo_rating_criterion` int(11) UNSIGNED NOT NULL,
                `grade` int(10) UNSIGNED NOT NULL,
                PRIMARY KEY (`id_sellerinfo_rating`,`id_sellerinfo_rating_criterion`)
            ) COLLATE=\'' . $collation_database . '\' ENGINE=' . _MYSQL_ENGINE_ . ';';

        $this->usql['sellerinfo_rating'] = 'DROP TABLE IF EXISTS `' . _DB_PREFIX_ . 'sellerinfo_rating`;';
        $this->usql['sellerinfo_rating_criterion'] = 'DROP TABLE IF EXISTS `' . _DB_PREFIX_ . 'sellerinfo_rating_criterion`;';
        $this->usql['sellerinfo_rating_criterion_lang'] = 'DROP TABLE IF EXISTS `' . _DB_PREFIX_ . 'sellerinfo_rating_criterion_lang`;';
        $this->usql['sellerinfo_rating_grade'] = 'DROP TABLE IF EXISTS `' . _DB_PREFIX_ . 'sellerinfo_rating_grade`;';
    }

    protected function setCreateMembership($collation_database)
    {
        $this->sql['sellerinfo_membership_type'] = 'CREATE TABLE IF NOT EXISTS `' . _DB_PREFIX_ . 'sellerinfo_membership_type` (
                `id_membership_type` bigint(11) unsigned AUTO_INCREMENT NOT NULL,
                `id_product` bigint(11) unsigned NULL,
                `interval_membership` varchar(64) NULL,
                `units` bigint(11) unsigned NULL,
                `id_group` bigint(11) unsigned NULL,
                `listing_max` int(10) unsigned NULL,
                `product_images` int(10) unsigned NULL,
                `date_add` datetime NOT NULL,
                PRIMARY KEY (`id_membership_type`),
                INDEX `idx_product` (`id_product`),
                INDEX `idx_group` (`id_group`)
            ) COLLATE=\'' . $collation_database . '\' ENGINE=' . _MYSQL_ENGINE_ . ';';

        $this->sql['sellerinfo_membership_history'] = 'CREATE TABLE IF NOT EXISTS `' . _DB_PREFIX_ . 'sellerinfo_membership_history` (
                `id_membership_history` bigint(11) unsigned AUTO_INCREMENT NOT NULL,
                `id_customer` bigint(11) unsigned NOT NULL,
                `id_product` bigint(11) unsigned NULL,
                `id_product_attribute` bigint(11) unsigned NULL,
                `id_order` bigint(11) unsigned NULL,
                `id_order_state` bigint(11) unsigned NULL,
                `quantity` bigint(11) unsigned NULL,
                `interval` varchar(64) NULL,
                `units` bigint(11) unsigned NULL,
                `is_cancelled` tinyint(1) NULL,
                `date_start` datetime NULL,
                `expiry_date` datetime NULL,
                `date_cancelled` datetime NULL,
                `date_add` datetime NOT NULL,
                PRIMARY KEY (`id_membership_history`),
                INDEX `idx_customer` (`id_customer`),
                INDEX `idx_product` (`id_product`),
                INDEX `idx_product_attribute` (`id_product_attribute`) ,
                INDEX `idx_order` (`id_order`)
            ) COLLATE=\'' . $collation_database . '\' ENGINE=' . _MYSQL_ENGINE_ . ';';

        $this->sql['sellerinfo_membership_customer'] = 'CREATE TABLE IF NOT EXISTS `' . _DB_PREFIX_ . 'sellerinfo_membership_customer` (
                `id_membership_customer` bigint(11) unsigned AUTO_INCREMENT NOT NULL,
                `id_customer` bigint(11) unsigned NOT NULL,
                `id_membership_type` bigint(11) unsigned NULL,
                `origin` bigint(11) unsigned NULL,
                `activated` tinyint(1) NULL,
                `activation_date` datetime NULL,
                `reminder_email_date` datetime NULL,
                `date_add` datetime NOT NULL,
                PRIMARY KEY (`id_membership_customer`),
                INDEX `idx_customer` (`id_customer`),
                INDEX `idx_membership_type` (`id_membership_type`)
            ) COLLATE=\'' . $collation_database . '\' ENGINE=' . _MYSQL_ENGINE_ . ';';

        $this->usql['sellerinfo_membership_type'] = 'DROP TABLE IF EXISTS `' . _DB_PREFIX_ . 'sellerinfo_membership_type`;';
        $this->usql['sellerinfo_membership_history'] = 'DROP TABLE IF EXISTS `' . _DB_PREFIX_ . 'sellerinfo_membership_history`;';
        $this->usql['sellerinfo_membership_customer'] = 'DROP TABLE IF EXISTS `' . _DB_PREFIX_ . 'sellerinfo_membership_customer`;';
    }

    protected function setCreateSellerAttachment($collation_database)
    {
        $this->sql['sellerinfo_attachment'] = 'CREATE TABLE IF NOT EXISTS `' . _DB_PREFIX_ . 'sellerinfo_attachment` (
            `id_seller_attachment` BIGINT NOT NULL AUTO_INCREMENT ,
            `id_sellerinfo` BIGINT NOT NULL ,
            `id_attachment` BIGINT NOT NULL ,
            PRIMARY KEY (`id_seller_attachment`)
            ) COLLATE=\'' . $collation_database . '\' ENGINE=' . _MYSQL_ENGINE_ . ';';

        $this->usql['sellerinfo_attachment'] = 'DROP TABLE IF EXISTS `' . _DB_PREFIX_ . 'sellerinfo_attachment`;';
    }

    protected function setCreateSellerOrderDetailInfo($collation_database)
    {
        $this->sql['sellerinfo_order_detail'] = 'CREATE TABLE IF NOT EXISTS `' . _DB_PREFIX_ . 'sellerinfo_order_detail` (
            `id_sellerinfo_order_detail` bigint(11) NOT NULL AUTO_INCREMENT,
            `id_order_detail` bigint(11) NOT NULL,
            `id_product` bigint(11) NOT NULL,
            `id_sellerinfo` bigint(11) NOT NULL,
            PRIMARY KEY (`id_sellerinfo_order_detail`)
            ) COLLATE=\'' . $collation_database . '\' ENGINE=' . _MYSQL_ENGINE_ . ';';

        $this->usql['sellerinfo_order_detail'] = 'DROP TABLE IF EXISTS `' . _DB_PREFIX_ . 'sellerinfo_order_detail`;';
    }

    protected function setCreateImportMatch($collation_database)
    {
        $this->sql['sellerinfo_import_match'] = 'CREATE TABLE IF NOT EXISTS `' . _DB_PREFIX_ . 'sellerinfo_import_match` (
            `id_sellerinfo` BIGINT UNSIGNED NOT NULL,
            `id_import_match` int(11) unsigned NOT NULL,
            PRIMARY KEY (`id_sellerinfo`, `id_import_match`)
            ) COLLATE=\'' . $collation_database . '\' ENGINE=' . _MYSQL_ENGINE_ . ';';

        $this->usql['sellerinfo_import_match'] = 'DROP TABLE IF EXISTS `' . _DB_PREFIX_ . 'sellerinfo_import_match`;';
    }

    /**
     * @return bool
     */
    public function install(): bool
    {
        foreach ($this->sql as $table => $sql) {
            if (!\Db::getInstance()->execute($sql)) {
                $this->errors[] = 'The table ' . $table . ' can not be created';

                return false;
            }
        }

        return true;
    }

    /**
     * @return bool
     */
    public function uninstall(): bool
    {
        foreach ($this->usql as $table => $sql) {
            if (!\Db::getInstance()->execute($sql)) {
                $this->errors[] = 'The table ' . $table . ' can not be removed';

                return false;
            }
        }

        return true;
    }

    /**
     * Get the errors list
     *
     * @return array
     */
    public function getErrors(): array
    {
        return $this->errors;
    }
}
