<?php

namespace GriffinForms\Includes\Packages;

use GriffinForms\Config;
use GriffinForms\Traits\Singleton;

class PackageCompatibility extends \GriffinForms\Format
{
    use Singleton;

    public function getCompatibilityState(array $package, bool $is_present): string
    {
        $package_id = sanitize_key((string) ($package['package_id'] ?? ''));

        if ($package_id === 'core') {
            return 'compatible';
        }

        if (!$is_present) {
            return 'unknown';
        }

        $minimum_core_version = (string) ($package['minimum_core_version'] ?? '');
        $package_version = (string) ($package['package_version'] ?? '');

        if ($minimum_core_version !== '' && version_compare(Config::VERSION, $minimum_core_version, '<')) {
            return 'incompatible_core_too_old';
        }

        if ($package_version !== '' && version_compare($package_version, '0.0.0.0', '<=')) {
            return 'incompatible_package_too_old';
        }

        return 'compatible';
    }
}
