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

test('Home page should display user drones from API', async ({ page }) => {
  await page.goto('https://code-dev.g4dge7.com:5174/sign-in');
  await page.waitForLoadState('networkidle');
  await page.fill('#email', 'rob@digitaltelepresence.com');
  await page.fill('#password', 'ionfrali');
  await page.click('button[type="submit"]');
  await page.waitForTimeout(3000);
  
  await page.goto('https://code-dev.g4dge7.com:5174/');
  await page.waitForLoadState('networkidle');
  await page.waitForTimeout(2000);
  
  const sidebar = page.locator('aside').first();
  const sidebarText = await sidebar.textContent();
  
  expect(sidebarText).toContain('Drones');
  expect(sidebarText).toContain('mysterymachine');
});

test('DroneInspector shows drone details', async ({ page }) => {
  await page.goto('https://code-dev.g4dge7.com:5174/sign-in');
  await page.waitForLoadState('networkidle');
  await page.fill('#email', 'rob@digitaltelepresence.com');
  await page.fill('#password', 'ionfrali');
  await page.click('button[type="submit"]');
  await page.waitForTimeout(3000);
  
  await page.goto('https://code-dev.g4dge7.com:5174/');
  await page.waitForLoadState('networkidle');
  await page.waitForTimeout(2000);
  
  const sidebar = page.locator('aside').first();
  
  const droneButton = sidebar.locator('button').filter({ hasText: 'mysterymachine' }).first();
  await droneButton.click();
  await page.waitForTimeout(500);
  
  const inspector = page.locator('text=Drone Inspector');
  await expect(inspector).toBeVisible();
  
  const backButton = page.locator('text=← Back to Dashboard');
  await expect(backButton).toBeVisible();
  
  const hostname = page.locator('text=Hostname');
  await expect(hostname).toBeVisible();
  
  const workspace = page.locator('text=Workspace');
  await expect(workspace).toBeVisible();
  
  await backButton.click();
  await page.waitForTimeout(500);
  
  await expect(inspector).not.toBeVisible();
});

test('Drones API returns available and busy drones', async ({ page }) => {
  await page.goto('https://code-dev.g4dge7.com:5174/sign-in');
  await page.waitForLoadState('networkidle');
  await page.fill('#email', 'rob@digitaltelepresence.com');
  await page.fill('#password', 'ionfrali');
  await page.click('button[type="submit"]');
  await page.waitForTimeout(2000);
  
  const token = await page.evaluate(() => localStorage.getItem('dtp_auth_token'));
  expect(token).toBeDefined();
  
  const dronesResponse = await page.request.get('https://code-dev.g4dge7.com:5174/api/v1/drone/registration', {
    headers: { 'Authorization': `Bearer ${token}` },
  });
  
  expect(dronesResponse.status()).toBe(200);
  const data = await dronesResponse.json() as { success: boolean; data: Array<{ status: string }> };
  expect(data.success).toBe(true);
  expect(data.data).toBeDefined();
  
  const availableOrBusy = data.data.filter(d => d.status === 'available' || d.status === 'busy');
  expect(availableOrBusy.length).toBeGreaterThan(0);
});