/* eslint-disable @typescript-eslint/no-explicit-any */
import { Page, expect } from '@playwright/test';
import { testUsers } from '../fixtures/test-data';

export class AuthHelper {
  constructor(private page: Page) {}

  async loginAs(userType: keyof typeof testUsers) {
    const user = testUsers[userType];
    
    await this.page.goto('/login');
    await this.page.getByLabel('Email').fill(user.email);
    await this.page.getByLabel('Password').fill(user.password);
    await this.page.getByRole('button', { name: 'Sign In' }).click();
    
    // Wait for redirect to dashboard
    await expect(this.page).toHaveURL('/dashboard');
  }

  async logout() {
    // Click user menu and logout
    await this.page.getByRole('button', { name: 'User Menu' }).click();
    await this.page.getByRole('menuitem', { name: 'Logout' }).click();
    
    // Should redirect to login
    await expect(this.page).toHaveURL('/login');
  }

  async ensureLoggedOut() {
    // Clear cookies and local storage
    await this.page.context().clearCookies();
    await this.page.evaluate(() => {
      localStorage.clear();
      sessionStorage.clear();
    });
  }
}

export class MockAPIHelper {
  constructor(private page: Page) {}

  async mockLoginSuccess(userType: keyof typeof testUsers) {
    const user = testUsers[userType];
    
    await this.page.route('**/api/v1/auth/login', async route => {
      await route.fulfill({
        status: 200,
        contentType: 'application/json',
        body: JSON.stringify({
          access_token: 'mock-jwt-token',
          token_type: 'bearer',
          user_id: `test-${userType}-id`,
          role: user.role,
        }),
      });
    });
  }

  async mockJobList(jobs: any[] = []) {
    await this.page.route('**/api/v1/jobs', async route => {
      await route.fulfill({
        status: 200,
        contentType: 'application/json',
        body: JSON.stringify({
          jobs,
          total: jobs.length,
          page: 1,
          size: 20,
        }),
      });
    });
  }

  async mockJobDetail(jobId: string, job: any) {
    await this.page.route(`**/api/v1/jobs/${jobId}`, async route => {
      await route.fulfill({
        status: 200,
        contentType: 'application/json',
        body: JSON.stringify(job),
      });
    });
  }

  async mockVttContent(jobId: string, language: string, content: any) {
    await this.page.route(`**/api/v1/jobs/${jobId}/vtt?language=${language}`, async route => {
      await route.fulfill({
        status: 200,
        contentType: 'application/json',
        body: JSON.stringify(content),
      });
    });
  }
}