const { test, expect } = require('@playwright/test');

test.describe('AutoBackups - Backup Workflow Tests', () => {
  test('should display backup status information', async ({ page }) => {
    await page.goto('/');
    
    // Look for backup status indicators
    const statusElements = page.locator('.status, .backup-status, #status, [data-testid*="status"]');
    if (await statusElements.count() > 0) {
      await expect(statusElements.first()).toBeVisible();
      console.log('Backup status indicators found');
    }
    
    // Look for last backup information
    const lastBackupInfo = page.locator(':has-text("Last backup"), :has-text("Último backup"), .last-backup');
    if (await lastBackupInfo.count() > 0) {
      console.log('Last backup information displayed');
    }
    
    await page.screenshot({ path: 'test-results/backup-status.png' });
  });

  test('should allow manual backup trigger', async ({ page }) => {
    await page.goto('/');
    
    // Look for backup trigger button
    const backupButton = page.locator(
      'button:has-text("Backup"), button:has-text("Start"), button:has-text("Iniciar"), ' +
      'button:has-text("Manual"), .backup-trigger, #backup-btn'
    );
    
    if (await backupButton.count() > 0) {
      console.log('Manual backup trigger found');
      
      // Test clicking the backup button
      await backupButton.first().click();
      
      // Wait for any response or status change
      await page.waitForTimeout(2000);
      
      // Look for feedback messages
      const messages = page.locator('.message, .alert, .notification, .success, .error');
      if (await messages.count() > 0) {
        console.log('Backup trigger feedback received');
      }
      
      await page.screenshot({ path: 'test-results/backup-triggered.png' });
    }
  });

  test('should display project list', async ({ page }) => {
    await page.goto('/');
    
    // Look for project list or table
    const projectElements = page.locator(
      'table, .project-list, .projects, ul li, .project-item, [data-testid*="project"]'
    );
    
    if (await projectElements.count() > 0) {
      await expect(projectElements.first()).toBeVisible();
      console.log('Project list/table found');
      
      // Count visible projects
      const projectCount = await projectElements.count();
      console.log(`Found ${projectCount} project-related elements`);
    }
    
    await page.screenshot({ path: 'test-results/project-list.png' });
  });

  test('should handle backup scheduling interface', async ({ page }) => {
    await page.goto('/config');
    
    // Look for scheduling options
    const scheduleElements = page.locator(
      'select[name*="schedule"], input[name*="schedule"], .schedule, .timing, ' +
      ':has-text("Schedule"), :has-text("Horario"), :has-text("Frequency")'
    );
    
    if (await scheduleElements.count() > 0) {
      console.log('Backup scheduling interface found');
      
      // Test schedule options if they're in a select dropdown
      const scheduleSelect = page.locator('select[name*="schedule"], select:has(option:has-text("daily")), select:has(option:has-text("hourly"))');
      if (await scheduleSelect.count() > 0) {
        const options = await scheduleSelect.first().locator('option').allTextContents();
        console.log('Schedule options available:', options);
      }
    }
    
    await page.screenshot({ path: 'test-results/backup-scheduling.png' });
  });

  test('should show backup progress or logs', async ({ page }) => {
    await page.goto('/');
    
    // Look for progress indicators or log displays
    const progressElements = page.locator(
      '.progress, .progress-bar, .logs, .log-output, .console, ' +
      '[data-testid*="progress"], [data-testid*="log"]'
    );
    
    if (await progressElements.count() > 0) {
      console.log('Progress or log display found');
      await expect(progressElements.first()).toBeVisible();
    }
    
    // Look for log entries or messages
    const logEntries = page.locator('.log-entry, .log-line, .message');
    if (await logEntries.count() > 0) {
      console.log(`Found ${await logEntries.count()} log entries`);
    }
    
    await page.screenshot({ path: 'test-results/backup-progress.png' });
  });
});
