const test = require('node:test');
const assert = require('assert');
const { app } = require('../app');

test('GET /help-requests should return all help requests', async () => {
  const response = await app.inject({
    method: 'GET',
    url: '/help-requests'
  });
  
  assert.strictEqual(response.statusCode, 200);
  assert.strictEqual(response.headers['content-type'], 'application/json; charset=utf-8');
});

test('POST /help-requests should create a new help request', async () => {
  const newRequest = {
    title: 'Test Help Request',
    description: 'This is a test help request',
    valueChf: 50.0
  };

  const response = await app.inject({
    method: 'POST',
    url: '/help-requests',
    payload: newRequest
  });

  assert.strictEqual(response.statusCode, 201);
  assert.strictEqual(response.headers['content-type'], 'application/json; charset=utf-8');
});

test('POST /help-requests should validate required fields', async () => {
  const response = await app.inject({
    method: 'POST',
    url: '/help-requests',
    payload: {}
  });

  assert.strictEqual(response.statusCode, 400);
});

test('POST /help-requests should validate title length', async () => {
  const response = await app.inject({
    method: 'POST',
    url: '/help-requests',
    payload: {
      title: 'Sh',
      description: 'This is a test help request',
      valueChf: 50.0
    }
  });

  assert.strictEqual(response.statusCode, 400);
});

test('POST /help-requests should validate description length', async () => {
  const response = await app.inject({
    method: 'POST',
    url: '/help-requests',
    payload: {
      title: 'Test Help Request',
      description: 'Short',
      valueChf: 50.0
    }
  });

  assert.strictEqual(response.statusCode, 400);
});

test('POST /help-requests should validate valueChf is positive', async () => {
  const response = await app.inject({
    method: 'POST',
    url: '/help-requests',
    payload: {
      title: 'Test Help Request',
      description: 'This is a test help request',
      valueChf: -10.0
    }
  });

  assert.strictEqual(response.statusCode, 400);
});

test('PUT /help-requests/:id should update a help request', async () => {
  const newRequest = {
    title: 'Test Help Request',
    description: 'This is a test help request',
    valueChf: 50.0
  };

  // First create a request
  const createResponse = await app.inject({
    method: 'POST',
    url: '/help-requests',
    payload: newRequest
  });

  assert.strictEqual(createResponse.statusCode, 201);

  const requestId = createResponse.json().id;

  // Then update it
  const updateResponse = await app.inject({
    method: 'PUT',
    url: `/help-requests/${requestId}`,
    payload: {
      title: 'Updated Title',
      description: 'Updated Description',
      valueChf: 75.0,
      status: 'in_progress'
    }
  });

  assert.strictEqual(updateResponse.statusCode, 200);
});

test('PUT /help-requests/:id should validate title length', async () => {
  const newRequest = {
    title: 'Test Help Request',
    description: 'This is a test help request',
    valueChf: 50.0
  };

  // First create a request
  const createResponse = await app.inject({
    method: 'POST',
    url: '/help-requests',
    payload: newRequest
  });

  assert.strictEqual(createResponse.statusCode, 201);

  const requestId = createResponse.json().id;

  // Then try to update with invalid title
  const updateResponse = await app.inject({
    method: 'PUT',
    url: `/help-requests/${requestId}`,
    payload: {
      title: 'Sh',
      description: 'Updated Description',
      valueChf: 75.0,
      status: 'in_progress'
    }
  });

  assert.strictEqual(updateResponse.statusCode, 400);
});

test('PUT /help-requests/:id should validate description length', async () => {
  const newRequest = {
    title: 'Test Help Request',
    description: 'This is a test help request',
    valueChf: 50.0
  };

  // First create a request
  const createResponse = await app.inject({
    method: 'POST',
    url: '/help-requests',
    payload: newRequest
  });

  assert.strictEqual(createResponse.statusCode, 201);

  const requestId = createResponse.json().id;

  // Then try to update with invalid description
  const updateResponse = await app.inject({
    method: 'PUT',
    url: `/help-requests/${requestId}`,
    payload: {
      title: 'Updated Title',
      description: 'Short',
      valueChf: 75.0,
      status: 'in_progress'
    }
  });

  assert.strictEqual(updateResponse.statusCode, 400);
});

test('PUT /help-requests/:id should validate valueChf is positive', async () => {
  const newRequest = {
    title: 'Test Help Request',
    description: 'This is a test help request',
    valueChf: 50.0
  };

  // First create a request
  const createResponse = await app.inject({
    method: 'POST',
    url: '/help-requests',
    payload: newRequest
  });

  assert.strictEqual(createResponse.statusCode, 201);

  const requestId = createResponse.json().id;

  // Then try to update with invalid valueChf
  const updateResponse = await app.inject({
    method: 'PUT',
    url: `/help-requests/${requestId}`,
    payload: {
      title: 'Updated Title',
      description: 'Updated Description',
      valueChf: -10.0,
      status: 'in_progress'
    }
  });

  assert.strictEqual(updateResponse.statusCode, 400);
});

test('DELETE /help-requests/:id should delete a help request', async () => {
  const newRequest = {
    title: 'Test Help Request',
    description: 'This is a test help request',
    valueChf: 50.0
  };

  // First create a request
  const createResponse = await app.inject({
    method: 'POST',
    url: '/help-requests',
    payload: newRequest
  });

  assert.strictEqual(createResponse.statusCode, 201);

  const requestId = createResponse.json().id;

  // Then delete it
  const deleteResponse = await app.inject({
    method: 'DELETE',
    url: `/help-requests/${requestId}`
  });

  assert.strictEqual(deleteResponse.statusCode, 200);
});