<?php

namespace KupShop\I18nBundle\Resources\upgrade;

class DeliveriesUpgrade extends \UpgradeNew
{
    public function check_deliveryTranslationTable()
    {
        return $this->checkTableExists('delivery_type_delivery_translations');
    }

    /** Create deliveries translation tables */
    public function upgrade_deliveryTranslationTable()
    {
        // delivery_type_delivery
        sqlQuery('CREATE TABLE IF NOT EXISTS delivery_type_delivery_translations
            (
              id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
              id_delivery_type_delivery INT(10) UNSIGNED NOT NULL,
              id_language VARCHAR(2) NOT NULL,
              id_admin INT DEFAULT NULL,
              created TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
              updated DATETIME DEFAULT NULL,
              name VARCHAR(50) DEFAULT NULL,
              description MEDIUMTEXT DEFAULT NULL,
              cart_description MEDIUMTEXT DEFAULT NULL
            ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
         ');

        // delivery_type_payment
        sqlQuery('CREATE TABLE IF NOT EXISTS delivery_type_payment_translations
            (
              id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
              id_delivery_type_payment INT(10) UNSIGNED NOT NULL,
              id_language VARCHAR(2) NOT NULL,
              id_admin INT DEFAULT NULL,
              created TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
              updated DATETIME DEFAULT NULL,
              name VARCHAR(50) DEFAULT NULL,
              description MEDIUMTEXT DEFAULT NULL
            ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;           
         ');

        // delivery_type
        sqlQuery('CREATE TABLE IF NOT EXISTS delivery_type_translations
            (
              id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
              id_delivery_type INT(10) UNSIGNED NOT NULL,
              id_language VARCHAR(2) NOT NULL,
              id_admin INT DEFAULT NULL,
              created TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
              updated DATETIME DEFAULT NULL,
              figure ENUM("Y", "N") DEFAULT NULL,
              description MEDIUMTEXT DEFAULT NULL
            ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
            
            CREATE UNIQUE INDEX delivery_type_translations_id_delivery_type_id_language_uindex
              ON delivery_type_translations (id_delivery_type, id_language);
              
            ALTER TABLE delivery_type_translations ADD FOREIGN KEY (id_delivery_type) 
              REFERENCES delivery_type_payment(id) ON DELETE CASCADE;
         ');

        $this->upgradeOK();
    }

    public function check_idDeliveryTypePaymentFK()
    {
        return $this->checkColumnType('delivery_type_payment_translations', 'id_delivery_type_payment', 'int(11)');
    }

    /** Change delivery_type_payment_translations.id_delivery_type_payment to INT(11) and add FK */
    public function upgrade_idDeliveryTypePaymentFK()
    {
        sqlQuery('DELETE FROM delivery_type_payment_translations WHERE id_delivery_type_payment NOT IN (SELECT id FROM delivery_type_payment);');
        sqlQuery('ALTER TABLE delivery_type_payment_translations MODIFY id_delivery_type_payment INT(11);');
        sqlQuery(
            'ALTER TABLE delivery_type_payment_translations ADD FOREIGN KEY (id_delivery_type_payment)
              REFERENCES delivery_type_payment(id) ON DELETE CASCADE;'
        );

        $this->upgradeOK();
    }

    public function check_idDeliveryTypeDeliveryFK()
    {
        return $this->checkColumnType('delivery_type_delivery_translations', 'id_delivery_type_delivery', 'int(11)');
    }

    /** Change delivery_type_delivery_translations.id_delivery_type_delivery to INT(11) and add FK */
    public function upgrade_idDeliveryTypeDeliveryFK()
    {
        sqlQuery('DELETE FROM delivery_type_delivery_translations WHERE id_delivery_type_delivery NOT IN (SELECT id FROM delivery_type_delivery);');
        sqlQuery('ALTER TABLE delivery_type_delivery_translations MODIFY id_delivery_type_delivery INT(11);');
        sqlQuery(
            'ALTER TABLE delivery_type_delivery_translations ADD FOREIGN KEY (id_delivery_type_delivery)
              REFERENCES delivery_type_delivery(id) ON DELETE CASCADE;'
        );

        $this->upgradeOK();
    }

    public function check_deliveryTypePaymentIndex()
    {
        return $this->checkIndexNameExists('delivery_type_payment_translations', 'dtpt_id_language_id_delivery_type_payment_uindex');
    }

    /** Add (id_language, id_delivery_type_payment) index to delivery_type_payment_translations */
    public function upgrade_deliveryTypePaymentIndex()
    {
        sqlQuery(
            'create unique index dtpt_id_language_id_delivery_type_payment_uindex
	            on delivery_type_payment_translations (id_language, id_delivery_type_payment);'
        );

        $this->upgradeOK();
    }

    public function check_deliveryTypeDeliveryIndex()
    {
        return $this->checkIndexNameExists('delivery_type_delivery_translations', 'dtdt_id_language_id_delivery_type_delivery_uindex');
    }

    /** Add (id_language, id_delivery_type_delivery) index to delivery_type_delivery_translations */
    public function upgrade_deliveryTypeDeliveryIndex()
    {
        sqlQuery(
            'create unique index dtdt_id_language_id_delivery_type_delivery_uindex
	on delivery_type_delivery_translations (id_language, id_delivery_type_delivery);'
        );

        $this->upgradeOK();
    }

    public function check_deliveryTypePaymentCartDescription()
    {
        return $this->checkColumnExists('delivery_type_payment_translations', 'cart_description');
    }

    /** Add cart_description field to delivery_type_payment */
    public function upgrade_deliveryTypePaymentCartDescription()
    {
        sqlQuery('ALTER TABLE delivery_type_payment_translations ADD COLUMN cart_description MEDIUMTEXT NULL ');

        $this->upgradeOK();
    }
}
