<?php
namespace Datacake\Core\Includes\Migrations;
use Datacake\Core\Models\IspRedirect;
use Datacake\Core\Models\IspCity;
use Datacake\Core\Models\IspRedirectCity;
use Datacake\Core\Includes\DatacakeCoreActivator;
use Datacake\Core\ORM\WP\Post;

$postEntity = new Post();
$postTable = $postEntity->getTable();

// Creating City Table
$CityEntity = new IspCity();
$cityTableName = $CityEntity->getTable();
$sql = "CREATE TABLE  `$cityTableName` (
        `id` int(10) NOT NULL AUTO_INCREMENT,
        `name` varchar(128) NOT NULL,
        `code` varchar(128) NOT NULL,
        `remote_id` int(128) NOT NULL,
        `lat` decimal(9,7) DEFAULT NULL,
        `long` decimal(9,7) DEFAULT NULL,
        created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
        updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
        PRIMARY KEY  (id),
        INDEX(`remote_id`)
    ) ; $charset_collate";
dbDelta($sql);

// Creating Redirect Table
$redirectEntity = new IspRedirect();
$redirectTableName = $redirectEntity->getTable();
$sql = "CREATE TABLE  `$redirectTableName` (
        `id` int(10) NOT NULL AUTO_INCREMENT,
        `from_page_id` int(10) NOT NULL,
        `to_page_id` int(10) NOT NULL,
        created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
        updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
        PRIMARY KEY  (id),
        INDEX(`from_page_id`),
        INDEX(`to_page_id`)
    ); $charset_collate";
dbDelta($sql);

// Creating Redirect Table
$redirCityentity = new IspRedirectCity();
$redirCityTableName = $redirCityentity->getTable();
$sql = "CREATE TABLE  `$redirCityTableName` (
        `id` int(10) NOT NULL AUTO_INCREMENT,
        `redirect_id` int(10) NOT NULL,
        `city_id` int(10) NOT NULL,
        created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
        updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
        PRIMARY KEY  (id),
        INDEX(`redirect_id`),
        INDEX(`city_id`)
    ); $charset_collate";
dbDelta($sql);

// Adding Foreing key contrains
update_option(DatacakeCoreActivator::$config_name,'1.0.0');