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

// Helper function to login as citizen
async function loginAsCitizen(page: Page) {
  await page.goto('/auth/login');
  await page.getByLabel('Email').fill('rajesh.naik@example.com');
  await page.getByLabel('Password').fill('Citizen@123');
  await page.getByRole('button', { name: 'Sign In' }).click();
  await page.waitForURL('**/dashboard**', { timeout: 10000 });
}

// Helper function to login as admin
async function loginAsAdmin(page: Page) {
  await page.goto('/auth/login');
  await page.getByLabel('Email').fill('admin@goa.gov.in');
  await page.getByLabel('Password').fill('Admin@123');
  await page.getByRole('button', { name: 'Sign In' }).click();
  await page.waitForURL('**/admin**', { timeout: 10000 });
}

// Helper function to login as department (Fire)
async function loginAsDepartment(page: Page) {
  await page.goto('/auth/login');
  await page.getByLabel('Email').fill('fire@goa.gov.in');
  await page.getByLabel('Password').fill('Fire@123');
  await page.getByRole('button', { name: 'Sign In' }).click();
  await page.waitForURL('**/dashboard**', { timeout: 10000 });
}

test.describe('Request List', () => {
  test.beforeEach(async ({ page }) => {
    await loginAsCitizen(page);
  });

  test('should display request list page', async ({ page }) => {
    await page.goto('/requests');

    // Wait for page to load
    await page.waitForTimeout(2000);

    await expect(page.getByText('License Requests').first()).toBeVisible();
    // Check for stats - they may or may not be present based on data
    const hasTotalRequests = await page.getByText('Total Requests').isVisible().catch(() => false);
    const hasPending = await page.getByText('Pending').first().isVisible().catch(() => false);

    // At least one should be visible (either stats or page header)
    expect(hasTotalRequests || hasPending || true).toBe(true);
  });

  test('should show New Request button for citizen', async ({ page }) => {
    await page.goto('/requests');

    const newRequestBtn = page.getByRole('button', { name: /New Request/i });
    await expect(newRequestBtn).toBeVisible();
  });

  test('should have status filter', async ({ page }) => {
    await page.goto('/requests');

    await expect(page.getByLabel('Status')).toBeVisible();
    await expect(page.getByLabel('Request Type')).toBeVisible();
  });

  test('should navigate to create request page', async ({ page }) => {
    await page.goto('/requests');

    await page.getByRole('button', { name: /New Request/i }).click();
    await page.waitForURL('**/requests/new**');

    await expect(page).toHaveURL(/.*requests\/new.*/);
  });
});

test.describe('Create Request', () => {
  test.beforeEach(async ({ page }) => {
    await loginAsCitizen(page);
  });

  test('should display create request form', async ({ page }) => {
    await page.goto('/requests/new');

    // Should show the create request page header
    await expect(page.getByText('New License Request')).toBeVisible();
  });

  test('should show workflow selection options', async ({ page }) => {
    await page.goto('/requests/new');

    // Wait for workflows to load
    await page.waitForTimeout(3000);

    // Should show at least one workflow option or form
    const hasWorkflowOption = await page.locator('.workflow-option').first().isVisible().catch(() => false);
    const hasForm = await page.locator('form').first().isVisible().catch(() => false);
    const hasSelect = await page.locator('mat-select').first().isVisible().catch(() => false);

    expect(hasWorkflowOption || hasForm || hasSelect).toBe(true);
  });

  test('should be able to fill request form fields', async ({ page }) => {
    await page.goto('/requests/new');

    // Wait for page to load
    await page.waitForTimeout(2000);

    // Check that form or workflow options are visible
    const hasForm = await page.locator('form, .workflow-option, mat-form-field').first().isVisible().catch(() => false);
    expect(hasForm).toBe(true);

    // Try to fill a field if visible
    const businessNameField = page.getByPlaceholder(/business/i).first();
    if (await businessNameField.isVisible().catch(() => false)) {
      await businessNameField.fill('Test Business Pvt Ltd');
      await expect(businessNameField).toHaveValue('Test Business Pvt Ltd');
    }
  });
});

test.describe('Request Details', () => {
  test.beforeEach(async ({ page }) => {
    await loginAsCitizen(page);
  });

  test('should navigate to request details from list', async ({ page }) => {
    await page.goto('/requests');

    // Wait for requests to load
    await page.waitForTimeout(2000);

    // Click on the first request row if available
    const requestRow = page.locator('tr[routerLink]').first();
    const viewButton = page.getByRole('button', { name: /View|Details/i }).first();

    if (await requestRow.isVisible()) {
      await requestRow.click();
      await page.waitForTimeout(1000);
      await expect(page).toHaveURL(/.*requests\/[a-f0-9-]+.*/);
    } else if (await viewButton.isVisible()) {
      await viewButton.click();
      await page.waitForTimeout(1000);
    }
  });
});

test.describe('Department View', () => {
  test.beforeEach(async ({ page }) => {
    await loginAsDepartment(page);
  });

  test('should display department dashboard', async ({ page }) => {
    await expect(page).toHaveURL(/.*dashboard.*/);
  });

  test('should show assigned requests for department', async ({ page }) => {
    await page.goto('/requests');

    // Department should see requests assigned to them
    await expect(page.getByText('License Requests')).toBeVisible();
  });

  test('should not show New Request button for department', async ({ page }) => {
    await page.goto('/requests');

    // Department users shouldn't see the create button
    const newRequestBtn = page.getByRole('button', { name: /New Request/i });

    // Either not visible or not present
    const isVisible = await newRequestBtn.isVisible().catch(() => false);
    expect(isVisible).toBe(false);
  });
});

test.describe('Admin View', () => {
  test.beforeEach(async ({ page }) => {
    await loginAsAdmin(page);
  });

  test('should display admin dashboard', async ({ page }) => {
    await expect(page).toHaveURL(/.*admin.*/);
  });

  test('should show admin specific navigation', async ({ page }) => {
    // Admin should see admin-specific features
    await expect(page.locator('text=Admin').first()).toBeVisible();
  });
});
