const test = require('node:test');
const assert = require('node:assert');
const { pool } = require('../db/connection.js');

// Mock the database pool
const mockPool = {
  query: (sql, params) => {
    if (sql.includes('INSERT INTO users')) {
      return [{ insertId: 1 }];
    }
    if (sql.includes('SELECT id, email, password_hash FROM users')) {
      return [[{ id: 1, email: 'test@example.com', password_hash: '$2a$12$...' }]];
    }
    return [];
  }
};

// Replace the actual pool with mock
const originalPool = pool;
pool.query = mockPool.query;

test('register should handle valid input', async () => {
  const req = {
    body: {
      email: 'test@example.com',
      password: 'password123',
      displayName: 'Test User'
    }
  };
  const res = {
    status: (code) => {
      res.statusCode = code;
      return res;
    },
    json: (data) => {
      res.data = data;
    }
  };

  await require('../routes/auth').default.post('/register', req, res);
  assert.strictEqual(res.statusCode, 201);
  assert.ok(res.data.token);
});

test('login should handle valid credentials', async () => {
  const req = {
    body: {
      email: 'test@example.com',
      password: 'password123'
    }
  };
  const res = {
    status: (code) => {
      res.statusCode = code;
      return res;
    },
    json: (data) => {
      res.data = data;
    }
  };

  await require('../routes/auth').default.post('/login', req, res);
  assert.strictEqual(res.statusCode, 200);
  assert.ok(res.data.token);
});

test('register should reject invalid email', async () => {
  const req = {
    body: {
      email: 'invalid-email',
      password: 'password123',
      displayName: 'Test User'
    }
  };
  const res = {
    status: (code) => {
      res.statusCode = code;
      return res;
    },
    json: (data) => {
      res.data = data;
    }
  };

  await require('../routes/auth').default.post('/register', req, res);
  assert.strictEqual(res.statusCode, 400);
});

test('login should reject invalid credentials', async () => {
  const req = {
    body: {
      email: 'nonexistent@example.com',
      password: 'wrongpassword'
    }
  };
  const res = {
    status: (code) => {
      res.statusCode = code;
      return res;
    },
    json: (data) => {
      res.data = data;
    }
  };

  await require('../routes/auth').default.post('/login', req, res);
  assert.strictEqual(res.statusCode, 401);
});

test('register should handle duplicate email', async () => {
  const mockPoolWithDup = {
    query: (sql, params) => {
      if (sql.includes('INSERT INTO users')) {
        const err = new Error('Duplicate entry');
        err.code = 'ER_DUP_ENTRY';
        throw err;
      }
      return [];
    }
  };

  pool.query = mockPoolWithDup.query;

  const req = {
    body: {
      email: 'test@example.com',
      password: 'password123',
      displayName: 'Test User'
    }
  };
  const res = {
    status: (code) => {
      res.statusCode = code;
      return res;
    },
    json: (data) => {
      res.data = data;
    }
  };

  await require('../routes/auth').default.post('/register', req, res);
  assert.strictEqual(res.statusCode, 409);

  // Restore the original pool
  pool.query = originalPool.query;
});

// Restore the original pool
pool.query = originalPool;