<?php

declare(strict_types=1);

namespace DoctrineMigrations;

use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;

/**
 * Auto-generated Migration: Please modify to your needs!
 */
final class Version20201016103219 extends AbstractMigration
{
    public function getDescription() : string
    {
        return '';
    }

    public function up(Schema $schema) : void
    {
        // this up() migration is auto-generated, please modify it to your needs
        $this->abortIf($this->connection->getDatabasePlatform()->getName() !== 'mysql', 'Migration can only be executed safely on \'mysql\'.');

        $this->addSql('ALTER TABLE customer_note CHANGE gender gender enum(\'male\', \'female\', \'diverse\')');
        $this->addSql('ALTER TABLE operator_note CHANGE gender gender enum(\'male\', \'female\', \'diverse\')');
        $this->addSql('ALTER TABLE operator_contact ADD is_xmas_mail_recipient TINYINT(1) NOT NULL, CHANGE gender gender enum(\'male\', \'female\', \'diverse\')');
        $this->addSql('ALTER TABLE customer_contact ADD is_xmas_mail_recipient TINYINT(1) NOT NULL, CHANGE gender gender enum(\'male\', \'female\', \'diverse\')');
        $this->addSql('ALTER TABLE customer_meeting CHANGE gender gender enum(\'male\', \'female\', \'diverse\')');
        $this->addSql('ALTER TABLE operator_meeting CHANGE gender gender enum(\'male\', \'female\', \'diverse\')');
    }

    public function postUp(Schema $schema): void
    {
        // Process xmas flags
        $opContacts = $this->connection->fetchAll("SELECT * FROM `operator_contact`");
        foreach ($opContacts as $opContact) {
            if (!is_null($opContact['comment']) && strpos($opContact['comment'], 'Weihnachten')) {
                $this->connection->exec("UPDATE `operator_contact` SET `is_xmas_mail_recipient` = 1 WHERE id = ".$opContact['id']);
            }
        }
        $cuContacts = $this->connection->fetchAll("SELECT * FROM `customer_contact`");
        foreach ($cuContacts as $cuContact) {
            if (!is_null($cuContact['comment']) && strpos($cuContact['comment'], 'Weihnachten')) {
                $this->connection->exec("UPDATE `customer_contact` SET `is_xmas_mail_recipient` = 1 WHERE id = ".$cuContact['id']);
            }
        }
    }

    public function down(Schema $schema) : void
    {
        // this down() migration is auto-generated, please modify it to your needs
        $this->abortIf($this->connection->getDatabasePlatform()->getName() !== 'mysql', 'Migration can only be executed safely on \'mysql\'.');

        $this->addSql('ALTER TABLE customer_contact DROP is_xmas_mail_recipient, CHANGE gender gender VARCHAR(255) CHARACTER SET utf8 DEFAULT NULL COLLATE `utf8_unicode_ci`');
        $this->addSql('ALTER TABLE customer_meeting CHANGE gender gender VARCHAR(255) CHARACTER SET utf8 DEFAULT NULL COLLATE `utf8_unicode_ci`');
        $this->addSql('ALTER TABLE customer_note CHANGE gender gender VARCHAR(255) CHARACTER SET utf8 DEFAULT NULL COLLATE `utf8_unicode_ci`');
        $this->addSql('ALTER TABLE operator_contact DROP is_xmas_mail_recipient, CHANGE gender gender VARCHAR(255) CHARACTER SET utf8 DEFAULT NULL COLLATE `utf8_unicode_ci`');
        $this->addSql('ALTER TABLE operator_meeting CHANGE gender gender VARCHAR(255) CHARACTER SET utf8 DEFAULT NULL COLLATE `utf8_unicode_ci`');
        $this->addSql('ALTER TABLE operator_note CHANGE gender gender VARCHAR(255) CHARACTER SET utf8 DEFAULT NULL COLLATE `utf8_unicode_ci`');
    }
}
