<?php

namespace App\Commands;

use App\Services\Command;
use CzProject\GitPhp\Git;
use z4kn4fein\SemVer\Version;
use function Laravel\Prompts\select;
use function Laravel\Prompts\spin;
use function Laravel\Prompts\info;

class Release extends Command
{
    protected $signature = 'release';
    protected $description = 'Create a new release';

    public function handle(): void
    {

        $git = new Git();

        $repository = $git->open(getcwd());

        if ($repository->hasChanges()) {
            $this->error('You have uncommited changes!');
            return;
        }


        $tags = $repository->getTags();

        $versions = array_map(function (string $version) {
            return Version::parse($version);
        }, $tags);

        $sorted = Version::sort($versions);
        $latest = end($sorted);


        $nextPatchVersion = (string)$latest->getNextPatchVersion();
        $nextMinorVersion = (string)$latest->getNextMinorVersion();
        $nextMajorVersion = (string)$latest->getNextMajorVersion();

        $nextVersion = select(
            label: 'Bump Version to?',
            options: [
                $nextPatchVersion => sprintf('%s (Patch)', $nextPatchVersion),
                $nextMinorVersion => sprintf('%s (Minor)', $nextMinorVersion),
                $nextMajorVersion => sprintf('%s (Major)', $nextMajorVersion),
            ],
            default: 'owner'
        );

        $branchName = sprintf('release/%s', $nextVersion);

        $this->info(sprintf('Starting Release Process for Version %s', $nextVersion));


        spin(
            callback: function () use ($repository, $branchName, $nextVersion) {
                $repository->fetch('origin', ['--tags']);

                $repository->checkout('master');
                $repository->pull();

                $repository->checkout('develop');
                $repository->pull();

                $repository->createBranch($branchName, true);

                $repository->checkout('master');

                $repository->merge($branchName, ['--no-ff', '-m', sprintf('Merge release %s into master', $nextVersion)]);

                $repository->createTag($nextVersion, ['-m', sprintf('Release version %s', $nextVersion)]);

                $repository->push(['origin', 'master']);
                $repository->push(['origin', $nextVersion]);


                $repository->checkout('develop');
                $repository->pull();

                $repository->merge($branchName, ['--no-ff', '-m', sprintf('Merge release %s into develop', $nextVersion)]);

                $repository->push(['origin', 'develop']);

                $repository->removeBranch($branchName);

                // $repository->push('origin', ['--delete', $branchName]);

                $repository->push('origin', ['--all']);
                $repository->push('origin', ['--tags']);

                $repository->checkout('develop');

            },
            message: 'Creating the release, be patient...'
        );


        info('✓ Project released successfully. Cheers!');



    }
}
