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

test.describe('Advanced Planning & Subtasks', () => {
    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"]'); // Assuming there is a submit button
        await expect(page).toHaveURL('http://localhost:5001/');
    });

    test('should create a task with start date and duration', async ({ page }) => {
        console.log('Starting test 1');
        // Wait for FAB to ensure page loaded
        await expect(page.getByTestId('fab-create-task')).toBeVisible({ timeout: 10000 });
        console.log('Page loaded');

        // Open Task Creation Modal
        await page.getByTestId('fab-create-task').click();
        await expect(page.getByTestId('input-task-title')).toBeVisible();

        // Fill Title
        await page.getByTestId('input-task-title').fill('Plan Weekend Trip');
        await page.getByTestId('input-task-description').fill('Detailed planning');

        // Set Duration (using new Input)
        // Click the badge for 60m
        console.log('Setting duration');
        await page.getByText('60m').first().click();

        // Verify Input value is 60
        // Use a more specific locator for the input
        await expect(page.getByTestId('input-duration')).toHaveValue('60');

        // Save
        console.log('Saving task');
        await page.getByTestId('button-save-task').click();

        // Check if modal closed
        await expect(page.getByTestId('input-task-title')).toBeHidden();

        // Verify Task Card appears
        console.log('Waiting for task card');
        const taskCard = page.locator('text=Plan Weekend Trip').first();
        await expect(taskCard).toBeVisible({ timeout: 10000 });

        // Verify Duration Badge (60m -> 1h)
        await expect(page.locator('text=⏳ 1h')).toBeVisible();
    });

    test('should create a subtask', async ({ page }) => {
        console.log('Starting test 2');
        // Wait for FAB to ensure page loaded
        await expect(page.getByTestId('fab-create-task')).toBeVisible({ timeout: 10000 });

        // Find a task (create one if none exists ideally, but let's assume 'Plan Weekend Trip' from prev test or seed)
        // Let's create a fresh parent task to be safe
        await page.getByTestId('fab-create-task').click();
        await expect(page.getByTestId('input-task-title')).toBeVisible();
        await page.getByTestId('input-task-title').fill('Parent Task Project');
        console.log('Saving parent task');
        await page.getByTestId('button-save-task').click();

        // Wait for it to appear
        console.log('Waiting for parent task');
        await expect(page.locator('text=Parent Task Project').first()).toBeVisible({ timeout: 10000 });

        // Open Task Details
        console.log('Opening task details');
        await page.locator('text=Parent Task Project').first().click();

        // Wait for Details Modal
        await expect(page.getByTestId('tab-subtasks')).toBeVisible();

        // Go to Subtasks Tab
        await page.getByTestId('tab-subtasks').click();

        // Create Subtask
        console.log('Creating subtask');
        await page.getByPlaceholder('New subtask title...').fill('Subtask 1');
        await page.getByRole('button', { name: 'Add Subtask' }).click();

        // Verify Subtask appears in list (Wait for network/state update)
        // It might take a moment
        await expect(page.locator('text=Subtask 1')).toBeVisible({ timeout: 10000 });

        // Close Modal
        await page.keyboard.press('Escape');

        // Verify "Subtasks" badge on card
        // Note: The badge says "0/1 Subtasks" or similar.
        console.log('Verifying badge');
        await expect(page.locator('text=Subtasks').first()).toBeVisible();
    });
});
