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

import { createAuthSession, resolveAuth, resolveIntegrationTokenAuth } from './authRepository.js';
import { mockDb } from '../test/mockDb.js';

test('resolveAuth returns null when session is missing', async () => {
  const { calls } = mockDb({ rowCount: 0, rows: [] });

  const result = await resolveAuth('hashed-token', 'raw-token');

  assert.equal(result, null);
  assert.equal(calls.length, 1);
  assert.match(calls[0].text, /FROM auth_sessions/);
  assert.deepEqual(calls[0].params, ['hashed-token']);
});

test('resolveIntegrationTokenAuth maps auth context and records last use', async () => {
  const { calls } = mockDb(
    {
      rowCount: 1,
      rows: [
        {
          integration_token_id: 'token-1',
          integration_token_user_id: 'user-1',
          integration_token_workspace_id: 12,
          integration_token_name: 'CLI token',
          integration_token_token_hash: 'hashed-token',
          integration_token_token_prefix: 'flpt_1234',
          integration_token_scope: 'read_write',
          integration_token_last_used_at: null,
          integration_token_expires_at: '2026-05-01T00:00:00.000Z',
          integration_token_revoked_at: null,
          integration_token_created_at: '2026-04-01T00:00:00.000Z',
          user_id_row: 'user-1',
          user_email: 'owner@example.com',
          user_password_hash: null,
          user_name: 'Owner',
          user_created_at: '2026-04-01T00:00:00.000Z',
          workspace_id_row: 12,
          workspace_name: 'Sanctuary',
          workspace_workspace_type: 'rescue',
          workspace_billing_email: 'billing@example.com',
          workspace_billing_plan: 'rescue_free',
          workspace_created_at: '2026-04-01T00:00:00.000Z',
          workspace_updated_at: '2026-04-02T00:00:00.000Z',
          membership_id_row: 'member-1',
          membership_workspace_id: 12,
          membership_user_id: 'user-1',
          membership_invite_email: 'owner@example.com',
          membership_name: 'Owner',
          membership_role: 'owner',
          membership_accepted_at: '2026-04-01T00:00:00.000Z',
          membership_created_at: '2026-04-01T00:00:00.000Z',
        },
      ],
    },
    { rowCount: 1, rows: [] },
  );

  const result = await resolveIntegrationTokenAuth('hashed-token', 'raw-token');

  assert.ok(result);
  assert.equal(result?.authType, 'integration_token');
  assert.equal(result?.token, 'raw-token');
  assert.equal(result?.integrationToken?.scope, 'read_write');
  assert.equal(result?.workspace.id, 12);
  assert.equal(calls.length, 2);
  assert.match(calls[0].text, /FROM integration_tokens/);
  assert.match(calls[1].text, /UPDATE integration_tokens/);
  assert.deepEqual(calls[1].params, ['token-1']);
});

test('createAuthSession persists the provided token hash and expiry', async () => {
  const { calls } = mockDb({
    rowCount: 1,
    rows: [
      {
        id: 'session-1',
        user_id: 'user-1',
        active_workspace_id: 5,
        token_hash: 'hashed-token',
        expires_at: '2026-05-01T00:00:00.000Z',
        created_at: '2026-04-14T00:00:00.000Z',
      },
    ],
  });

  const session = await createAuthSession('user-1', 5, 'hashed-token', '2026-05-01T00:00:00.000Z');

  assert.equal(session?.id, 'session-1');
  assert.deepEqual(calls[0].params, ['user-1', 5, 'hashed-token', '2026-05-01T00:00:00.000Z']);
});
