<?php

namespace App\Console\Commands;

use Carbon\Carbon;
use Illuminate\Console\Command;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Storage;

use App\Jobs\CatalogScan;
use App\Traits\Type;

class CatalogScanAll extends Command
{
	use DispatchesJobs,Type;

	private int|bool $depth = true;
	protected const chunk_size = 5;

	/**
	 * The name and signature of the console command.
	 *
	 * @var string
	 */
	protected $signature = 'catalog:scanall'
		.' {type : Photo | Video }'
		.' {--i|ignore : Ignore missing files}'
		.' {--s|scan : Force Rescan of all files }';

	/**
	 * The console command description.
	 *
	 * @var string
	 */
	protected $description = '(re)Scan Media';

	/**
	 * Execute the console command.
	 *
	 * @return int
	 */
	public function handle(): int
	{
		$started = Carbon::now();
		$class = $this->getModelType($this->argument('type'));

		Log::info('Scanning disk: '.Storage::disk('nas')->path(''));

		$c = 0;
		$dirs = Storage::disk('nas')->directories($class::dir_prefix());
		// Scan files in dir, and make sure file lives in DB, (touch it if it does), otherwise create it
		foreach ($dirs as $dir) {
			Log::info(sprintf(' - DIR: %s',$dir));

			// Take x files at a time and check the DB
			foreach ($this->files($dir,$class::config,$class::dir_prefix())->chunk(self::chunk_size) as $chunk) {
				$list = $class::whereIn('filename',$chunk)->get();

				// If there is a new file found it wont be in the DB
				if ($list->count() !== self::chunk_size)
					foreach ($chunk->diff($list->pluck('filename')) as $file) {
						Log::info(sprintf('Found new file [%s] - queueing scan',$file));

						$o = new $class;
						$o->filename = $file;
						$o->file_signature = $o->getObjectOriginal('file_signature');
						$o->save();

						CatalogScan::dispatch($o)
							->onQueue('scan');

						$c++;
					}

				foreach ($list as $o) {
					// Check the details are valid
					if ($o->file_signature === $o->getObjectOriginal('file_signature')) {
						// For sanity, we'll check a couple of other attrs
						if (($o->width !== $o->getObjectOriginal('width')) || ($o->height !== $o->getObjectOriginal('height')))
							Log::alert(sprintf('Dimensions [%s] (%s x %s) mismatch for [%s]',
								$o->dimensions,
								$o->getObjectOriginal('width'),
								$o->getObjectOriginal('height'),
								$o->file_name(FALSE)));

					} else {
						Log::alert(sprintf('File Signature [%s] doesnt match [%s] for [%s]',
							$o->getObjectOriginal('file_signature'),
							$o->file_signature,
							$o->file_name(FALSE)));
					}

					if ($o->signature !== $o->getObjectOriginal('signature')) {
						Log::notice(sprintf('Updating image signature for [%s] to [%s] was [%s]',$o->filename,$o->signature,$o->getObjectOriginal('signature')));

						$o->signature = $o->getObjectOriginal('signature');
					}

					if ($o->isDirty())
						$o->save();
					else
						$o->touch();

					if ($this->option('scan')) {
						Log::info(sprintf('Forcing re-scan of [%s] - queued',$o->filename));

						CatalogScan::dispatch($o)
							->onQueue('scan');
					}

					$c++;
				}
			}

			break;
		}

		Log::info('Checking for missing files');

		// Find DB records before $started, check they exist (they shouldnt), and delete if not
		if (! $this->option('ignore'))
			foreach ($class::select(['id','filename'])->where('updated_at','<',$started)->cursor() as $o)
				Log::error(sprintf('It appears that file [%s] is missing (%d)',$o->filename,$o->id));

		Log::info(sprintf('Processed [%s]',$c));

		return self::SUCCESS;
	}

	/**
	 * Recursively find files that we should catalog
	 *
	 * @param string $dir Directory to get files from
	 * @param string $type Configuration key to refer to config())
	 * @param string $prefix Remove the prefix from the filename
	 * @return Collection
	 */
	public function files(string $dir,string $type,string $prefix): Collection
	{
		$files = collect(Storage::disk('nas')->files($dir))
			->map(fn($item)=>preg_replace('#^'.$prefix.'#','',$item))
			->filter(function($item) use ($type) {
				return ((! ($x=strrpos($item,'.')))
					|| (! in_array(strtolower(substr($item,$x+1)),config($type.'.import.accepted'))))
					? NULL
					: $item;
			});

		if (! $this->depth)
			return $files;

		if (is_numeric($this->depth))
			$this->depth--;

		foreach (Storage::disk('nas')->directories($dir) as $dir)
			$files = $files->merge($this->files($dir,$type,$prefix));

		if (is_numeric($this->depth))
			$this->depth++;

		return $files;
	}
}