<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;

class GenerateMCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'generatem
                            {path: /path/to/models.json}';

    /**
     * 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 mixed
     */
    public function handle()
    {
        //

        global $argv;
        $models = json_decode(file_get_contents($argv[2]));

        $template = file_get_contents(base_path('generatecv/model.template.php'));

        foreach ($models as $model) {
            $name = str_replace(" ", "", ucwords(str_replace("_", " ", $model)));
            $text = str_replace("_NAME_", $name, $template);
            $text = str_replace("_TABLE_", $model, $text);
            if(!file_exists(app_path("Models/$name.php"))) {
                file_put_contents(app_path("Models/$name.php"), $text);
                echo "Generated " . app_path("Models/$name.php") . "\n";
            }
        }

    }
}
