<?php

namespace App\Console\Commands;

use App\Models\Modules;

class ModuleAdd extends BaseCommand
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'module:add {module}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Adds a module that Creatia will look for.';

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

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $module = $this->argument('module');

        // Check if that path exists in the first place. 
        if(!is_dir(base_path("app/Modules/$module"))) {
            echo "[CREATIA][ERROR]: Could not find a module directory at path: '" . base_path('app/Modules/$module') . "' - Make sure spacing, casing and pathing is correct.\n";
            return 1;
        }

        if(Modules::where('name', '=', $module)->count()) {
            echo "[CREATIA][ERROR]: Module '$module' already exists.\n";
            return 1;
        }
        
        Modules::create([
            'name' => $module
        ]);

        echo "[CREATIA][SUCCESS]Successfully generated module entry in database. Please test your module thoroughly however.\n";
        
        return 0;
    }
}