<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Foundation\Bus\DispatchesJobs;

use App\Jobs\PhotoDelete;
use App\Models\Photo;

class PhotoAutoDelete extends Command
{
	use DispatchesJobs;

    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'photo:autodelete';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Auto Delete Photos';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        Photo::where('remove',1)->chunk(10,function($chunk) {
        	foreach ($chunk as $o) {
	            foreach ($o->list_duplicates(TRUE) as $oo) {
	            	if (! $oo->signature OR ! $oo->file_signature)
	            		continue;

	            	if ($oo->signature == $o->signature AND $oo->file_signature == $o->file_signature) {
	            		if ($oo->remove) {
		            		$this->info(sprintf('Removing: %s (%s)',$oo->id,$oo->filename));

				            $this->dispatch((new PhotoDelete($o))->onQueue('delete'));
			            }
		            }
		        }
	        }
        });
    }
}