<?php

declare(strict_types=1);

namespace Aeon\Automation\Tests\Integration\Console\Command;

use Aeon\Automation\Console\AeonApplication;
use Aeon\Automation\Console\Command\ChangelogReleaseUnreleased;
use Aeon\Automation\Tests\Double\HttpRequestStub;
use Aeon\Automation\Tests\Integration\Console\CommandTestCase;
use Aeon\Automation\Tests\Mother\GitHub\GitHubResponseMother;
use Aeon\Automation\Tests\Mother\GitHub\SHAMother;
use Aeon\Automation\Tests\Mother\ResponseMother;
use Aeon\Calendar\Gregorian\DateTime;
use Aeon\Calendar\Gregorian\GregorianCalendarStub;
use Aeon\Calendar\Gregorian\TimeZone;
use Github\Client;
use Symfony\Component\Cache\Adapter\ArrayAdapter;
use Symfony\Component\Console\Output\ConsoleOutput;
use Symfony\Component\Console\Tester\CommandTester;

final class ChangelogReleaseUnreleasedTest extends CommandTestCase
{
    public function test_changing_unreleased_into_new_release_in_changelog() : void
    {
        $client = Client::createWithHttpClient($httpClient = $this->httpClient(
            new HttpRequestStub('GET', '/repos/aeon-php/automation/contents/CHANGELOG.md', ResponseMother::jsonSuccess([
                'content' => \base64_encode(<<<'CHANGELOG'
## [Unreleased] - 2021-01-01

### Changed
  - [#3](http://api.github.com) - **Release 1.1.0 Title - 3** - [@user_login](http//github.com/user_login)
  - [#2](http://api.github.com) - **Release 1.1.0 Title - 2** - [@user_login](http//github.com/user_login)
  - [#1](http://api.github.com) - **Release 1.1.0 Title - 1** - [@user_login](http//github.com/user_login)

Generated by [Automation](https://github.com/aeon-php/automation)
CHANGELOG),
            ])),
            new HttpRequestStub('GET', '/repos/aeon-php/automation/tags', ResponseMother::jsonSuccess([
                GitHubResponseMother::tag('0.1.0', $tag100SHA = SHAMother::random()),
            ])),
            new HttpRequestStub('GET', '/repos/aeon-php/automation/releases', ResponseMother::jsonSuccess([
                GitHubResponseMother::release(1, '0.1.0'),
            ])),
            new HttpRequestStub('PATCH', '/repos/aeon-php/automation/releases/1', ResponseMother::jsonSuccess([])),
            new HttpRequestStub('PUT', '/repos/aeon-php/automation/contents/CHANGELOG.md', ResponseMother::jsonSuccess([]))
        ));

        $command = new ChangelogReleaseUnreleased(\getenv('AUTOMATION_ROOT_DIR'));
        $command->setGithub($client);
        $command->setCalendar(new GregorianCalendarStub(TimeZone::UTC(), DateTime::fromString('2021-01-15')));
        $command->setHttpCache(new ArrayAdapter());
        $command->setGitHubCache(new ArrayAdapter());

        $application = new AeonApplication();
        $application->add($command);

        $commandTester = new CommandTester($application->get(ChangelogReleaseUnreleased::getDefaultName()));

        $commandTester->setInputs(['verbosity' => ConsoleOutput::VERBOSITY_VERY_VERBOSE]);

        $commandTester->execute(
            ['project' => 'aeon-php/automation', 'changelog-file-path' => 'CHANGELOG.md', 'release-name' => '0.1.0', '--github-release-update' => true, '--github-file-changelog-update'],
            ['verbosity' => ConsoleOutput::VERBOSITY_VERBOSE]
        );

        $this->assertStringContainsString('Changelog - Release Unreleased', $commandTester->getDisplay());

        $this->assertStringContainsString('Generated by [Automation](https://github.com/aeon-php/automation)', $commandTester->getDisplay());

        $this->assertStringContainsString('## [0.1.0] - 2021-01-15', $commandTester->getDisplay());
        $this->assertStringNotContainsString('## [Unreleased] - 2021-01-01', $commandTester->getDisplay());
        $this->assertStringContainsString('Updating release description...', $commandTester->getDisplay());
        $this->assertStringContainsString('Release description updated', $commandTester->getDisplay());

        $this->assertSame(0, $commandTester->getStatusCode());
    }
}
