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

test.describe('Recurring Tasks', () => {
    test.beforeEach(async ({ page }) => {
        // Login
        await page.goto('http://localhost:5001/auth');
        await page.fill('input[name="username"]', 'admin');
        await page.fill('input[name="password"]', 'admin123');
        await page.click('button[type="submit"]');
        await expect(page).toHaveURL('http://localhost:5001/');
    });

    test('should create a daily recurring task and generate next occurrence on completion', async ({ page }) => {
        // 1. Open Create Task
        await page.click('button[data-testid="fab-create-task"]'); // Correct ID found in App.tsx
        // Look for dialog
        await expect(page.locator('div[role="dialog"]')).toBeVisible();

        // 2. Fill form
        const timestamp = Date.now();
        const taskTitle = `Recurring Task ${timestamp}`;
        await page.fill('input[data-testid="input-task-title"]', taskTitle);

        // 3. Set Recurrence
        await page.click('[data-testid="recurrence-trigger"]');
        await page.click('[data-testid="recurrence-option-daily"]');
        // User locale might be German if previously set. Admin default is English usually.
        // I'll try english text first. If logic fails, I'll update.

        // 4. Save
        await page.click('button[data-testid="button-save-task"]');

        // Give backend time to process
        await page.waitForTimeout(1000);

        // 5. Verify task created
        await page.goto('http://localhost:5001/tasks');
        await page.waitForLoadState('networkidle'); // Wait for tasks to load
        await expect(page.locator(`text=${taskTitle}`)).toBeVisible();

        // 6. Complete Task
        // Find the card containing the text, then click the checkbox inside it
        await page.locator('[data-testid^="card-task-"]').filter({ hasText: taskTitle }).locator('button[role="checkbox"]').click();

        // 7. Verify logic
        // Task should disappear (if filtered) or become checked.
        // Allow time for async recurrence creation
        await page.waitForTimeout(2000);

        // Reload to see the new task (it might be added to the list or need refresh)
        await page.reload();
        await page.waitForLoadState('networkidle');

        // 8. Verify NEW task exists. 
        // It should have the same title.
        // There might be 2 tasks now (one done, one todo) if we show done tasks.
        // Or just one if done is hidden.
        // We want to check that a "Todo" task with that title exists.
        // We can check the checkbox state logic.
        // But simplest check: Ensure at least one such task exists and is NOT checked?
        // Or just that 2 exist?
        // Let's check count.
        const titleCount = await page.locator(`text=${taskTitle}`).count();
        expect(titleCount).toBeGreaterThanOrEqual(1);

        // Verify at least one is unchecked (the new one)
        const uncheckedCount = await page.locator('[data-testid^="card-task-"]').filter({ hasText: taskTitle }).locator('button[role="checkbox"][aria-checked="false"]').count();
        expect(uncheckedCount).toBeGreaterThanOrEqual(1);
    });
});
