<?php
namespace OfficeBrain\Bundle\ObSolrOperationBundle\Command;
use OfficeBrain\Bundle\SolrBundle\Command\SyncProductCommand;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

/**
 * Command to execute code for
 * @author 4220 <info@officebrain.com>
 */
class SyncAllProductCommand extends SyncProductCommand {

    const COMMAND_NAME = "office_brain:solr:sync:all-product";
    const CLIENT_ACTIVITY_ID = 7;
    /**
     * {@inheritdoc}
     */
    protected function configure()
    {

        $this
            ->setName(self::COMMAND_NAME)
            ->setDescription('Command to delete product data')
            ->setHelp(<<<EOT
This command helps you to migrate keyword from SOLR to SPHINX.
EOT
            );
    }
    /**
     * {@inheritdoc}
     */
    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $dbConnection = $this->getContainer()->get('doctrine')->getConnection();
        $sql = "SELECT count(id) as cnt FROM `client_activity_logs` WHERE client_activity_id=".self::CLIENT_ACTIVITY_ID." AND created BETWEEN '".date("Y-m-d")." 00:00:00' AND  '".date("Y-m-d")." 23:59:59'";

        $stmt = $dbConnection->prepare($sql);

        $executed = $stmt->execute();
        $returnObject = $stmt->fetch(\PDO::FETCH_OBJ);
        $command = '/usr/bin/php ' . $this->getContainer()->get('kernel')->getRootDir() . '/console '.parent::COMMAND_NAME.' --supplierId="all" ';

        if ($returnObject->cnt > 0) {
            passthru($command.' --culture="en"  &');
            passthru($command.' --culture="fr"  &');
            $output->writeln("All products are updating on ".date("Y-m-d"));
        } else {
            $output->writeln("There is no change in activity log on ".date("Y-m-d"));
        }
    }
}