<?php

namespace App\Services;

use App\Constants\HttpStatus;
use App\Models\PhoneCode;
use App\Models\User;
use Exception;

class PhoneCodeService extends BaseService
{
    public static string $MODEL = PhoneCode::class;

    public static function getWhere(array $search = []): array
    {
        $where = [];
        if (isset($search['phone']) && !empty($search['phone'])) {
            $where[] = ['phone', '=', $search['phone']];
        }

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

    public static function findOne(array $search = []): ?PhoneCode
    {
        return self::$MODEL::where(static::getWhere($search))->first();
    }

    /**
     * @param $phone
     * @param $code
     * @param $memberId
     * @param $visitorId
     * @return void
     * @throws Exception
     */
    public static function verify($phone, $code, $memberId, $visitorId): void
    {
        $search = ['phone' => $phone, 'code' => $code];
        $phoneCode = static::$MODEL::where(static::getWhere($search))->orderByDesc('id')->first();
        if (!$phoneCode) throw new Exception("验证码错误", HttpStatus::CUSTOM_ERROR);
        $time = time();
        if ($phoneCode->ext < $time) throw new Exception("验证码过期", HttpStatus::CUSTOM_ERROR);
        $phoneCode->status = 1;
        $phoneCode->save();
        $user = UserService::findOne(['member_id' => $memberId]);
        if (!$user) throw new Exception('用户不存在', HttpStatus::CUSTOM_ERROR);
        $registerIp = request()->ip();
        $user->register_ip = $registerIp;
        $user->phone = $phone;
        $user->visitor_id = $visitorId;
        if (User::where('phone', $phone)->where('member_id', '!=', $memberId)->exists()) {
            User::where('phone', $phone)->where('member_id', '!=', $memberId)->update(['status' => 1]);
            $user->status = 1;
            if ($user->admin_note) $user->admin_note .= "\n";
            $user->admin_note .= "手机号重复";
        }
        if (User::where('register_ip', $registerIp)->where('member_id', '!=', $memberId)->exists()) {
            User::where('register_ip', $registerIp)->where('member_id', '!=', $memberId)->update(['status' => 1]);
            $user->status = 1;
            if ($user->admin_note) $user->admin_note .= "\n";
            $user->admin_note .= "IP重复";
        }
        if (User::where('visitor_id', $visitorId)->where('member_id', '!=', $memberId)->exists()) {
            User::where('visitor_id', $visitorId)->where('member_id', '!=', $memberId)->update(['status' => 1]);
            $user->status = 1;
            if ($user->admin_note) $user->admin_note .= "\n";
            $user->admin_note .= "访客ID重复";
        }

        $user->save();
    }

    public static function add($phone, $code): PhoneCode
    {
        $time = time();
        $ext = $time + 600;
        $data = [
            'phone' => $phone,
            'code' => $code,
            'ext' => $ext,
        ];
        return static::$MODEL::create($data);
    }
}