<?php

namespace App\Console\Commands;

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

class GenerateCrudViews extends Command
{
    protected $signature = 'make:crud-views';
    protected $description = 'Generates the basic CRUD view structure.';
    protected $resources = ['alumnos', 'cepros', 'ugels', 'users'];

    public function handle()
    {
        $viewsPath = resource_path('views');

        // Crear carpetas layouts y components si no existen
        File::ensureDirectoryExists($viewsPath . '/layouts');
        File::ensureDirectoryExists($viewsPath . '/components/form');
        // ... crear otras subcarpetas de componentes si es necesario

        // Crear archivos de layout base (ejemplo)
        if (!File::exists($viewsPath . '/layouts/app.blade.php')) {
            File::put($viewsPath . '/layouts/app.blade.php', '<!DOCTYPE html>...');
        }
        // ... crear otros archivos de layout base

        // Crear archivos de componente base (ejemplo)
        if (!File::exists($viewsPath . '/components/alert.blade.php')) {
            File::put($viewsPath . '/components/alert.blade.php', '<div class="alert alert-{{ $type }}">{{ $message }}</div>');
        }
        if (!File::exists($viewsPath . '/components/form/input.blade.php')) {
            File::put($viewsPath . '/components/form/input.blade.php', '<div class="mb-3"><label for="{{ $name }}" class="form-label">{{ $label }}</label><input type="{{ $type ?? \'text\' }}" class="form-control" id="{{ $name }}" name="{{ $name }}" value="{{ old($name, $value ?? \'\') }}">@error($name) <div class="text-danger">{{ $message }}</div> @enderror</div>');
        }
        // ... crear otros archivos de componente base

        // Crear carpetas y archivos CRUD para cada recurso
        foreach ($this->resources as $resource) {
            $resourcePath = $viewsPath . '/' . $resource;
            File::ensureDirectoryExists($resourcePath);

            $files = ['index.blade.php', 'create.blade.php', 'show.blade.php', 'edit.blade.php', '_form.blade.php'];
            foreach ($files as $file) {
                if (!File::exists($resourcePath . '/' . $file)) {
                    File::put($resourcePath . '/' . $file, ''); // Crear archivo vacío
                    $this->info("Created: {$resourcePath}/{$file}");
                }
            }
        }

        $this->info('Basic CRUD view structure generated successfully.');
    }
}