<?php

namespace App\Services;

use App\Constants\HttpStatus;
use App\Constants\Util;
use App\Models\ActivityReward;
use App\Models\Config;
use App\Models\Role;
use App\Models\UserSession;
use App\Models\User;
use Exception;

class ActivityRewardService extends BaseService
{

    public static string $MODEL = ActivityReward::class;

    /**
     * @description: 查询单条数据
     * @param array $search
     * @return ActivityReward|null
     */
    public static function findOne(array $search): ActivityReward|null
    {
        return static::$MODEL::where(static::getWhere($search))->first();
    }

    public static function getWhere(array $search = []): array
    {
        $where = [];

        if (isset($search['id']) && !empty($search['id'])) {
            $where[] = ['id', '=', $search['id']];
        }

        if (isset($search['status']) && $search['status'] != '') {
            $where[] = ['status', '=', $search['status']];
        }

        if (isset($search['type']) && $search['type'] != '') {
            $where[] = ['type', '=', $search['type']];
        }

        if (isset($search['title']) && !empty($search['title'])) {
            $where[] = ['title', 'like', "%{$search['title']}%"];
        }

        if (isset($search['start_time']) && !empty($search['start_time'])) {
            if (is_array($search['start_time'])) {
                if (count($search['start_time']) == 2) {
                    $where[] = ['start_time', $search['start_time'][0], $search['start_time'][1]];
                } else {
                    $where[] = ['start_time', '=', $search['start_time'][0]];
                }
            } else {
                $where[] = ['start_time', '=', $search['start_time']];
            }
        }

        if (isset($search['end_time']) && !empty($search['end_time'])) {
            if (is_array($search['end_time'])) {
                if (count($search['end_time']) == 2) {
                    $where[] = ['end_time', $search['end_time'][0], $search['end_time'][1]];
                } else {
                    $where[] = ['end_time', '=', $search['end_time'][0]];
                }
            } else {
                $where[] = ['end_time', '=', $search['end_time']];
            }
        }
        return $where;
    }

    public static function deleteAll(array $search = []): bool
    {
        $count = static::$MODEL::where(static::getWhere($search))->delete();
        if ($count < 1) throw new Exception('删除失败', HttpStatus::CUSTOM_ERROR);
        return true;
    }

    /**
     * Update or create
     * @param array $params
     * @return bool
     * @throws Exception
     */
    public static function submit(array $params = []): bool
    {
        if (isset($params['start_time']))
            $params['start_time'] = strtotime($params['start_time']);
        if (isset($params['end_time']))
            $params['end_time'] = strtotime($params['end_time']);
        if (isset($params['detail_image']))
            $params['detail_image'] = Util::replacePartInUrl($params['detail_image']);
        if (isset($params['pc_image']))
            $params['pc_image'] = Util::replacePartInUrl($params['pc_image']);
        if (isset($params['detail_banner']))
            $params['detail_banner'] = Util::replacePartInUrl($params['detail_banner']);
        if (isset($params['pc_banner']))
            $params['pc_banner'] = Util::replacePartInUrl($params['pc_banner']);

        if (!empty($params['id'])) {
            $info = static::findOne(['id' => $params['id']]);
            if (!$info) throw new Exception("操作失败", HttpStatus::CUSTOM_ERROR);
            $info->update($params);
        } else {
            static::$MODEL::create($params);
        }
        return true;
    }


    public static function promotionalActivity($chatId): array
    {
        $keyboard = [];
        $url = Config::where('field','activity_home_url')->first()->val;
        $url.="?member_id=".$chatId;
        $keyboard[] = [
            ['text'=>'活动首页','url'=>$url]
        ];
        $msg = [];
        $msg['chat_id'] = $chatId;
        $msg['text'] = "点击下方按钮进入活动首页";
        $msg['reply_markup'] = json_encode(['inline_keyboard' => $keyboard]);

        return $msg;
    }

    //F1bet娱乐更多玩法(足球首页)
    public static function footballUrl($chatId): array
    {
        $token = UserSession::setToken($chatId);
        $is_bind = User::where('member_id', $chatId)->value('account') ? 1 : 0;
        $keyboard = [];
        $url = Config::where('field','football_home_url ')->first()->val;
        $url.="?token=".$token."&is_bind=".$is_bind;
        $keyboard[] = [
            ['text'=>'F1bet娱乐首页','url'=>$url]
        ];
        $msg = [];
        $msg['chat_id'] = $chatId;
        $msg['text'] = "点击下方按钮进入F1bet娱乐首页";
        $msg['reply_markup'] = json_encode(['inline_keyboard' => $keyboard]);

        return $msg;
    }
}
