<?php


namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Models\AppSwitch;
use App\Util;
use Illuminate\Http\Request;

class ConfigController extends Controller
{
    public function index(Request $request)
    {

        $channel = $request->input('channel');
        if(!isset($channel)){
            $channel = $request->input('Channel');
        }
        if(!isset($channel)){
            $pack=Util::getPackageByURL();
            if(isset($pack)){
                $channel=$pack['channel'];
                if($channel!=103)$channel=-1;
            }
        }else if(strstr("PackageName",$channel)){
            $channel=explode("&",$channel)[0];
        }


        $defaultSwitches = AppSwitch::query()->where('channel', -1)
            ->pluck('status', 'name');
        $channelSwitches = [];
        if ($channel && $channel != -1) {
            $channelSwitches = AppSwitch::query()->where('channel', $channel)
                ->pluck('status', 'name');
        }
        $switches = array_merge(
            json_decode(json_encode($defaultSwitches), true),
            json_decode(json_encode($channelSwitches), true)
        );

        return apiReturnSuc([
            'switches' => $switches,
        ]);
    }
}
