import { test, expect } from '@playwright/test';

test.describe('Smoke Tests - Backend Refactor', () => {
    test('Login, Exercises, and Session Flow', async ({ request }) => {
        const email = `smoke_${Date.now()}@example.com`;
        const password = 'password123';

        // 1. Register
        const registerRes = await request.post('http://localhost:3201/api/auth/register', {
            data: { email, password }
        });
        expect(registerRes.ok()).toBeTruthy();
        const registerBody = await registerRes.json();
        // Check new structure
        expect(registerBody.success).toBe(true);
        expect(registerBody.data).toHaveProperty('token');
        const token = registerBody.data.token;

        // 2. Get Exercises
        const exercisesRes = await request.get('http://localhost:3201/api/exercises', {
            headers: { Authorization: `Bearer ${token}` }
        });
        expect(exercisesRes.ok()).toBeTruthy();
        const exercisesBody = await exercisesRes.json();
        expect(exercisesBody.success).toBe(true);
        expect(Array.isArray(exercisesBody.data)).toBe(true);

        // 3. Create Session
        const sessionRes = await request.post('http://localhost:3201/api/sessions', {
            headers: { Authorization: `Bearer ${token}` },
            data: {
                id: "test-session-" + Date.now(),
                startTime: new Date().toISOString(),
                sets: []
            }
        });
        expect(sessionRes.ok()).toBeTruthy();
        const sessionBody = await sessionRes.json();
        expect(sessionBody.success).toBe(true);
        expect(sessionBody.data).toHaveProperty('id');

        // 4. Get Active Session
        const activeRes = await request.get('http://localhost:3201/api/sessions/active', {
            headers: { Authorization: `Bearer ${token}` }
        });
        expect(activeRes.ok()).toBeTruthy();
        const activeBody = await activeRes.json();
        expect(activeBody.success).toBe(true);
        expect(activeBody.data).toHaveProperty('session');
        expect(activeBody.data.session.id).toBe(sessionBody.data.id);
    });
});
