<?php

namespace App\Tests\unit\core\legacy\Statistics;

use App\Legacy\ModuleNameMapperHandler;
use App\Tests\UnitTester;
use BeanFactory;
use Codeception\Test\Unit;
use DateInterval;
use DateTime;
use Doctrine\ORM\EntityManagerInterface;
use Exception;
use aCase;
use App\Tests\_mock\Mock\core\legacy\Statistics\CaseDaysOpenMock;

/**
 * Class CaseDaysOpenTest
 * @package App\Tests
 */
class CaseDaysOpenTest extends Unit
{
    /**
     * @var UnitTester
     */
    protected $tester;

    /**
     * @var CaseDaysOpenMock
     */
    private $handler;

    /**
     * @throws Exception
     */
    protected function _before(): void
    {
        $projectDir = $this->tester->getProjectDir();
        $legacyDir = $this->tester->getLegacyDir();
        $legacySessionName = $this->tester->getLegacySessionName();
        $defaultSessionName = $this->tester->getDefaultSessionName();

        $legacyScope = $this->tester->getLegacyScope();

        $moduleNameMapper = new ModuleNameMapperHandler(
            $projectDir,
            $legacyDir,
            $legacySessionName,
            $defaultSessionName,
            $legacyScope
        );


        /** @var EntityManagerInterface $entityManager */
        $entityManager = $this->makeEmpty(
            EntityManagerInterface::class
        );

        $this->handler = new CaseDaysOpenMock(
            $projectDir,
            $legacyDir,
            $legacySessionName,
            $defaultSessionName,
            $legacyScope,
            $moduleNameMapper,
            $entityManager
        );
    }

    /**
     * Test Unsupported context module
     * @throws Exception
     */
    public function testUnsupportedContextModule(): void
    {
        $this->handler->reset();
        $dateString = $this->getPastDateString(2) . ' 12:00:00';
        $bean = $this->buildCase($dateString);

        $this->handler->setCase($bean);

        $result = $this->handler->getData(
            [
                'context' => [
                    'module' => 'accounts',
                    'id' => '12345',
                ]
            ]
        );

        static::assertNotNull($result);
        static::assertNotNull($result->getData());
        static::assertNotNull($result->getMetadata());
        static::assertIsArray($result->getData());
        static::assertIsArray($result->getMetadata());
        static::assertEquals('case-days-open', $result->getId());
        static::assertArrayHasKey('type', $result->getMetadata());
        static::assertEquals('single-value-statistic', $result->getMetadata()['type']);
        static::assertArrayHasKey('dataType', $result->getMetadata());
        static::assertEquals('varchar', $result->getMetadata()['dataType']);
        static::assertArrayHasKey('value', $result->getData());
        static::assertEquals('-', $result->getData()['value']);
    }

    /**
     * Test Open State Calculation
     * @throws Exception
     */
    public function testOpenStateCalculation(): void
    {
        $this->handler->reset();

        $closedDateString = $this->getPastDateString(2) . ' 19:00:00';
        $rows = [
            [
                'after_value_string' => 'Open',
                'first_update' => $closedDateString,
                'last_update' => $closedDateString,
            ],
        ];
        $this->handler->setMockQueryResult($rows);

        $dateString = $this->getPastDateTimeString(4);

        $bean = $this->buildCase($dateString);
        $bean->state = 'Open';

        $this->handler->setCase($bean);

        $result = $this->handler->getData(
            [
                'context' => [
                    'module' => 'cases',
                    'id' => '12345',
                ]
            ]
        );

        static::assertNotNull($result);
        static::assertNotNull($result->getData());
        static::assertNotNull($result->getMetadata());
        static::assertIsArray($result->getData());
        static::assertIsArray($result->getMetadata());
        static::assertArrayHasKey('value', $result->getData());
        static::assertEquals('4', $result->getData()['value']);
        static::assertEquals('case-days-open', $result->getId());
        static::assertArrayHasKey('type', $result->getMetadata());
        static::assertEquals('single-value-statistic', $result->getMetadata()['type']);
        static::assertArrayHasKey('dataType', $result->getMetadata());
        static::assertEquals('int', $result->getMetadata()['dataType']);
    }

    /**
     * Test Closed State Calculation
     * @throws Exception
     */
    public function testClosedStateCalculation(): void
    {
        $this->handler->reset();
        $closedDateString = $this->getPastDateString(2) . ' 19:00:00';
        $rows = [
            [
                'after_value_string' => 'Closed',
                'first_update' => $closedDateString,
                'last_update' => $closedDateString,
            ],
        ];
        $this->handler->setMockQueryResult($rows);

        $dateString = $this->getPastDateString(4) . ' 16:00:00';

        $bean = $this->buildCase($dateString);
        $bean->state = 'Closed';

        $this->handler->setCase($bean);

        $result = $this->handler->getData(
            [
                'context' => [
                    'module' => 'cases',
                    'id' => '12345',
                ]
            ]
        );

        static::assertNotNull($result);
        static::assertNotNull($result->getData());
        static::assertNotNull($result->getMetadata());
        static::assertIsArray($result->getData());
        static::assertIsArray($result->getMetadata());
        static::assertArrayHasKey('value', $result->getData());
        static::assertEquals('2', $result->getData()['value']);
        static::assertEquals('case-days-open', $result->getId());
        static::assertArrayHasKey('type', $result->getMetadata());
        static::assertEquals('single-value-statistic', $result->getMetadata()['type']);
        static::assertArrayHasKey('dataType', $result->getMetadata());
        static::assertEquals('int', $result->getMetadata()['dataType']);
    }

    /**
     * @param int $days
     * @return string
     * @throws Exception
     */
    protected function getPastDateString(int $days): string
    {
        $now = new DateTime();
        $now->sub(new DateInterval('P' . $days . 'D'));

        return $now->format('Y-m-d');
    }

    /**
     * @param int $days
     * @return string
     * @throws Exception
     */
    protected function getPastDateTimeString(int $days): string
    {
        $now = new DateTime();
        $now->sub(new DateInterval('P' . $days . 'DT1H'));

        return $now->format('Y-m-d H:i:s');
    }

    /**
     * @param string $dateString
     * @return aCase
     */
    protected function buildCase(string $dateString): aCase
    {
        /** @var aCase $bean */
        $bean = BeanFactory::newBean('Cases');
        $bean->date_entered = $dateString;
        $bean->id = '12355';

        return $bean;
    }
}
