<?php
// +----------------------------------------------------------------------
// | onezan-admin 快速开发的管理平台
// +----------------------------------------------------------------------
// | 官方网址：https://www.onezan.cn
// +----------------------------------------------------------------------
// | onezan团队 版权所有 开源版本可自由商用
// +----------------------------------------------------------------------
// | Author: 543885307@qq.com
// +----------------------------------------------------------------------
namespace app\service\domain\upgrade;

use app\model\upgrade\UpgradeToken;
use Core\service\BaseCoreService;
use think\facade\Db;

/**
 * 升级令牌服务
 * Class UpgradeTokenService
 * @package app\service\domain\upgrade
 */
class UpgradeTokenService extends BaseCoreService
{
    /**
     * 获取当前升级令牌
     * @return array|null
     */
    public function currentToken(): ?array
    {
        $token = UpgradeToken::where('status', 1)
            ->order('id', 'desc')
            ->find();

        if (!$token) {
            return null;
        }

        return $token->toArray();
    }

    /**
     * 保存升级令牌
     * @param string $code 升级令牌
     * @param string $domain 域名
     * @param string $type 升级类型
     * @param string $appCode 应用编码
     * @return array
     */
    public function save(string $code, string $domain, string $type = 'framework', string $appCode = ''): array
    {
        $exists = UpgradeToken::where('code', $code)->find();
        if ($exists) {
            $exists->domain = $domain;
            $exists->type = $type;
            $exists->industry_code = $appCode;
            $exists->status = 1;
            $exists->activated_at = date('Y-m-d H:i:s');
            $exists->update_time = date('Y-m-d H:i:s');
            $exists->save();
            return $exists->toArray();
        }

        $token = new UpgradeToken();
        $token->code = $code;
        $token->domain = $domain;
        $token->type = $type;
        $token->industry_code = $appCode;
        $token->status = 1;
        $token->activated_at = date('Y-m-d H:i:s');
        $token->create_time = date('Y-m-d H:i:s');
        $token->update_time = date('Y-m-d H:i:s');
        $token->save();

        return $token->toArray();
    }

    /**
     * 验证本地升级令牌
     * @param string $code 升级令牌
     * @param string $domain 域名
     * @return bool
     */
    public function validateLocal(string $code, string $domain): bool
    {
        $token = UpgradeToken::where('code', $code)
            ->where('status', 1)
            ->find();

        if (!$token) {
            return false;
        }

        if ($token->end_date) {
            $now = date('Y-m-d');
            $start = $token->start_date ?? '2000-01-01';
            $end = $token->end_date ?? '2099-12-31';
            if ($now < $start || $now > $end) {
                return false;
            }
        }

        return true;
    }

    /**
     * 更新远程升级令牌验证信息
     * @param string $code 升级令牌
     * @param array $remoteData 远程验证数据
     * @return void
     */
    public function updateRemoteVerification(string $code, array $remoteData): void
    {
        UpgradeToken::where('code', $code)->update([
            'remote_verified_at' => $remoteData['verified_at'] ?? date('Y-m-d H:i:s'),
            'start_date' => $remoteData['start_date'] ?? null,
            'end_date' => $remoteData['end_date'] ?? null,
            'update_time' => date('Y-m-d H:i:s'),
        ]);
    }

    /**
     * 获取远程升级令牌验证配置
     * @return array
     */
    public function remoteConfig(): array
    {
        $apiUrl = $this->app->config->get('upgrade.remote_api_url', '');
        $secret = $this->app->config->get('upgrade.remote_verify_secret', '');
        return [
            'api_url' => $apiUrl,
            'secret' => $secret,
        ];
    }
}