<?php

declare(strict_types=1);

namespace App\Services\SupportCenter;

/**
 * 将 DBErp 配置的 stable/rc 映射为 Support Center Open Host release_channel。
 */
final class HostSystemUpdateReleaseChannelResolver
{
    public const CONFIG_STABLE = 'stable';

    public const CONFIG_RC = 'rc';

    /** Support Center 稳定通道（与 Phase 1 Open Host 约定一致）。 */
    public const OPEN_HOST_STABLE = 'production';

    /** Support Center 预发布通道。 */
    public const OPEN_HOST_RC = 'rc';

    public function configuredChannel(): string
    {
        $raw = strtolower(trim((string) config('dberp.system_update.release_channel', self::CONFIG_STABLE)));

        return $raw === self::CONFIG_RC ? self::CONFIG_RC : self::CONFIG_STABLE;
    }

    public function resolveOpenHostChannel(?string $override = null): string
    {
        if ($override !== null && trim($override) !== '') {
            return $this->mapToOpenHostChannel(trim($override));
        }

        return $this->mapToOpenHostChannel($this->configuredChannel());
    }

    public function mapToOpenHostChannel(string $channel): string
    {
        $normalized = strtolower(trim($channel));

        return match ($normalized) {
            self::CONFIG_RC, self::OPEN_HOST_RC, 'release-candidate', 'beta' => self::OPEN_HOST_RC,
            self::CONFIG_STABLE, self::OPEN_HOST_STABLE, 'prod', 'production', 'stable' => self::OPEN_HOST_STABLE,
            default => self::OPEN_HOST_STABLE,
        };
    }
}
