// spec: specs/gymflow-test-plan.md
// seed: tests/seed.spec.ts

import { test, expect } from './fixtures';

test.describe('User & System Management', () => {
  test('AI Coach - Send a Message', async ({ page, createUniqueUser }) => {
    // 1. Log in as a regular user.
    const user = await createUniqueUser();
    await page.goto('/');
    await page.getByLabel('Email').fill(user.email);
    await page.getByLabel('Password').fill(user.password);
    await page.getByRole('button', { name: 'Login' }).click();

    // Handle First Time Password Change if it appears
    try {
        await expect(page.getByRole('heading', { name: /Change Password/i }).or(page.getByText('Free Workout'))).toBeVisible({ timeout: 5000 });
        if (await page.getByRole('heading', { name: /Change Password/i }).isVisible()) {
             await page.getByLabel('New Password').fill('StrongNewPass123!');
             await page.getByRole('button', { name: /Save|Change/i }).click();
        }
    } catch (e) {
        // Ignore timeout
    }
    await expect(page.getByText('Free Workout')).toBeVisible();

    // 2. Navigate to the 'AI Coach' section.
    await page.getByRole('button', { name: 'AI Coach' }).click();

    // 3. Type a message into the input field (e.g., 'What's a good workout for chest?').
    const message = "What's a good workout for chest?";
    await page.getByRole('textbox', { name: 'Ask about workouts...' }).fill(message);

    // 4. Click 'Send' button.
    // Using filter to find the button with no text (icon only) which is the send button in the chat interface
    await page.getByRole('button').filter({ hasText: /^$/ }).click();

    // Expected Results: User's message appears in the chat.
    await expect(page.getByText(message)).toBeVisible();

    // Expected Results: AI Coach responds with relevant advice.
    // We expect a response to appear. Since AI response takes time, we wait for it.
    // We can check for a common response starter or just that another message bubble appears.
    // Assuming the response is long, we can check for a part of it or just non-empty text that is NOT the user message.
    // Or check if the "thinking" state goes away if implemented.
    // Here we'll just wait for any text that contains "chest" or "workout" that isn't the input prompt.
    // But better to check for element structure if possible.
    // Based on manual execution, we saw "That's a great goal!"
    await expect(page.getByText(/chest/i).nth(1)).toBeVisible(); // Just ensuring related content appeared
  });
});
