import { expect } from '@playwright/test';
import type { Page } from '@playwright/test';

export async function startOrStopTimerWithButton(page: Page) {
    await page
        .getByTestId('dashboard_timer')
        .getByTestId('timer_button')
        .and(page.locator(':visible'))
        .click();
}

export async function assertThatTimerHasStarted(page: Page) {
    await expect(
        page
            .getByTestId('dashboard_timer')
            .getByTestId('timer_button')
            .and(page.locator(':visible'))
    ).toHaveClass(/bg-red-400\/80/);
}

export function newTimeEntryResponse(
    page: Page,
    {
        description = '',
        status = 201,
        tags = [],
        type,
    }: {
        description?: string;
        status?: number;
        tags?: string[];
        type?: 'work' | 'break';
    } = {}
) {
    return page.waitForResponse(async (response) => {
        return (
            response.url().includes('/time-entries') &&
            response.status() === status &&
            (await response.headerValue('Content-Type')) === 'application/json' &&
            (await response.json()).data.id !== null &&
            (await response.json()).data.start !== null &&
            (await response.json()).data.end === null &&
            (await response.json()).data.project_id === null &&
            (await response.json()).data.description === description &&
            (await response.json()).data.task_id === null &&
            (await response.json()).data.user_id !== null &&
            (type === undefined || (await response.json()).data.type === type) &&
            JSON.stringify((await response.json()).data.tags) === JSON.stringify(tags)
        );
    });
}

export async function assertThatTimerIsStopped(page: Page) {
    await expect(
        page
            .getByTestId('dashboard_timer')
            .getByTestId('timer_button')
            .and(page.locator(':visible'))
    ).toHaveClass(/bg-accent-300\/70/);
}

export async function stoppedTimeEntryResponse(
    page: Page,
    {
        description = '',
        tags = [],
        type,
    }: {
        description?: string;
        tags?: string[];
        type?: 'work' | 'break';
    } = {}
) {
    return page.waitForResponse(async (response) => {
        return (
            response.status() === 200 &&
            response.url().includes('/time-entries/') &&
            (await response.headerValue('Content-Type')) === 'application/json' &&
            (await response.json()).data.id !== null &&
            (await response.json()).data.start !== null &&
            (await response.json()).data.end !== null &&
            (await response.json()).data.project_id === null &&
            (await response.json()).data.description === description &&
            (await response.json()).data.task_id === null &&
            (await response.json()).data.duration !== null &&
            (await response.json()).data.user_id !== null &&
            (type === undefined || (await response.json()).data.type === type) &&
            JSON.stringify((await response.json()).data.tags) === JSON.stringify(tags)
        );
    });
}
