<?php

namespace App\Console\Commands;

use App\Models\Theme;
use Illuminate\Console\Command;

class ThemesScann extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'theme:scan';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

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

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $themesGallery = scandir(__DIR__."/../../../resources/views/themes/gallery");
        foreach ($themesGallery as $theme) {
            if(substr($theme, 0, 1) == ".") {
                continue;
            }
            $themeModel = Theme::query()->where("name", "=", $theme)->where("typ", "=", "gallery")->first();
            if(!is_null($themeModel)) {
                continue;
            }

            $this->info("Found new Theme: ".$theme);

            $themeModel = new Theme();
            $themeModel->typ = "gallery";
            $themeModel->name = $theme;
            $themeModel->status = "private";
            $themeModel->saveOrFail();
        }

        $themesGallery = scandir(__DIR__."/../../../resources/views/themes/tenant");
        foreach ($themesGallery as $theme) {
            $this->info("Check Theme: ".$theme);
            if(substr($theme, 0, 1) == ".") {
                continue;
            }
            $themeModel = Theme::query()->where("name", "=", $theme)->where("typ", "=", "tenant")->first();
            if(!is_null($themeModel)) {
                $this->info("Themes already exists: ".$theme);
                continue;
            }

            $this->info("Found new Theme: ".$theme);

            $themeModel = new Theme();
            $themeModel->typ = "tenant";
            $themeModel->name = $theme;
            $themeModel->status = "private";
            $themeModel->saveOrFail();
        }
    }
}
