<?php

namespace App\Command;

use App\Entity\HardwareType;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;

#[AsCommand(
    name: 'app:load-hardware-types',
    description: 'Carga los tipos de hardware iniciales en la base de datos'
)]
class LoadHardwareTypesCommand extends Command
{
    public function __construct(
        private readonly EntityManagerInterface $entityManager
    ) {
        parent::__construct();
    }

    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        $io = new SymfonyStyle($input, $output);

        $hardwareTypes = [
            ['code' => 'boo', 'name' => 'BIOS', 'description' => 'Sistema básico de entrada/salida'],
            ['code' => 'mod', 'name' => 'Motherboard', 'description' => 'Placa base del sistema'],
            ['code' => 'boa', 'name' => 'Board', 'description' => 'Placa base o tarjeta madre'],
            ['code' => 'cpu', 'name' => 'CPU', 'description' => 'Procesador central'],
            ['code' => 'mem', 'name' => 'Memory', 'description' => 'Memoria RAM'],
            ['code' => 'ide', 'name' => 'IDE Controller', 'description' => 'Controlador IDE'],
            ['code' => 'vga', 'name' => 'Graphics Card', 'description' => 'Tarjeta gráfica'],
            ['code' => 'dis', 'name' => 'Disk', 'description' => 'Disco de almacenamiento'],
            ['code' => 'usb', 'name' => 'USB Controller', 'description' => 'Controlador USB'],
            ['code' => 'cdr', 'name' => 'CD/DVD', 'description' => 'Unidad de CD/DVD'],
            ['code' => 'net', 'name' => 'Network Card', 'description' => 'Tarjeta de red'],
        ];

        $repository = $this->entityManager->getRepository(HardwareType::class);
        $created = 0;
        $updated = 0;

        foreach ($hardwareTypes as $typeData) {
            // Buscar por nombre primero
            $hardwareType = $repository->findOneBy(['name' => $typeData['name']]);
            
            if (!$hardwareType) {
                $hardwareType = new HardwareType();
                $hardwareType->setName($typeData['name']);
                $created++;
                $io->writeln(sprintf('Creando tipo: %s (%s)', $typeData['name'], $typeData['code']));
            } else {
                $updated++;
                $io->writeln(sprintf('Actualizando tipo: %s (%s)', $typeData['name'], $typeData['code']));
            }

            $hardwareType->setDescription($typeData['description']);
            $hardwareType->setCode($typeData['code']);
            $this->entityManager->persist($hardwareType);
        }

        $this->entityManager->flush();

        $io->success(sprintf(
            'Tipos de hardware cargados exitosamente. Creados: %d, Actualizados: %d',
            $created,
            $updated
        ));

        return Command::SUCCESS;
    }
} 