<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Str;

class CreateModelAndMigrationCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'module:create {name}
                                          {--momi=}
                                          {--create_model=}
                                          {--create_policy=} {--model=}
                                          {--create_migration=}
                                          {--add_migration=}
    ';

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

    /**
     * Execute the console command.
     */
    public function handle()
    {
        if ($this->option('momi')) {
            $this->input->setOption('create_model', $this->option('momi'));
            $this->input->setOption('create_migration', $this->option('momi'));
        }
        if ($this->option('create_policy')) {
            $this->createPolicy();
        }
        if ($this->option('create_model')) {
            $this->createModel();
        }
        if ($this->option('create_migration')) {
            $this->createMigration();
        }
        if ($this->option('add_migration')) {
            $this->addMigration();
        }
    }

    private function createModel()
    {
        try
        {
            $path = trim($this->argument('name'));
            $model = Str::singular(class_basename($this->option('create_model')));
            $this->call('make:model', [
                'name' => 'App\\Modules\\' . $path . '\\Models\\' . $model
            ]);
        }
        catch (\Exception $e)
        {
            $e->getMessage();
        }
        
    }

    private function createPolicy()
    {
        try
        {
            $path = trim($this->argument('name'));
            $policy = Str::singular(class_basename($this->option('create_policy')));
            $model = Str::singular(class_basename($this->option('model')));
            $this->call('make:policy', [
                'name' => 'App\\Modules\\' . $path . '\\Http\\Policies\\' . $policy,
                '--model' => $model
            ]);
        }
        catch (\Exception $e)
        {
            $e->getMessage();
        }
        
    }

    private function createMigration()
    {
        $path = trim($this->argument('name'));
        $table = Str::plural(Str::snake(class_basename($this->option('create_migration'))));
        try
        {
            $this->call('make:migration', [
                'name' => 'create_' . $table . '_table',
                '--create' => $table,
                '--path' => 'app/Modules/' . $path . '/Database/Migrations'
            ]);
        }
        catch (\Exception $e)
        {
            $e->getMessage();
        }
    }

    private function addMigration()
    {
        $path = trim($this->argument('name'));
        $table = $this->option('add_migration');
        try
        {
            $this->call('make:migration', [
                'name' => $table,
                '--path' => 'app/Modules/' . $path . '/Database/Migrations'
            ]);
        }
        catch (\Exception $e)
        {
            $e->getMessage();
        }
    }
}
