<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class EmailSupplier extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
		Schema::create('supplier_email', function (Blueprint $table) {
			$table->integer('id',TRUE)->unsigned();
			$table->timestamps();
			$table->integer('site_id')->unsigned();
			$table->boolean('active');
			$table->integer('supplier_detail_id')->unsigned();
			$table->string('product_id');
			$table->string('product_desc')->nullable();
			$table->float('base_cost');
			$table->float('setup_cost')->nullable();
			$table->integer('contract_term')->nullable();

			$table->foreign(['supplier_detail_id'])->references(['id'])->on('supplier_details');
			$table->foreign(['site_id'])->references(['id'])->on('sites');
		});

		Schema::create('product_email', function (Blueprint $table) {
			$table->integer('id',TRUE)->unsigned();
			$table->timestamps();
			$table->integer('site_id')->unsigned();
			$table->boolean('active');
			$table->integer('supplier_email_id')->unsigned();
			$table->string('product_id');
			$table->string('product_desc')->nullable();
			$table->float('base_cost');
			$table->float('setup_cost')->nullable();
			$table->integer('contract_term')->nullable();

			$table->foreign(['supplier_email_id'])->references(['id'])->on('supplier_email');
			$table->foreign(['site_id'])->references(['id'])->on('sites');
		});

		Schema::create('service_emails', function (Blueprint $table) {
			$table->integer('id',TRUE)->unsigned();
			$table->integer('site_id')->unsigned();
			$table->integer('service_id')->unsigned();

			$table->integer('tld_id')->unsigned();
			$table->string('domain_name',128);
			$table->date('expire_at')->nullable();
			$table->string('admin_url')->nullable();
			$table->string('admin_user')->nullable();
			$table->string('admin_pass')->nullable();
			$table->integer('accounts')->nullable();

			$table->foreign(['site_id'])->references(['id'])->on('sites');
			$table->foreign(['service_id'])->references(['id'])->on('ab_service');
			$table->foreign(['tld_id'])->references(['id'])->on('tlds');
		});

		// Migrate email hosting from hosting table to service_email
		// Setup Domains
		foreach (\Illuminate\Support\Facades\DB::select("SELECT * FROM ab_service__hosting WHERE host_type='email'") as $o) {
			$oo = new \App\Models\Service\Email;

			foreach (['site_id','service_id'] as $item)
				$oo->{$item} = $o->{$item};

			$oo->tld_id = $o->domain_tld_id;
			$oo->domain_name = strtolower($o->domain_name);
			$oo->admin_user = strtolower($o->host_username);
			$oo->admin_pass = $o->host_password;
			$oo->expire_at = \Carbon\Carbon::createFromTimestamp($o->host_expire);
			$oo->save();

			\App\Models\Service::where('id',$o->service_id)->update(['model'=>get_class($oo)]);
		};

		\Illuminate\Support\Facades\DB::select("DELETE FROM ab_service__hosting WHERE host_type='email'");

		// insert into suppliers value (null,1,'Google',null,null,null,null,null);
		// insert into supplier_details values (null,now(),now(),null,null,null,null,14,1,null);
		// insert into supplier_email values (null,now(),now(),1,1,13,'Legacy Email','Legacy Email',0,0,0);
		// insert into product_email values (null,now(),now(),1,1,1,'Email Hosting',null,150,0,12);
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
		/*
        Schema::dropIfExists('product_email');
		Schema::dropIfExists('supplier_email');
		Schema::dropIfExists('service_emails');
		*/
		abort(500,'cant go back');
    }
}