<?php

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

function geo_postal_upgrade($pluginversion) {
    if ($pluginversion < "0.0.1") {
        $table = Wrapper::get_table("geo_postals");
        $table->addColumn("country_id",  "integer", Array("null" => false));
        $table->addColumn("state_id",    "integer", Array("null" => false));
        $table->addColumn("city_id",     "integer", Array("null" => false));
        $table->addColumn("zone_id",     "integer", Array("null" => true));
        $table->addColumn("code",        "string",  Array("null" => false));
        $table->addColumn("road",        "string",  Array("null" => true));
        $table->addColumn("description", "string",  Array("null" => true));
        $table->addTimestamps();
        $table->addSoftDelete();
        $table->addForeignKey('state_id',   Wrapper::get_table('geo_states'));
        $table->addForeignKey('country_id', Wrapper::get_table('geo_countries'));
        $table->addForeignKey('city_id',    Wrapper::get_table('geo_cities'));
        $table->addForeignKey('zone_id',    Wrapper::get_table('geo_zones'));
        $table->create();

        $table = Wrapper::get_table("geo_postal_caches");
        $table->addColumn("code", "string",  Array("null" => false));
        $table->addColumn("json", "text",  Array("null" => false));
        $table->addTimestamps();
        $table->create();

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

    if ($pluginversion < "1.0.1") {

        $table = Wrapper::get_table("geo_postal_caches");
        $table->addColumn("code", "string",  Array("null" => false));
        $table->addColumn("json", "text",  Array("null" => false));
        $table->addTimestamps();
        $table->create();

        Migrator::getInstance()->update_plugin_version("geo_postal", "1.0.1");
        return;
    }

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

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