<?php

namespace App\Console\Commands;

use App\Models\Echoarea;
use App\Models\Echomail;
use App\Models\Netmail;
use Carbon\Carbon;
use Illuminate\Console\Command;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Notification;

class ConvertMongo extends Command
{
	/**
	 * The name and signature of the console command.
	 *
	 * @var string
	 */
	protected $signature = 'convert:mongo {start=0}';

	/**
	 * The console command description.
	 *
	 * @var string
	 */
	protected $description = 'Convert Mongo to Cockroach';

	/**
	 * Execute the console command.
	 *
	 * @return mixed
	 * @throws \Exception
	 */
	public function handle()
	{
		/*
		// Netmails
		foreach (DB::connection('mongodb')->collection('netmails')->cursor() as $o) {
			$o['mid'] = (string)$o['_id'];

			foreach([
				'reply'=>'replyid',
				'packet'=>'sent_pkt',
			] as $key => $newkey) {
				if (array_key_exists($key,$o)) {
					$o[$newkey] = $o[$key];
					unset($o[$key]);
				}
			}

			unset($o['_id'],$o['sent'],$o['reply']);

			foreach (['created_at','updated_at','sent_at','datetime','deleted_at'] as $key) {
				if (Arr::get($o,$key))
					$o[$key] = Carbon::create($o[$key]->toDatetime());
			}

			if (! Arr::get($o,'datetime'))
				$o['datetime'] = $o['created_at'];

			$oo = Netmail::withTrashed()->where('mid',$o['mid'])->firstOrNew();
			$oo->forceFill($o);

			try {
				$oo->save(['timestamps' => false]);
			} catch (\Exception $e) {
				$this->warn(sprintf('Netmail didnt move over: %s (%s)',$o['mid'],$e->getMessage()));
			}
		}
		*/

		/**/
		// Echomail
		$c = 0;
		foreach (DB::connection('mongodb')->collection('echomails')->cursor() as $o) {
			if (++$c < $this->argument('start'))
				continue;

			if (! ($c%100))
				Log::debug(sprintf('Processed : %d Echomails',$c));

			$o['mid'] = (string)$o['_id'];

			foreach([
				'reply'=>'replyid',
				'rogue_seen'=>'rogue_seenby',
			] as $key => $newkey) {
				if (array_key_exists($key,$o)) {
					$o[$newkey] = $o[$key];
					unset($o[$key]);
				}
			}

			$path = (array_key_exists('path',$o) && $o['path']) ? $o['path'] : NULL;
			$seenby = (array_key_exists('seenby',$o) && $o['seenby']) ? $o['seenby'] : NULL;
			$packet = (array_key_exists('packet',$o) && $o['packet']) ? $o['packet'] : NULL;

			unset($o['_id'],$o['reply'],$o['path'],$o['seenby'],$o['toexport'],$o['sent_at'],$o['packet'],$o['sent']);

			foreach (['created_at','updated_at','datetime','deleted_at'] as $key) {
				if (Arr::get($o,$key))
					$o[$key] = Carbon::create($o[$key]->toDatetime());
			}

			if (! Arr::get($o,'datetime'))
				$o['datetime'] = $o['created_at'];

			if (Arr::get($o,'echoarea') && ! Arr::get($o,'echoarea_id')) {
				$ea = Echoarea::where('name',$o['echoarea'])->single();
				$o['echoarea_id'] = $ea->id;
				unset($o['echoarea']);
			}

			if (! $o['echoarea_id']) {
				Log::error(sprintf('Echomail didnt move over: %s [%d] - has no echoarea_id',$o['mid'],$c));
				continue;
			}

			if (Arr::get($o,'msg_src') && ! Arr::get($o,'msg_crc')) {
				$o['msg_crc'] = 'x'.md5($o['msg_src']);
			}

			$oo = Echomail::withTrashed()->where('mid',$o['mid'])->firstOrNew();
			$oo->forceFill($o);
			$oo->set_path = $path ? array_filter($path) : [];
			$oo->set_seenby = $seenby ? array_filter($seenby): [];
			$oo->set_packet = $packet;
			$oo->no_export = TRUE;

			try {
				$oo->save(['timestamps'=>FALSE]);

			} catch (\Exception $e) {
				Log::error(sprintf('Echomail didnt move over: %s [%d] (%s@%d|%s)',$o['mid'],$c,$e->getFile(),$e->getLine(),$e->getMessage()));
				dd(['e'=>$e,'o'=>$o,'oo'=>$oo]);
			}

			DB::connection('mongodb')->collection('echomails')->delete($o['mid']);
		}
		/**/

		// Update old MID seenby with proper ID
		foreach (DB::connection('cockroach')->table('echomail_seenby')->whereNotNull('mid')->whereNull('echomail_id')->cursor() as $o)
		{
			$eo = Echomail::where('mid',$o->mid)->get();

			if ($eo->count() && $eo->count() == 1) {
				DB::update('UPDATE echomail_seenby set echomail_id = ?, mid=NULL where echomail_id IS NULL AND mid = ? ',[
					$eo->first()->id,
					$o->mid,
				]);

			} elseif ($eo->count() > 1) {
				Log::error(sprintf('Echomail [%s] has more than 1 record [%d] - skipped',$o->mid,$eo->count()));
			}
		}
	}
}