<?php

namespace App\Base\Framework\Command;

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: 'startup:status',
    description: 'Used by the Docker healthcheck system to signal when the container is healthy.',
)]
class StartupStatusCommand extends Command
{
    private EntityManagerInterface $entityManager;

    public function __construct(EntityManagerInterface $entityManager)
    {
        parent::__construct();
        $this->entityManager = $entityManager;
    }

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

        $ready = true;
        $messengerTableExists = $this->doesMessengerTableExist();

        if (false === $messengerTableExists) {
            $ready = false;
        }

        return (true === $ready) ? Command::SUCCESS : Command::FAILURE;
    }

    private function doesMessengerTableExist()
    {
        return $this->entityManager->getConnection()
            ->createSchemaManager()
            ->tablesExist('messenger_messages');
    }
}
