<?php

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

return new class extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
		DB::statement('ALTER TABLE service__change RENAME COLUMN ordered_at TO ordered_at_old');
		DB::statement('ALTER TABLE service__change RENAME COLUMN effective_at TO effective_at_old');

		Schema::table('service__change', function (Blueprint $table) {
			$table->dateTime('created_at')->nullable()->after('id');
			$table->dateTime('updated_at')->nullable()->after('created_at');
			$table->date('ordered_at')->nullable()->after('ordered_by');
			$table->date('effective_at')->nullable()->after('ordered_at');
			$table->text('notes')->nullable();
		});

		DB::statement('ALTER TABLE service__change MODIFY service_id int unsigned NOT NULL');
		DB::statement('ALTER TABLE service__change MODIFY product_id int unsigned NOT NULL');
		DB::statement('ALTER TABLE service__change MODIFY ordered_by int unsigned NOT NULL');

		DB::statement('ALTER TABLE service__change MODIFY active tinyint(1) NOT NULL');
		DB::statement('ALTER TABLE service__change MODIFY complete tinyint(1) NOT NULL');

		// Convert out dates
		foreach (\App\Models\ServiceChange::withoutGlobalScope(\App\Models\Scopes\SiteScope::class)->cursor() as $o) {
			// If we are running again
			if ($o->created_at)
				continue;

			if ($o->ordered_at_old)
				$o->created_at = \Carbon\Carbon::create(substr($o->ordered_at_old,0,4),substr($o->ordered_at_old,4,2),substr($o->ordered_at_old,6,2));

			$o->updated_at = $o->created_at;
			$o->ordered_at = $o->created_at;

			if ($o->effective_at_old)
				$o->effective_at = \Carbon\Carbon::create(substr($o->effective_at_old,0,4),substr($o->effective_at_old,4,2),substr($o->effective_at_old,6,2));

			$o->save();
		}

		Schema::table('service__change', function (Blueprint $table) {
			$table->dropColumn(['ordered_at_old','effective_at_old']);
		});

		DB::statement('ALTER TABLE service__change MODIFY ordered_at date NOT NULL');

		foreach (\App\Models\Service::where('order_info','LIKE','%change%')->withoutGlobalScope(\App\Models\Scopes\SiteScope::class)->cursor() as $o) {
			if ($o->order_info->only(['change_note','change_product_id','change_date'])->count() !== 3)
				continue;

			$o->changes()->attach([$o->id => [
				'site_id'=> $o->site_id,
				'ordered_by' => 1,
				'ordered_at' => $x=\Carbon\Carbon::createFromDate(\Illuminate\Support\Arr::get($o->order_info,'change_date')),
				'effective_at' => $x,
				'product_id' => \Illuminate\Support\Arr::get($o->order_info,'change_product_id'),
				'notes' => \Illuminate\Support\Arr::get($o->order_info,'change_note'),
				'active' => true,
				'complete' => true,
			]]);

			$o->order_info->forget(['change_note','change_product_id','change_date']);
			$o->save();
		}

		// Additional cleanup
		foreach (\App\Models\Service::whereNotNull('order_info')->withoutGlobalScope(\App\Models\Scopes\SiteScope::class)->cursor() as $o) {
			foreach (['notes','provision_notes','cancel_note'] as $key) {
				if ($o->order_info && ((is_array($o->order_info) && array_key_exists($key,$o->order_info)) || ($o->order_info->has($key))) && is_null(\Illuminate\Support\Arr::get($o->order_info,$key)))
					$o->order_info->forget($key);
			}

			$o->save();
		}

		// Final cleanup
		DB::statement("UPDATE services set order_info=null WHERE order_info='[]'");
    }

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