<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redis;
use Illuminate\Support\Facades\Validator;

class SystemHealthController extends Controller
{
    // Redis键
    const REDIS_KEY = 'system:health';

    /**
     * 创建一个新的控制器实例
     *
     * @return void
     */
    public function __construct()
    {
        // 设置默认时区，以确保所有时间戳处理都使用系统配置的时区
        // 如果config/app.php中没有设置时区，默认使用'Asia/Shanghai'
        $timezone = config('app.timezone', 'Asia/Shanghai');
        date_default_timezone_set($timezone);
    }

    /**
     * 显示系统健康状态配置页面
     */
    public function index()
    {
        // 从Redis获取系统健康配置
        $healthConfig = $this->getHealthConfig();

        // 返回视图
        return view('admin.system_health.index', [
            'healthConfig' => $healthConfig
        ]);
    }

    /**
     * 更新系统健康状态配置
     */
    public function update(Request $request)
    {
        // 验证输入
        $validator = Validator::make($request->all(), [
            'status' => 'required|integer|in:0,1',
            'pay_status' => 'required|integer|in:0,1',
            'pay_start_time' => 'nullable|required_if:pay_status,0',
            'pay_end_time' => 'nullable|required_if:pay_status,0',
            'cashout_status' => 'required|integer|in:0,1',
            'cashout_start_time' => 'nullable|required_if:cashout_status,0',
            'cashout_end_time' => 'nullable|required_if:cashout_status,0',
            'notice' => 'nullable|string|max:500',
        ]);

        if ($validator->fails()) {
            return redirect()->back()->withErrors($validator)->withInput();
        }

        // 构建健康状态配置
        $healthConfig = [
            'status' => (int)$request->input('status'),
            'notice' => $request->input('notice', ''),
        ];

        // 处理支付维护配置
        if ((int)$request->input('pay_status') === 0) {
            // 支付维护模式 - 配置时间
            // 将本地时间字符串转换为时间戳，保留原始时区设置
            $payStartTime = new \DateTime($request->input('pay_start_time'));
            $payEndTime = new \DateTime($request->input('pay_end_time'));

            $healthConfig['pay'] = [
                'st' => $payStartTime->getTimestamp(),
                'ed' => $payEndTime->getTimestamp(),
                'status' => 0
            ];
        } else {
            // 支付正常模式
            $healthConfig['pay'] = 1;
        }

        // 处理提现维护配置
        if ((int)$request->input('cashout_status') === 0) {
            // 提现维护模式 - 配置时间
            // 将本地时间字符串转换为时间戳，保留原始时区设置
            $cashoutStartTime = new \DateTime($request->input('cashout_start_time'));
            $cashoutEndTime = new \DateTime($request->input('cashout_end_time'));

            $healthConfig['cashout'] = [
                'st' => $cashoutStartTime->getTimestamp(),
                'ed' => $cashoutEndTime->getTimestamp(),
                'status' => 0
            ];
        } else {
            // 提现正常模式
            $healthConfig['cashout'] = 1;
        }

        // 存储到Redis
        Redis::set(self::REDIS_KEY, json_encode($healthConfig));

        return redirect()->route('admin.system_health.index')->with('success', '系统健康状态配置已更新');
    }

    /**
     * 从Redis获取系统健康配置
     */
    protected function getHealthConfig()
    {
        $config = Redis::get(self::REDIS_KEY);

        if (!$config) {
            // 默认配置
            return [
                'status' => 1,
                'pay' => 1,
                'cashout' => 1,
                'notice' => '',
            ];
        }

        return json_decode($config, true);
    }
}
