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

test.describe('VII. AI Coach Features', () => {

    async function handleFirstLogin(page: any) {
        try {
            const heading = page.getByRole('heading', { name: /Change Password/i });
            const dashboard = page.getByText('Free Workout');

            await expect(heading.or(dashboard)).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();
                await expect(dashboard).toBeVisible();
            }
        } catch (e) {
            if (await page.getByText('Free Workout').isVisible()) return;
        }
    }

    test('7.1 AI Coach - Basic Conversation & Markdown', async ({ page, createUniqueUser }) => {
        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();
        await handleFirstLogin(page);

        await page.getByRole('button', { name: 'AI Coach' }).click();

        const input = page.getByPlaceholder(/Ask your AI coach/i).or(page.getByRole('textbox', { name: 'Ask about workouts...' }));
        await input.fill('How to do a pushup?');
        await page.getByRole('button').filter({ hasText: /^$/ }).last().click();

        await expect(page.locator('.prose').first()).toBeVisible({ timeout: 30000 });
    });

    test('7.2, 7.3, 7.4 AI Coach - Bookmark Flow', async ({ page, createUniqueUser }) => {
        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();
        await handleFirstLogin(page);

        await page.getByRole('button', { name: 'AI Coach' }).click();

        const input = page.getByPlaceholder(/Ask your AI coach/i).or(page.getByRole('textbox', { name: 'Ask about workouts...' }));
        await input.fill('Tell me a short fitness tip');
        await page.getByRole('button').filter({ hasText: /^$/ }).last().click();

        const responseBubble = page.locator('.prose').first();
        await expect(responseBubble).toBeVisible({ timeout: 30000 });

        const bookmarkBtn = page.getByRole('button', { name: /Bookmark/i }).first();
        if (await bookmarkBtn.isVisible()) {
            await bookmarkBtn.click();
        } else {
            // Try to hover or find icon
            const iconBtn = page.getByRole('button').filter({ has: page.locator('svg.lucide-bookmark') }).last();
            await iconBtn.click();
        }

        await expect(page.getByText(/Message saved/i)).toBeVisible();

        await page.getByRole('button', { name: /Saved/i }).click();
        await expect(page.getByText('Saved Messages')).toBeVisible();

        await expect(page.getByText(/fitness tip/i)).toBeVisible({ timeout: 3000 });

        const dialog = page.getByRole('dialog').filter({ hasText: 'Saved Messages' });
        const deleteBtn = dialog.getByRole('button', { name: /Remove bookmark/i }).first();
        await deleteBtn.click({ force: true });

        await expect(deleteBtn).not.toBeVisible();
    });

    // Merged from ai-coach-send-message.spec.ts
    test('7.5 AI Coach - Send a Message (Verification)', async ({ page, createUniqueUser }) => {
        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();

        await handleFirstLogin(page);

        await page.getByRole('button', { name: 'AI Coach' }).click();

        const message = "What's a good workout for chest?";
        await page.getByRole('textbox', { name: 'Ask about workouts...' }).fill(message);

        await page.getByRole('button').filter({ hasText: /^$/ }).last().click();

        await expect(page.getByText(message)).toBeVisible();
        await expect(page.getByText(/chest/i).nth(1)).toBeVisible({ timeout: 30000 });
    });

});
