<?php

namespace App\Console\Module;

use Illuminate\Support\Str;

class EntityHandler
{

    private $namespace = "modules";
    private $moduleName;
    private $entityName;
    
    public function __construct(
        $moduleName,
        $entityName
    ) {
        $this->moduleName   = $moduleName;
        $this->entityName   = $entityName;
    }

    public function execute()
    {
        $this->createController();
        $this->createModel();
        $this->createRelation();
    }

    private function createController()
    {
        $fileName = "{$this->namespace}/{$this->moduleName}/Controllers/{$this->entityName}Controller.php";

        $data = 
"<?php 

namespace Modules\\{$this->moduleName}\\Controllers;

class {$this->moduleName}Controller extends \Ire\Base\Controllers\Controller {

}
";
        $fileHandler = new FileHandler($fileName, $data);
        $fileHandler->create();
    }

    private function createModel()
    {

        $fileName = "{$this->namespace}/{$this->moduleName}/Models/{$this->entityName}.php";
        $snake_name_table = Str::of($this->entityName)->snake();
        $varTable      = "$" . "table";
        $varfillable   = "$" . "fillable";
        $varHidden     = "$" . "hidden";

        $data = 
"<?php

namespace Modules\\{$this->moduleName}\\Models;

class {$this->entityName} extends \Ire\Crud\Models\EloquentModel {

    protected  $varTable   = '{$snake_name_table}';

    protected $varfillable = [];

    protected $varHidden   = [
        'deleted_at',
        'updated_at',
        'created_at'
    ];

}
        ";
        $fileHandler = new FileHandler($fileName, $data);
        $fileHandler->create();
    }

    private function createRelation()
    {

        $fileName = "{$this->namespace}/{$this->moduleName}/Relations/{$this->entityName}Relations.php";

        $data = 
"<?php 

namespace Modules\\{$this->moduleName}\\Relations;

trait {$this->entityName}Relations { 

    public function  your_relations() {}
    
}
";
        $fileHandler = new FileHandler($fileName, $data);
        $fileHandler->create();
    }
}
