import request from 'supertest';
import app from '../src/app'; // Adjust the path as necessary
import { Task } from '../src/models/Task'; // Adjust the path as necessary

describe('Task API', () => {
  let taskId: string;

  beforeAll(async () => {
    // Setup code to run before all tests, e.g., connecting to the database
  });

  afterAll(async () => {
    // Cleanup code to run after all tests, e.g., disconnecting from the database
  });

  it('should create a new task', async () => {
    const response = await request(app)
      .post('/api/tasks') // Adjust the endpoint as necessary
      .send({
        title: 'Test Task',
        description: 'This is a test task',
        completed: false,
      });

    expect(response.status).toBe(201);
    expect(response.body).toHaveProperty('id');
    taskId = response.body.id; // Store the task ID for later tests
  });

  it('should retrieve the created task', async () => {
    const response = await request(app).get(`/api/tasks/${taskId}`); // Adjust the endpoint as necessary

    expect(response.status).toBe(200);
    expect(response.body).toHaveProperty('id', taskId);
    expect(response.body).toHaveProperty('title', 'Test Task');
  });

  it('should update the task', async () => {
    const response = await request(app)
      .put(`/api/tasks/${taskId}`) // Adjust the endpoint as necessary
      .send({
        title: 'Updated Test Task',
        description: 'This is an updated test task',
        completed: true,
      });

    expect(response.status).toBe(200);
    expect(response.body).toHaveProperty('title', 'Updated Test Task');
  });

  it('should delete the task', async () => {
    const response = await request(app).delete(`/api/tasks/${taskId}`); // Adjust the endpoint as necessary

    expect(response.status).toBe(204);
  });
});