#!/usr/bin/env php
<?php

declare(strict_types = 1);

use Interactiv4\ComposerJsonFile\Api\ComposerJsonFileInterface;
use Interactiv4\ComposerJsonFile\ComposerJsonFile;
use Interactiv4\SemanticVersioning\NextVersionIncrease;
use Interactiv4\SemanticVersioning\TagSelfInterface;

include_once __DIR__ . '/abstract-tag-self';

$composerJsonFile = new ComposerJsonFile($path);
$nextVersionIncrease = $composerJsonFile->getSubNode(
    ComposerJsonFileInterface::EXTRA,
    NextVersionIncrease::COMPOSER_JSON_EXTRA_KEY
);

if (!isset($nextVersionIncrease)) {
    fwrite(STDOUT, 'Cannot tag automatically, next version increase is not set.' . PHP_EOL);
    exit(0);
}

$className = 'Interactiv4\SemanticVersioning\TagSelf' . ucfirst(strtolower($nextVersionIncrease));

if (!class_exists($className)) {
    fwrite(STDERR, "Cannot tag automatically, class $className does not exist." . PHP_EOL);
    exit(1);
}

$tagSelf = new $className($path);
if (!$tagSelf instanceof TagSelfInterface) {
    fwrite(STDERR, "Cannot tag automatically, class $className is not instance of TagSelfInterface." . PHP_EOL);
    exit(1);
}

$tagSelf->run(...$argv);
