<?php

use Openguru\OpenCartFramework\Application;
use Openguru\OpenCartFramework\Config\Settings;
use Openguru\OpenCartFramework\Support\Utils;

if (! function_exists('table')) {
    function db_table(string $name): string
    {
        $prefix = Application::getInstance()->getConfigValue('database.prefix');

        return $prefix . $name;
    }
}

if (! function_exists('column')) {
    function db_column($column): string
    {
        if (strpos($column, '.') !== false) {
            [$table, $column] = explode('.', $column, 2);

            if ($table === '' || $column === '') {
                throw new InvalidArgumentException('Invalid column reference: ' . $column);
            }

            return db_table($table) . '.' . $column;
        }

        return $column;
    }
}

if (! function_exists('configs_path')) {
    function configs_path(string $path = ''): string
    {
        return BP_PHAR_BASE_PATH . '/configs/' . $path;
    }
}

if (! function_exists('resources_path')) {
    function resources_path(string $path = ''): string
    {
        return BP_PHAR_BASE_PATH . '/resources/' . $path;
    }
}

if (! function_exists('database_path')) {
    function database_path(string $path = ''): string
    {
        return BP_PHAR_BASE_PATH . '/database/' . $path;
    }
}

if (! function_exists('base_path')) {
    function base_path(string $path = ''): string
    {
        return BP_PHAR_BASE_PATH . '/' . $path;
    }
}

if (! function_exists('config')) {
    function config(string $key, $default = null)
    {
        return Application::getInstance()->get(Settings::class)->get($key, $default);
    }
}

if (! function_exists('dd')) {
    function dd(): void
    {
        Utils::dd(func_get_args());
    }
}

if (! function_exists('env')) {
    function env(string $key, $default = null)
    {
        $value = getenv($key) !== false
            ? getenv($key)
            : ($_ENV[$key] ?? $default);

        $lower = strtolower($value);

        if ($lower === 'true' || $lower === '(true)') {
            return true;
        }

        if ($lower === 'false' || $lower === '(false)') {
            return false;
        }

        if ($lower === 'null' || $lower === '(null)') {
            return null;
        }

        if ($lower === 'empty' || $lower === '(empty)') {
            return '';
        }

        return $value;
    }
}

if (! function_exists('module_version')) {
    function module_version(): string
    {
        static $version = null;

        if ($version !== null) {
            return $version;
        }

        $versionFile = BP_PHAR_BASE_PATH . '/version.txt';
        if (is_readable($versionFile)) {
            $raw = trim((string) file_get_contents($versionFile));
            if ($raw !== '') {
                return $version = $raw;
            }
        }

        $composerFile = BP_PHAR_BASE_PATH . '/composer.json';
        if (is_readable($composerFile)) {
            $composer = json_decode((string) file_get_contents($composerFile), true);
            if (is_array($composer) && ! empty($composer['version'])) {
                return $version = (string) $composer['version'];
            }
        }

        return $version = 'unknown';
    }
}
