<?php

namespace App\Http\Controllers\App;

use App\Http\Controllers\Controller;
use App\Http\Response\ResponseJson;
use App\Jobs\AddErrorLog;
use App\Models\AppVersion;
use App\Models\CountryPhoneCode;
use App\Models\FatLog;
use App\Models\IosVersion;
use App\Models\NewFatLog;
use App\Models\PublicConfig;
use App\Models\User;
use App\Models\Version;
use App\Models\Video;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class ConfigController extends Controller
{
    use ResponseJson;

    /**
     * 获取隐私协议
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View|\Illuminate\Http\JsonResponse
     */
    public function getPrivacy(Request $request)
    {
        try {
            $name = $request->get('name', PublicConfig::APP_PRIVACY);
            $language = $request->get('language');
            $sub_name = 'cn_privacy';
            if (strlen($language) > 0) {
                $name_arr = PublicConfig::NAME_ARR[$name] ?? [];
                $sub_name = $name_arr[$language] ?? '';
            }
            $privacy = PublicConfig::where('name', $name)->where('sub_name', $sub_name)->get();

            return $this->success('ok', $privacy);
        } catch (\Exception $e) {
            Log::error('getPrivacy:' . $e->getMessage());
            return $this->failure('获取失败');
        }
    }

    /**
     * 通过版本号获取数据
     * @param Request $request
     */
    public function getVersionStatus(Request $request)
    {
        $version = $request->get('version');
        //查找版本号状态
        $version_status = Version::where('version', $version)->where('type', Version::TYPE_APP)->value('status');
        return $this->success('ok', compact("version_status"));
    }

    public function getAppVersionConfig(Request $request)
    {
        $version = $request->get('version');
        $code = $request->get('code');
        $version_config = AppVersion::where('version', $version)->where('code', '>', $code)->orderByDesc('id')->first();
        if (!$version_config) {
            return $this->failure('当前已是最新版本');
        }
        return $this->success('ok', $version_config);
    }

    public function getIosVersionConfig(Request $request)
    {
        $platform = $request->get('platform');
        $version_config = IosVersion::where('platform', $platform)->where('status', 1)->orderByDesc('id')->first();
        return $this->success('ok', $version_config);
    }

    public function getAppVersionConfigV1(Request $request)
    {
        $platform = $request->header("isServer");
        $query = AppVersion::query()->orderByDesc('id')->where('is_prod', 1);
        if ($platform == AppVersion::PLATFORM_SERVER) {
            $query->where("platform", AppVersion::PLATFORM_SERVER);
        } else {
            $query->where("platform", AppVersion::PLATFORM_USER);
        }
        $version_config = $query->first();
        return $this->success('ok', $version_config);
    }

    public function getMpShareConfig(Request $request)
    {
        try {
            $user = auth()->user();
            $sub_type = $request->get('sub_type');
            $id = $request->get('id');
            switch ($sub_type) {
                case 1:
                    $fat_log = NewFatLog::where('user_id', $user->id)->orderByDesc('id')->first();
                    $config = [
                        'title' => '友福健康',
                        'desc' => '测量记录',
                        'app_id' => config('wechat.mini_program.init_id'),
                        //                        'path' =>"pages/sub_page/shareReport?user_id=".$user->id.'&id='.$fat_log->id,
                        'path' => 'pages/tabBar/home',
                        'image_path' => "https://image.fulllinkai.com/202311/27/fd67ab8389d4ff1c5832ddda72cd0dc1.png",
                        'web_page_url' => config('app.url') . '/page',
                        'ticket' => true,
                        'type' => 0,
                        'scene' => 0,
                        'sub_type' => $sub_type
                    ];
                    break;
                case 2:
                    $video = Video::find($id);
                    $config = [
                        'title' => $video->title,
                        'desc' => $video->desc,
                        'img_url' => $video->img_url,
                        'video_url' => $video->url,
                        'sub_type' => $sub_type
                    ];
                    break;
                default:
                    $config = [];
                    break;
            }


            return $this->success('ok', $config);
        } catch (\Exception $e) {
            AddErrorLog::dispatch('getMpShareConfig file:' . $e->getFile() . ' line:' . $e->getLine() . ' message:' . $e->getMessage())->onQueue('health');
            return $this->failure("获取失败");
        }
    }

    public function getCountryPhoneCode(Request $request)
    {
        try {
            $language = $request->get('language', 0);
            $language_config = [
                User::LANGUAGE_CH => 'zh_name',
                User::LANGUAGE_EN => 'en_name',
                User::LANGUAGE_TW => 'tw_name'
            ];
            if (!isset($language_config[$language])) {
                return $this->failure('获取失败');
            }
            $list = CountryPhoneCode::select(DB::raw("$language_config[$language] as name"), DB::raw("phone_code"))
                ->orderByRaw("phone_code = '86' desc")->orderByDesc('phone_code')->get();
            return $this->success('ok', $list);
        } catch (\Exception $e) {
            AddErrorLog::dispatch('getMpShareConfig file:' . $e->getFile() . ' line:' . $e->getLine() . ' message:' . $e->getMessage())->onQueue('health');
            return $this->failure("获取失败");
        }
    }

    public function getDmaProcess(Request $request)
    {
        $process_config = [
            "before" => [
                [
                    "title" => "1、填写用户信息",
                    "sub" => [
                        [
                            "key" => "send_use_call_video",
                            "sub_title" => "转发了【体脂秤使用教程】",
                            "status" => 0,
                            "type" => 5,
                            "log" => [],
                        ]
                    ]
                ]
            ],
            "in" => [
                [
                    "title" => "1、引导用户使用体脂秤、手环",
                    "sub" => [
                        [
                            "key" => "send_use_call_video",
                            "sub_title" => "转发了【体脂秤使用教程】",
                            "status" => 0,
                            "type" => 5,
                            "log" => [],
                        ],
                        [
                            "key" => "send_use_band_video",
                            "sub_title" => "转发了【手环使用教程】",
                            "status" => 1,
                            "type" => 6,
                            "log" => [],
                        ],
                    ]
                ],
                [
                    "title" => "2、餐单设置及执行",
                    "sub" => [
                        [
                            "key" => "set_guide_template",
                            "sub_title" => "编辑用户餐单模板",
                            "status" => 0,
                            "type" => 7,
                            "log" => [],
                        ],
                        [
                            "key" => "save_guide",
                            "sub_title" => "保存用户餐单",
                            "status" => 0,
                            "type" => 0,
                            "log" => [],
                        ],
                        [
                            "key" => "send_save_guide_msg",
                            "sub_title" => "系统发送【餐单待审核】通知",
                            "status" => 0,
                            "type" => 0,
                            "log" => [],
                        ],
                    ]
                ],
            ],
            "after" => [

            ],
        ];
        return $this->success("ok", $process_config);
    }
}
