<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Str;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Artisan;

class CrudGenerator extends Command
{
    protected $signature = 'crud:generator
        {name : Class (singular) for example User}';

    protected $description = 'Create CRUD operations';

    public function __construct()
    {
        parent::__construct();
    }

    public function handle()
    {
        $name = $this->argument('name');

        $this->controller($name);
        $this->model($name);
        $this->service($name);
        $this->migration($name);
        // $this->validator($name);
        //$this->request($name);

        File::append(base_path('routes/api.php'), 'Route::resource(\'' . Str::plural(strtolower($name)) . "', '{$name}Controller');");
    }

    protected function controller($name)
    {
        $controllerTemplate = str_replace(
            [
                '{{modelName}}',
                '{{modelNamePluralLowerCase}}',
                '{{modelNameSingularLowerCase}}'
            ],
            [
                $name,
                strtolower(Str::plural($name)),
                strtolower($name)
            ],
            $this->getStub('Controller')
        );

        file_put_contents(app_path("/Http/Controllers/{$name}Controller.php"), $controllerTemplate);
    }

    protected function model($name)
    {
        $modelTemplate = str_replace(
            ['{{modelName}}'],
            [$name],
            $this->getStub('Model')
        );

        if (!file_exists($path = app_path('/Repositories')))
            mkdir($path, 0777, true);

        file_put_contents(app_path("/Repositories/{$name}.php"), $modelTemplate);
    }

    protected function request($name)
    {
        $requestTemplate = str_replace(
            ['{{modelName}}'],
            [$name],
            $this->getStub('Request')
        );

        if (!file_exists($path = app_path('/Http/Requests')))
            mkdir($path, 0777, true);

        file_put_contents(app_path("/Http/Requests/{$name}Request.php"), $requestTemplate);
    }

    protected function service($name)
    {
        $modelTemplate = str_replace(
            [
                '{{modelName}}',
                '{{modelNameSingularLowerCase}}'
            ],
            [
                $name,
                strtolower($name)
            ],
            $this->getStub('Service')
        );

        if (!file_exists($path = app_path('/Services')))
            mkdir($path, 0777, true);

        file_put_contents(app_path("/Services/{$name}Service.php"), $modelTemplate);
    }

    protected function migration($name)
    {
        $tableName = strtolower(Str::plural($name));
        Artisan::call('make:migration', ['name' => "create_{$tableName}_table"]);
    }

    protected function validator($name)
    {
        $modelTemplate = str_replace(
            ['{{modelName}}'],
            [$name],
            $this->getStub('Validator')
        );

        if (!file_exists($path = app_path('/Validators')))
            mkdir($path, 0777, true);

        file_put_contents(app_path("/Validators/{$name}Validator.php"), $modelTemplate);
    }

    protected function getStub($type)
    {
        return file_get_contents(base_path("stubs/custom/$type.stub"));
    }
}
