<?php

namespace Lucent\Commands;

use DirectoryIterator;
use Exception;
use Illuminate\Console\Command;
use Intervention\Image\ImageManager;
use Lucent\Channel\ChannelService;
use Lucent\File\FileService;
use Lucent\Query\Query;
use Lucent\Schema\FilesSchema;
use Lucent\Schema\Schema;
use Lucent\Schema\Type;

class RebuildThumbnails extends Command
{

    protected $signature = 'lucent:rebuild-thumbnails';

    protected $description = 'Rebuilds thumbnails for path';


    public function __construct(
        public Query $query,
        public FileService $fileService,
        )
    {
        parent::__construct();
    }


    public function handle(ChannelService $channelService): int
    {
        $channelService->channel->schemas
            ->filter(fn(Schema $schema) => get_class($schema) === FilesSchema::class)
            ->map([$this, 'rebuildThumbnails']);

        return 0;
    }

    public function rebuildThumbnails(FilesSchema $schema): void
    {
        $this->info("Rebuilding thumbnails for ". $schema->name);
        $records = $this->query->filter(["schema" => $schema->name])->run()->records;
        $disk = $this->fileService->loadDisk($schema->disk);
        foreach ($records as $record) {
            $this->fileService->createTemplates($disk, $record->_file->path);
        }
    }

}
