<?php declare(strict_types=1);

require_once __DIR__ . '/../../common/QrbException.php';

final class QrbFilePaths
{
    // relative path to configuration directory
    public  const CONFIGURATION_DIR = "/../../../cfg";
    // relative path to data directory that contains profile directories
    public  const DATA_DIR          = "/../../../data";
    // input directory below profile directory
    private const INPUT_DIR         = "/input";
    // output directory below profile directory
    private const OUTPUT_DIR        = "/output";

    // file name of the environment file
    private const ENVIRONMENT_FILE_NAME = "QrBillEnvironment.json";
    // name of the profile configuration file
    private const PROFILE_CFG_FILE_NAME = "ProfileConfiguration.json";

    private static function createPath(string $path) : void
    {
        if (!file_exists($path) && !mkdir($path, 0777, true))
            throw new QrbException(static::class . " - Unable to create path '{$path}'",
                                   "exception.upload.unableToCreatePath",
                                   "{$path}");
    }

    // Returns the absolute path to the cfg sub directory (without trailing slash).
    public static function getCfgPath() : string
    {
        $path = QrbFiles::fixPath(__DIR__ . self::CONFIGURATION_DIR);
        self::createPath($path);
        return $path;
    }

    // Returns the absolute path to the data sub directory (without trailing slash).
    public static function getDataPath() : string
    {
        $path = QrbFiles::fixPath(__DIR__ . self::DATA_DIR);
        self::createPath($path);
        return $path;
    }

    // Returns the absolute path to the given profile (without trailing slash).
    public static function getProfilePath(string $profile) : string
    {
        $path = self::getDataPath() . "/" . $profile;
        self::createPath($path);
        return $path;
    }

    // Returns the relative path to the given profile (without trailing slash).
    public static function getRelativeProfilePath(string $profile) : string
    {
        $path = QrbFiles::fixPath(self::DATA_DIR . "/" . $profile );
        return $path;
    }
    
    // Returns the absolute input path of the given profile (without trailing slash).
    public static function getInputPath(string $profile) : string
    {
        $path = self::getProfilePath($profile) . self::INPUT_DIR;
        self::createPath($path);
        return $path;
    }
    
    // Returns the relative input path of the given profile (without trailing slash).
    public static function getRelativeInputPath(string $profile) : string
    {
        return self::getRelativeProfilePath($profile) . self::INPUT_DIR;
    }

    // Returns the absolute output path of the given profile (without trailing slash).
    public static function getOutputPath(string $profile) : string
    {
        $path = self::getProfilePath($profile) . self::OUTPUT_DIR;
        self::createPath($path);
        return $path;
    }

    // Returns the relative output path of the given profile (without trailing slash).
    private static function getRelativeOutputPath(string $profile) : string
    {
        return self::getRelativeProfilePath($profile) . self::OUTPUT_DIR;
    }

    // Returns the absolute output path of the given run of the given profile (without trailing slash).
    public static function getOutputPathOfRun(string $profile, string $run) : string
    {
        $path = self::getOutputPath($profile) . "/" . $run;
        self::createPath($path);
        return $path;
    }

    // Returns the relative output path of the given run of the given profile (without trailing slash).
    public static function getRelativeOutputPathOfRun(string $profile, string $run) : string
    {
        return self::getRelativeOutputPath($profile) . "/" . $run;
    }

    public static function getEnvironmentFullFileName() : string
    {
        return self::getCfgPath() . "/" . self::ENVIRONMENT_FILE_NAME;
    }

    // Returns the full file name of the configuration file of the given profile.
    public static function getConfigurationFullFileName($profile) : string
    {
        return self::getProfilePath($profile) . "/" . self::PROFILE_CFG_FILE_NAME;
    }
}

?>
