<?php
namespace Tests\Unit;
use App\Enums\CheckStatusEnum;
use App\Format\UserFormat;
use App\Format\UserProfileFormat;
use App\Models\CheckUserProfile;
use App\Models\User;
use App\Models\UserProfile;
use App\Services\UserService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class UserTest extends TestCase
{
    use RefreshDatabase;
    public function testMakeUserDefaultName()
    {
        $phone = '18131699918';
        $name = makeNameFromPhone($phone);
        $this->assertEquals($name, '181***9918');
    }
    public function testRegUser()
    {
        $phone = '18131699918';
        $password = '1234567';
        $userService = new UserService();
        $userFormat = new UserFormat([
            'phone' => $phone,
            'password' => $password,
            'password_question_type' => 1,
            'password_question_answer' => '111',
        ]);
        $reg = $userService->reg($userFormat, true);
        $this->assertEquals($reg['user_id'], 1);
    }
    public function testLogin()
    {
        $phone = '18131699918';
        $password = '1234567';
        $userService = new UserService();
        $userFormat = new UserFormat([
            'phone' => $phone,
            'password' => $password,
            'password_question_type' => 1,
            'password_question_answer' => '111',
        ]);
        $userService->reg($userFormat, false);
        $vcode = $userService->getVcode($phone)['vcode'];
        $login = $userService->login($phone, md5($vcode . md5($password)));
        $this->assertEquals($login['user_id'], 1);
    }
    public function testUpdateUserInfo()
    {
        $userService = new UserService();
        
        $user = User::factory()->create();
        $updateUserData = [
            'name' => 'new-name',
            'id' => $user->id,
        ];
        $format = new UserFormat($updateUserData);
        $userService->changeUserInfo($format);
        $newUser = User::query()->find($user->id);
        $this->assertEquals('new-name', $newUser->name);
    }
    public function testCheckUserProfile()
    {
        $userService = new UserService();
        
        $user = User::factory()->create();
        $setData = [
            'name' => 'name',
            'sex' => 1,
            'phone' => '11818181818',
            'native_place' => '河北不告诉你',
            'marital_status' => 1,
            'has_children' => 1,
            'education' => 1,
            'residence_place' => 'alalal',
            'come_beijing_time' => date('Y-m-d', time()),
            'now_address' => 'aaa',
            'company_name' => 'pppp',
            'industry' => 1,
            'urgent_people' => 'aaaa',
            'urgent_people_phone' => '12121212121',
            'has_contribution' => 1,
            'contribution' => 'papapaa',
            'has_violated' => 1,
            'violated_detail' => 'ssksssns',
            'user_id' => $user->id,
            'identity' => 1,
        ];
        $format = new UserProfileFormat($setData);
        
        $checkProfile = $userService->userPutProfileCheck($format);
        $userService->checkUserProfile($checkProfile->id, CheckStatusEnum::CHECK_ADOPT, 'pass');
        
        $userProfile = UserProfile::query()->where('user_id', $user->id)->first();
               $this->assertEquals($userProfile->native_place, '河北不告诉你');
               $checkOldData = CheckUserProfile::query()->where('user_id', $user->id)->first();
        $this->assertNull($checkOldData);
    }
    public function testResetPassword()
    {
        $phone = '18131699918';
        $password = '1234567';
        $newPwd = '1212121';
        $userService = new UserService();
        $userFormat = new UserFormat([
            'phone' => $phone,
            'password' => $password,
            'password_question_type' => 1,
            'password_question_answer' => '111',
        ]);
        $userService->reg($userFormat, true);
        $userService->resetPassword($phone, 1, '111', $newPwd);
        
        $user = User::query()->where('phone', $phone)->first();
        $this->assertEquals(md5($newPwd), $user->password);
    }
    public function testSaveUserProfileAsDraft()
    {
        $user = User::factory()->create();
        $setData = [
            'name' => 'name',
            'sex' => 1,
            'phone' => '11818181818',
            'native_place' => '河北不告诉你',
            'marital_status' => 1,
            'has_children' => 1,
            'education' => 1,
            'residence_place' => 'alalal',
            'come_beijing_time' => date('Y-m-d', time()),
            'now_address' => 'aaa',
            'company_name' => 'pppp',
            'industry' => 1,
            'urgent_people' => 'aaaa',
            'urgent_people_phone' => '12121212121',
            'has_contribution' => 1,
            'contribution' => 'papapaa',
            'has_violated' => 1,
            'violated_detail' => 'ssksssns',
            'user_id' => $user->id,
            'identity' => 1,
        ];
        $format = new UserProfileFormat($setData);
        $userService = new UserService();
        $save = $userService->saveUserProfileAsDraft($format);
        $this->assertTrue($save);
        $profile = UserProfile::query()->where('id', 1)->first();
        $this->assertEquals($profile->check_status, CheckStatusEnum::DRAFT);
    }
}