<?php

namespace KupShop\OrderDiscountBundle\Resources\upgrade;

use KupShop\CatalogBundle\Util\ReviewsUtil;

class ReviewsUpgrade extends \UpgradeNew
{
    protected function isAllowed()
    {
        return findModule(\Modules::REVIEWS);
    }

    public function check_heurekaIDNameType()
    {
        return !$this->checkColumnExists('reviews', 'heureka_id');
    }

    /** Change name of column heureka_id in reviews to external_id and type from int(11) to varchar(32)*/
    public function upgrade_heurekaIDNameType()
    {
        sqlQuery('ALTER TABLE reviews CHANGE heureka_id external_id VARCHAR(32) NULL');
        $this->upgradeOK();
    }

    public function check_externalId()
    {
        return $this->checkColumnExists('reviews', 'external_id');
    }

    /** Add external_id column to table reviews */
    public function upgrade_externalId()
    {
        sqlQuery('ALTER TABLE reviews ADD COLUMN external_id VARCHAR(50) NULL UNIQUE;');

        $this->upgradeOK();
    }

    public function check_externalIdType()
    {
        return $this->checkColumnType('reviews', 'external_id', 'VARCHAR(50)');
    }

    /** Increase reviews.external_id length */
    public function upgrade_externalIdType()
    {
        sqlQuery('ALTER TABLE reviews CHANGE COLUMN external_id external_id VARCHAR(50) NULL');
        $this->upgradeOK();
    }

    public function check_DataColumn()
    {
        return $this->checkColumnExists('reviews', 'data');
    }

    /** Add data column into reviews */
    public function upgrade_DataColumn()
    {
        sqlQuery('ALTER TABLE reviews ADD COLUMN data MEDIUMTEXT DEFAULT NULL');

        $this->upgradeOK();
    }

    public function check_reportSeverity()
    {
        return $this->checkColumnExists('reviews', 'source');
    }

    /** Add source to reviews */
    public function upgrade_reportSeverity()
    {
        sqlQuery("alter table reviews	add source enum('heureka.cz', 'heureka.sk', 'arukereso.hu', 'compari.ro', 'zbozi.cz') NULL;");
        sqlQuery('create index report_reviews_security_index ON reviews (source);');

        $this->updateReviewSource();

        $this->upgradeOK();
    }

    public function check_ReviewsSource()
    {
        return $this->checkEnumOptions('reviews', 'source', array_keys(ReviewsUtil::$sources));
    }

    /** reviews: Add 'source' column enum   */
    public function upgrade_ReviewsSource()
    {
        $this->updateEnumOptions(
            'reviews',
            'source',
            array_keys(ReviewsUtil::$sources));
        $this->upgradeOK();
    }

    public function check_ReviewsSourceUpToDate()
    {
        return $this->checkDataMigration(21);
    }

    /** Reviews: Update source column */
    public function upgrade_ReviewsSourceUpToDate()
    {
        $this->updateReviewSource();

        $this->commitDataMigration(21);

        $this->upgradeOK();
    }

    protected function updateReviewSource(): void
    {
        if (findModule(\Modules::TRANSLATIONS)) {
            sqlQuery("UPDATE reviews SET source='heureka.cz' WHERE external_id  REGEXP '^[0-9]+$' AND id_language = 'cs' AND source IS NULL");
            sqlQuery("UPDATE reviews SET source='heureka.sk' WHERE external_id  REGEXP '^[0-9]+$' AND id_language = 'sk' AND source IS NULL");
            sqlQuery("UPDATE reviews SET source='zbozi.cz' WHERE external_id NOT REGEXP '^[0-9]+$' AND id_language = 'cs' AND source IS NULL");
            sqlQuery("UPDATE reviews SET source='arukereso.hu' WHERE external_id LIKE ('hu_%') AND id_language = 'hu' AND source IS NULL");
            sqlQuery("UPDATE reviews SET source='compari.ro' WHERE external_id LIKE ('ro_%') AND id_language = 'ro'  AND source IS NULL");
        } else {
            sqlQuery("UPDATE reviews SET source='heureka.cz' WHERE external_id  REGEXP '^[0-9]+$' AND source IS NULL");
            sqlQuery("UPDATE reviews SET source='zbozi.cz' WHERE external_id NOT REGEXP '^[0-9]+$' AND source IS NULL");
        }
    }

    public function check_ReviewResponse()
    {
        return $this->checkColumnExists('reviews', 'response');
    }

    /** Add review response */
    public function upgrade_ReviewResponse()
    {
        sqlQuery('ALTER TABLE reviews ADD COLUMN response MEDIUMTEXT NULL;');
        sqlQuery('ALTER TABLE reviews ADD COLUMN response_time DATETIME NULL;');

        $this->upgradeOK();
    }

    public function check_ReviewSaleIdColumn(): bool
    {
        return findModule(\Modules::SALES) && $this->checkColumnExists('reviews', 'id_sale');
    }

    /** Add reviews.id_sale column */
    public function upgrade_ReviewSaleIdColumn(): void
    {
        sqlQuery('ALTER TABLE reviews ADD COLUMN id_sale INT(11) UNSIGNED NULL AFTER id_order');
        sqlQuery('ALTER TABLE reviews ADD CONSTRAINT FK_reviews_id_sale FOREIGN KEY (id_sale) REFERENCES sales(id) ON UPDATE CASCADE ON DELETE CASCADE');

        $this->upgradeOK();
    }
}
