<?php

use Schema\Wrapper as Wrapper;
use App\Core\Sanity\MigratorController as Migrator;

//https://book.cakephp.org/phinx/0/en/migrations.html#valid-column-types
function common_email_upgrade($pluginversion) {
    if ($pluginversion < "0.0.1") {

        $table = Wrapper::get_table("common_emails_config");
        $table->addColumn("name", "string", Array("null" => false));
        $table->addColumn("smtphosts", "string", Array("null" => false));
        $table->addColumn("smtpsecure", "integer", Array("null" => false, "default" => 0));
        $table->addColumn("smtpauthtype", "integer", Array("null" => false, "default" => 0));
        $table->addColumn("smtpuser", "string", Array("null" => false));
        $table->addColumn("smtppass", "string", Array("null" => false));
        $table->addColumn("smtpmaxbulk", "integer", Array("null" => false, "default" => 1));
        $table->addColumn("noreplyaddress", "string", Array("null" => false, "default" => ''));
        $table->addColumn("sitemailcharset", "string", Array("null" => false, "default" => 'UTF-8'));
        $table->addTimestamps();
        $table->addSoftDelete();
        $table->create();

        $table = Wrapper::get_table("common_emails_templates");
        $table->addColumn("name", "string", Array("null" => false));
        $table->addColumn("title", "string", Array("null" => false));
        $table->addColumn("body", "text");
        $table->addTimestamps();
        $table->addSoftDelete();
        $table->create();

        $table = Wrapper::get_table("common_emails");
        $table->addColumn("name", "string", Array("null" => false));
        $table->addColumn("template", "integer", Array());
        $table->addColumn("title", "text");
        $table->addColumn("body", "text");
        $table->addColumn("timesent", "timestamp", Array());
        $table->create();

        Migrator::getInstance()->update_plugin_version("common_email", "1.0.0");
        return;
    }

    //if ($pluginversion < "0.0.2") {
        //$table = Wrapper::get_table("common_emails");
        //Migrator::getInstance()->update_plugin_version("common_email", "1.0.1");
        //return;
    //}
}

function common_email_rollback($pluginversion) {
    if($pluginversion > "0.0.1"){
        $table = Wrapper::get_table("common_emails");
        $table->drop();
        return;
    }
}