<?php

namespace Database\Seeders;

use App\Models\Environments\Type;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Storage;

class TypeSeeder extends Seeder
{
    public function run(): void
    {
        $images = [
            'moodle' => 'moodle.svg',
            'workplace' => 'workplace.svg',
            'goodle' => 'goodle.svg',
            'wordpress' => 'wordpress.svg',
        ];

        foreach ($images as $shortname => $filename) {
            $imagePath = "images_static/environments/{$filename}";

            // Asegurar que la imagen existe en storage
            $storagePath = storage_path("app/public/{$imagePath}");
            $publicPath = public_path($imagePath);

            // Si existe en public pero no en storage, copiarla
            if (File::exists($publicPath) && !Storage::disk('public')->exists($imagePath)) {
                Storage::disk('public')->put($imagePath, File::get($publicPath));
            }

            // Si no existe en ningún lado, crear el directorio (la imagen debería estar en el repo)
            if (!Storage::disk('public')->exists($imagePath)) {
                Storage::disk('public')->makeDirectory('images_static/environments');
            }

            $typeData = [
                'name' => match($shortname) {
                    'moodle' => 'Moodle LMS',
                    'workplace' => 'Moodle Workplace',
                    'goodle' => 'Goodle',
                    'wordpress' => 'Wordpress',
                    default => ucfirst($shortname),
                },
                'description' => "Entorno desarrollado en " . match($shortname) {
                    'moodle' => 'Moodle',
                    'workplace' => 'Workplace',
                    'goodle' => 'Goodle',
                    'wordpress' => 'Wordpress',
                    default => ucfirst($shortname),
                },
                'image' => $imagePath,
            ];

            Type::firstOrCreate(
                ['shortname' => $shortname],
                $typeData
            );
        }
    }
}
