import test from 'node:test'
import assert from 'node:assert/strict'

import {
  buildAdminCloudtentaclesLoginResult,
  buildAdminCloudtentaclesPersistedSessionPayload,
  buildAdminCloudtentaclesSessionSummary,
  buildAdminCloudtentaclesValidateResult,
  resolveAdminCloudtentaclesCredentialPayload,
  resolveAdminCloudtentaclesSessionPayload,
} from './payloads.js'

test('resolveAdminCloudtentaclesCredentialPayload merges payload and saved source defaults', () => {
  assert.deepEqual(
    resolveAdminCloudtentaclesCredentialPayload(
      {
        baseUrl: ' https://override.example.com ',
        phone: ' 13800138000 ',
        deviceId: ' custom-device ',
      },
      {
        savedSource: {
          baseUrl: 'https://saved.example.com',
          username: 'saved-user',
          password: 'saved-pass',
          phone: '13900000000',
          deviceId: 'saved-device',
          deviceType: 2,
        },
      },
    ),
    {
      sourceKey: 'default',
      baseUrl: 'https://override.example.com',
      username: 'saved-user',
      password: 'saved-pass',
      phone: '13800138000',
      deviceId: 'custom-device',
      deviceType: 2,
    },
  )
})

test('resolveAdminCloudtentaclesSessionPayload merges payload source and persisted session defaults', () => {
  assert.deepEqual(
    resolveAdminCloudtentaclesSessionPayload(
      {
        token: ' payload-token ',
      },
      {
        savedSource: {
          baseUrl: 'https://saved.example.com',
          deviceId: 'saved-device',
          deviceType: 3,
        },
        persistedSession: {
          token: 'session-token',
          deviceId: 'session-device',
          deviceType: 5,
        },
      },
    ),
    {
      sourceKey: 'default',
      baseUrl: 'https://saved.example.com',
      token: 'payload-token',
      deviceId: 'saved-device',
      deviceType: 3,
    },
  )
})

test('buildAdminCloudtentaclesPersistedSessionPayload prefers explicit overrides', () => {
  assert.deepEqual(
    buildAdminCloudtentaclesPersistedSessionPayload(
      {
        token: ' token-1 ',
        baseUrl: ' https://cloud.example.com ',
        username: ' session-user ',
        phone: ' 13900000000 ',
        loggedInAt: ' 2026-05-04T12:00:00.000Z ',
        deviceId: ' session-device ',
        deviceType: 9,
      },
      {
        username: 'saved-user',
        phone: '13800138000',
        deviceId: 'saved-device',
        deviceType: 3,
      },
    ),
    {
      token: 'token-1',
      baseUrl: 'https://cloud.example.com',
      username: 'saved-user',
      phone: '13800138000',
      loggedInAt: '2026-05-04T12:00:00.000Z',
      deviceId: 'saved-device',
      deviceType: 3,
    },
  )
})

test('buildAdminCloudtentaclesSessionSummary masks token and reports permission count', () => {
  assert.deepEqual(
    buildAdminCloudtentaclesSessionSummary(
      {
        token: 'abcdef1234567890',
        permissions: ['asset', 'sku'],
      },
      {
        token: 'persisted-token',
      },
    ),
    {
      sourceKey: 'default',
      tokenMasked: 'abcdef****567890',
      permissionCount: 2,
      permissions: ['asset', 'sku'],
      persisted: true,
    },
  )
})

test('buildAdminCloudtentaclesLoginResult assembles login response payload', () => {
  assert.deepEqual(
    buildAdminCloudtentaclesLoginResult(
      {
        token: 'abcdef1234567890',
        baseUrl: ' https://cloud.example.com ',
        username: ' test-user ',
        phone: '13812345678',
        loggedInAt: ' 2026-05-04T12:00:00.000Z ',
        responseMessage: ' success ',
        permissions: ['asset'],
        userInfo: { id: 1 },
        asset: { balance: 10 },
      },
      {
        token: 'persisted-token',
      },
    ),
    {
      sourceKey: 'default',
      baseUrl: 'https://cloud.example.com',
      username: 'test-user',
      phoneMasked: '138****5678',
      loggedInAt: '2026-05-04T12:00:00.000Z',
      responseMessage: 'success',
      token: 'abcdef1234567890',
      session: {
        sourceKey: 'default',
        tokenMasked: 'abcdef****567890',
        permissionCount: 1,
        permissions: ['asset'],
        persisted: true,
      },
      userInfo: { id: 1 },
      asset: { balance: 10 },
    },
  )
})

test('buildAdminCloudtentaclesValidateResult assembles validate response payload', () => {
  assert.deepEqual(
    buildAdminCloudtentaclesValidateResult(
      {
        token: 'abcdef1234567890',
        baseUrl: ' https://cloud.example.com ',
        loggedInAt: ' 2026-05-04T12:00:00.000Z ',
        permissions: ['asset', 'sku'],
        userInfo: { id: 2 },
        asset: { balance: 20 },
      },
      {},
    ),
    {
      sourceKey: 'default',
      baseUrl: 'https://cloud.example.com',
      loggedInAt: '2026-05-04T12:00:00.000Z',
      session: {
        sourceKey: 'default',
        tokenMasked: 'abcdef****567890',
        permissionCount: 2,
        permissions: ['asset', 'sku'],
        persisted: false,
      },
      userInfo: { id: 2 },
      asset: { balance: 20 },
    },
  )
})

test('buildAdminCloudtentaclesSessionSummary preserves sourceKey', () => {
  assert.deepEqual(
    buildAdminCloudtentaclesSessionSummary(
      {
        token: 'abcdef1234567890',
        permissions: [],
      },
      {},
      'account2',
    ),
    {
      sourceKey: 'account2',
      tokenMasked: 'abcdef****567890',
      permissionCount: 0,
      permissions: [],
      persisted: false,
    },
  )
})
