const test = require('node:test')
const assert = require('node:assert/strict')

const wechatService = require('../src/services/wechatService')
const pocketbaseService = require('../src/services/pocketbaseService')
const jwtService = require('../src/services/jwtService')
const userService = require('../src/services/userService')

test('authenticateWechatUser 首次注册默认写入游客类型', async (t) => {
    const originGetWxOpenId = wechatService.getWxOpenId
    const originListUsersByFilter = pocketbaseService.listUsersByFilter
    const originCreateUser = pocketbaseService.createUser
    const originSignAccessToken = jwtService.signAccessToken
    const originGetCompanyByCompanyId = pocketbaseService.getCompanyByCompanyId

    wechatService.getWxOpenId = async () => 'openid-register'
    pocketbaseService.listUsersByFilter = async () => []
    pocketbaseService.createUser = async (payload) => ({
        id: 'pb-1',
        created: '2026-03-19T00:00:00Z',
        updated: '2026-03-19T00:00:00Z',
        ...payload,
    })
    pocketbaseService.getCompanyByCompanyId = async () => null
    jwtService.signAccessToken = () => 'token-1'

    t.after(() => {
        wechatService.getWxOpenId = originGetWxOpenId
        pocketbaseService.listUsersByFilter = originListUsersByFilter
        pocketbaseService.createUser = originCreateUser
        pocketbaseService.getCompanyByCompanyId = originGetCompanyByCompanyId
        jwtService.signAccessToken = originSignAccessToken
    })

    const result = await userService.authenticateWechatUser({
        users_wx_code: 'code-1',
    })

    assert.equal(result.status, 'register_success')
    assert.equal(result.is_info_complete, false)
    assert.equal(result.user.users_type, '游客')
})

test('updateWechatUserProfile 首次完整补充资料时升级为注册用户', async (t) => {
    const originGetWxOpenId = wechatService.getWxOpenId
    const originGetWxPhoneNumber = wechatService.getWxPhoneNumber
    const originListUsersByFilter = pocketbaseService.listUsersByFilter
    const originUpdateUser = pocketbaseService.updateUser
    const originGetCompanyByCompanyId = pocketbaseService.getCompanyByCompanyId

    wechatService.getWxOpenId = async () => 'openid-profile'
    wechatService.getWxPhoneNumber = async () => '13800138000'
    pocketbaseService.listUsersByFilter = async (filter) => {
        if (filter.includes('users_wx_openid')) {
            return [
                {
                    id: 'pb-2',
                    users_id: 'U2001',
                    users_type: '游客',
                    users_name: '',
                    users_phone: '',
                    users_picture: '',
                    users_wx_openid: 'openid-profile',
                    created: '2026-03-19T00:00:00Z',
                    updated: '2026-03-19T00:00:00Z',
                },
            ]
        }
        return []
    }
    pocketbaseService.updateUser = async (_id, payload) => ({
        id: 'pb-2',
        users_id: 'U2001',
        users_wx_openid: 'openid-profile',
        users_type: payload.users_type || '游客',
        users_name: payload.users_name,
        users_phone: payload.users_phone,
        users_picture: payload.users_picture,
        created: '2026-03-19T00:00:00Z',
        updated: '2026-03-19T00:10:00Z',
    })
    pocketbaseService.getCompanyByCompanyId = async () => null

    t.after(() => {
        wechatService.getWxOpenId = originGetWxOpenId
        wechatService.getWxPhoneNumber = originGetWxPhoneNumber
        pocketbaseService.listUsersByFilter = originListUsersByFilter
        pocketbaseService.updateUser = originUpdateUser
        pocketbaseService.getCompanyByCompanyId = originGetCompanyByCompanyId
    })

    const result = await userService.updateWechatUserProfile('openid-profile', {
        users_name: '张三',
        users_phone_code: 'phone-code-1',
        users_picture: 'https://example.com/a.png',
    })

    assert.equal(result.status, 'update_success')
    assert.equal(result.user.users_type, '注册用户')
    assert.equal(result.user.users_phone_masked, '138****8000')
})

test('updateWechatUserProfile 非首次补充资料时不覆盖已确定类型', async (t) => {
    const originGetWxOpenId = wechatService.getWxOpenId
    const originGetWxPhoneNumber = wechatService.getWxPhoneNumber
    const originListUsersByFilter = pocketbaseService.listUsersByFilter
    const originUpdateUser = pocketbaseService.updateUser
    const originGetCompanyByCompanyId = pocketbaseService.getCompanyByCompanyId

    wechatService.getWxOpenId = async () => 'openid-registered'
    wechatService.getWxPhoneNumber = async () => '13900139000'
    pocketbaseService.listUsersByFilter = async (filter) => {
        if (filter.includes('users_wx_openid')) {
            return [
                {
                    id: 'pb-3',
                    users_id: 'U2002',
                    users_type: '注册用户',
                    users_name: '老用户',
                    users_phone: '13800138000',
                    users_picture: 'https://example.com/old.png',
                    users_wx_openid: 'openid-registered',
                    created: '2026-03-19T00:00:00Z',
                    updated: '2026-03-19T00:00:00Z',
                },
            ]
        }
        return []
    }
    pocketbaseService.updateUser = async (_id, payload) => ({
        id: 'pb-3',
        users_id: 'U2002',
        users_wx_openid: 'openid-registered',
        users_type: '注册用户',
        users_name: payload.users_name,
        users_phone: payload.users_phone,
        users_picture: payload.users_picture,
        created: '2026-03-19T00:00:00Z',
        updated: '2026-03-19T00:10:00Z',
    })
    pocketbaseService.getCompanyByCompanyId = async () => null

    t.after(() => {
        wechatService.getWxOpenId = originGetWxOpenId
        wechatService.getWxPhoneNumber = originGetWxPhoneNumber
        pocketbaseService.listUsersByFilter = originListUsersByFilter
        pocketbaseService.updateUser = originUpdateUser
        pocketbaseService.getCompanyByCompanyId = originGetCompanyByCompanyId
    })

    const result = await userService.updateWechatUserProfile('openid-registered', {
        users_name: '新名字',
        users_phone_code: 'phone-code-2',
        users_picture: 'https://example.com/new.png',
    })

    assert.equal(result.user.users_type, '注册用户')
})

test('refreshWechatToken 返回新的 token', async (t) => {
    const originListUsersByFilter = pocketbaseService.listUsersByFilter
    const originSignAccessToken = jwtService.signAccessToken

    pocketbaseService.listUsersByFilter = async () => [
        {
            id: 'pb-4',
            users_id: 'U3001',
            users_wx_openid: 'openid-refresh',
        },
    ]
    jwtService.signAccessToken = () => 'refresh-token'

    t.after(() => {
        pocketbaseService.listUsersByFilter = originListUsersByFilter
        jwtService.signAccessToken = originSignAccessToken
    })

    const result = await userService.refreshWechatToken('openid-refresh')

    assert.equal(result.token, 'refresh-token')
})
