<?php

namespace KupShop\ElninoBundle\Resources\upgrade;

class RichContentUpgrade extends \UpgradeNew
{
    public function check_abraRichContentsTable()
    {
        return $this->checkTableExists('abra_richcontents');
    }

    /** Add table abra_richcontents */
    public function upgrade_abraRichContentsTable()
    {
        sqlQuery('CREATE TABLE IF NOT EXISTS abra_richcontents
            (
              id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
              id_abra INT DEFAULT NULL,
              id_language VARCHAR(2) NOT NULL,
              path VARCHAR(191) DEFAULT NULL,
              updated TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
            ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
            
            CREATE UNIQUE INDEX abra_richcontents_id_abra_uindex
              ON abra_richcontents (id_abra);');
        $this->upgradeOK();
    }

    public function check_RichContentForeignKeys()
    {
        return $this->checkColumnExists('abra_models', 'id_richcontent');
    }

    /** Add column id_richcontent to tables products, abra_models, abra_series, producers and its translations */
    public function upgrade_RichContentForeignKeys()
    {
        sqlQuery('ALTER TABLE products ADD COLUMN id_richcontent INT UNSIGNED DEFAULT NULL;
            ALTER TABLE products ADD FOREIGN KEY (`id_richcontent`) REFERENCES `abra_richcontents`(`id`);');

        sqlQuery('ALTER TABLE abra_models ADD COLUMN id_richcontent INT UNSIGNED DEFAULT NULL;
            ALTER TABLE abra_models ADD FOREIGN KEY (`id_richcontent`) REFERENCES `abra_richcontents`(`id`);');

        sqlQuery('ALTER TABLE abra_series ADD COLUMN id_richcontent INT UNSIGNED DEFAULT NULL;
            ALTER TABLE abra_series ADD FOREIGN KEY (`id_richcontent`) REFERENCES `abra_richcontents`(`id`);');

        sqlQuery('ALTER TABLE producers ADD COLUMN id_richcontent INT UNSIGNED DEFAULT NULL;
            ALTER TABLE producers ADD FOREIGN KEY (`id_richcontent`) REFERENCES `abra_richcontents`(`id`);');

        if (findModule(\Modules::TRANSLATIONS)) {
            sqlQuery('ALTER TABLE products_translations ADD COLUMN id_richcontent INT UNSIGNED DEFAULT NULL;
                ALTER TABLE products_translations ADD FOREIGN KEY (`id_richcontent`) REFERENCES `abra_richcontents`(`id`);');

            sqlQuery('ALTER TABLE abra_models_translations ADD COLUMN id_richcontent INT UNSIGNED DEFAULT NULL;
                ALTER TABLE abra_models_translations ADD FOREIGN KEY (`id_richcontent`) REFERENCES `abra_richcontents`(`id`);');

            sqlQuery('ALTER TABLE abra_series_translations ADD COLUMN id_richcontent INT UNSIGNED DEFAULT NULL;
                ALTER TABLE abra_series_translations ADD FOREIGN KEY (`id_richcontent`) REFERENCES `abra_richcontents`(`id`);');

            sqlQuery('ALTER TABLE producers_translations ADD COLUMN id_richcontent INT UNSIGNED DEFAULT NULL;
                ALTER TABLE producers_translations ADD FOREIGN KEY (`id_richcontent`) REFERENCES `abra_richcontents`(`id`);');
        }

        $this->upgradeOK();
    }

    public function check_RichContentForeignKeysTranslations(): bool
    {
        return findModule(\Modules::TRANSLATIONS) && $this->checkColumnExists('products_translations', 'id_richcontent');
    }

    /** Add column id_richcontent to translations tables products, abra_models, abra_series, producers */
    public function upgrade_RichContentForeignKeysTranslations(): void
    {
        sqlQuery('ALTER TABLE products_translations ADD COLUMN id_richcontent INT UNSIGNED DEFAULT NULL;
                ALTER TABLE products_translations ADD FOREIGN KEY (`id_richcontent`) REFERENCES `abra_richcontents`(`id`);');

        sqlQuery('ALTER TABLE abra_models_translations ADD COLUMN id_richcontent INT UNSIGNED DEFAULT NULL;
                ALTER TABLE abra_models_translations ADD FOREIGN KEY (`id_richcontent`) REFERENCES `abra_richcontents`(`id`);');

        sqlQuery('ALTER TABLE abra_series_translations ADD COLUMN id_richcontent INT UNSIGNED DEFAULT NULL;
                ALTER TABLE abra_series_translations ADD FOREIGN KEY (`id_richcontent`) REFERENCES `abra_richcontents`(`id`);');

        sqlQuery('ALTER TABLE producers_translations ADD COLUMN id_richcontent INT UNSIGNED DEFAULT NULL;
                ALTER TABLE producers_translations ADD FOREIGN KEY (`id_richcontent`) REFERENCES `abra_richcontents`(`id`);');

        $this->upgradeOK();
    }

    public function check_RichContentForeignKeysTypes()
    {
        return !$this->checkColumnExists('abra_models', 'id_richcontent') && $this->checkColumnType('abra_models', 'id_richcontent', 'INT(10) UNSIGNED');
    }

    /** Fix id_richcontent column type */
    public function upgrade_RichContentForeignKeysTypes()
    {
        sqlQuery('ALTER TABLE products MODIFY id_richcontent INT UNSIGNED DEFAULT NULL;
            ALTER TABLE products ADD FOREIGN KEY (`id_richcontent`) REFERENCES `abra_richcontents`(`id`);');

        sqlQuery('ALTER TABLE abra_models MODIFY id_richcontent INT UNSIGNED DEFAULT NULL;
            ALTER TABLE abra_models ADD FOREIGN KEY (`id_richcontent`) REFERENCES `abra_richcontents`(`id`);');

        sqlQuery('ALTER TABLE abra_series MODIFY id_richcontent INT UNSIGNED DEFAULT NULL;
            ALTER TABLE abra_series ADD FOREIGN KEY (`id_richcontent`) REFERENCES `abra_richcontents`(`id`);');

        sqlQuery('ALTER TABLE producers MODIFY id_richcontent INT UNSIGNED DEFAULT NULL;
            ALTER TABLE producers ADD FOREIGN KEY (`id_richcontent`) REFERENCES `abra_richcontents`(`id`);');

        if (findModule(\Modules::TRANSLATIONS)) {
            sqlQuery('ALTER TABLE products_translations MODIFY id_richcontent INT UNSIGNED DEFAULT NULL;
                ALTER TABLE products_translations ADD FOREIGN KEY (`id_richcontent`) REFERENCES `abra_richcontents`(`id`);');

            sqlQuery('ALTER TABLE abra_models_translations MODIFY id_richcontent INT UNSIGNED DEFAULT NULL;
                ALTER TABLE abra_models_translations ADD FOREIGN KEY (`id_richcontent`) REFERENCES `abra_richcontents`(`id`);');

            sqlQuery('ALTER TABLE abra_series_translations MODIFY id_richcontent INT UNSIGNED DEFAULT NULL;
                ALTER TABLE abra_series_translations ADD FOREIGN KEY (`id_richcontent`) REFERENCES `abra_richcontents`(`id`);');

            sqlQuery('ALTER TABLE producers_translations MODIFY id_richcontent INT UNSIGNED DEFAULT NULL;
                ALTER TABLE producers_translations ADD FOREIGN KEY (`id_richcontent`) REFERENCES `abra_richcontents`(`id`);');
        }

        $this->upgradeOK();
    }
}
