/**
 * Seller Settings Test
 * 
 * Tags: @seller @p2 @settings
 * 
 * Tests seller settings functionality:
 * - View settings page
 * - View profile settings
 * - Verify settings categories
 * - Test navigation within settings
 */

import { test, expect } from '@playwright/test';
import { 
  printTestCase, 
  printSuccess, 
  printWarning, 
  ensureAuthenticated 
} from '../../helpers/test-helpers';

test.describe('06_Seller_Portal - Settings', () => {
  test.use({ storageState: 'auth.json' });
  
  test.beforeEach(async ({ page }) => {
    await ensureAuthenticated(page);
  });
  
  test('Test Case 1: Navigate to settings page', async ({ page }) => {
    printTestCase(1, 'Navigate to Settings Page');
    
    await page.goto('/seller/settings');
    await page.waitForLoadState('networkidle');
    
    // Verify settings page loaded
    const settingsHeader = page.locator('h1:has-text("Settings"), h6:has-text("Settings"), .card-header:has-text("Settings")');
    const headerVisible = await settingsHeader.first().isVisible({ timeout: 5000 }).catch(() => false);
    
    if (headerVisible) {
      printSuccess('Settings page loaded');
    } else {
      printWarning('Settings header not found');
    }
    
    // Verify URL
    const currentUrl = page.url();
    if (currentUrl.includes('/seller/settings')) {
      printSuccess('On seller settings page');
    }
  });
  
  test('Test Case 2: View profile settings', async ({ page }) => {
    printTestCase(2, 'View Profile Settings');
    
    await page.goto('/seller/settings/profile');
    await page.waitForLoadState('networkidle');
    
    // Look for profile form fields
    const profileFields = page.locator('input[type="text"], input[type="email"], textarea');
    const fieldCount = await profileFields.count();
    
    if (fieldCount > 0) {
      printSuccess(`Found ${fieldCount} profile field(s)`);
      
      // Check for save button
      const saveBtn = page.locator('button:has-text("Save"), button:has-text("Update")');
      if (await saveBtn.first().isVisible({ timeout: 2000 }).catch(() => false)) {
        printSuccess('Save button found');
      }
    } else {
      printWarning('No profile fields found');
    }
  });
  
  test('Test Case 3: Verify settings sections', async ({ page }) => {
    printTestCase(3, 'Verify Settings Sections');
    
    await page.goto('/seller/settings');
    await page.waitForLoadState('networkidle');
    
    // Look for settings navigation tabs or sections
    const settingsTabs = page.locator('.nav-tabs, .settings-nav, [role="tablist"]');
    if (await settingsTabs.first().isVisible({ timeout: 3000 }).catch(() => false)) {
      printSuccess('Settings navigation found');
      
      // Count tab items
      const tabItems = settingsTabs.first().locator('.nav-link, [role="tab"], li');
      const tabCount = await tabItems.count();
      
      if (tabCount > 0) {
        printSuccess(`Found ${tabCount} settings section(s)`);
      }
    } else {
      printWarning('Settings navigation not found');
    }
  });
  
  test('Test Case 4: View company information', async ({ page }) => {
    printTestCase(4, 'View Company Information');
    
    await page.goto('/seller/settings');
    await page.waitForLoadState('networkidle');
    
    // Look for company info section
    const companyInfo = page.locator('.company-info, [class*="company"], :has-text("Company")');
    if (await companyInfo.first().isVisible({ timeout: 3000 }).catch(() => false)) {
      printSuccess('Company information section found');
      
      // Check for company name
      const companyName = page.locator('input[name*="company"], [id*="company"]');
      if (await companyName.first().isVisible().catch(() => false)) {
        const name = await companyName.first().inputValue();
        if (name) {
          printSuccess(`Company name: ${name}`);
        }
      }
    } else {
      printWarning('Company information not found');
    }
  });
  
  test('Test Case 5: Test settings form validation', async ({ page }) => {
    printTestCase(5, 'Test Settings Form Validation');
    
    await page.goto('/seller/settings');
    await page.waitForLoadState('networkidle');
    
    // Look for required fields
    const requiredFields = page.locator('input[required], [aria-required="true"]');
    const requiredCount = await requiredFields.count();
    
    if (requiredCount > 0) {
      printSuccess(`Found ${requiredCount} required field(s)`);
      
      // Try submitting form without required fields (if save button exists)
      const saveBtn = page.locator('button:has-text("Save"), button:has-text("Update")');
      if (await saveBtn.first().isVisible({ timeout: 2000 }).catch(() => false)) {
        // Check if form has validation
        const form = page.locator('form');
        if (await form.first().isVisible().catch(() => false)) {
          printSuccess('Settings form found');
        }
      }
    } else {
      printWarning('No required fields found');
    }
  });
  
  test('Test Case 6: Verify breadcrumb navigation', async ({ page }) => {
    printTestCase(6, 'Verify Breadcrumb Navigation');
    
    await page.goto('/seller/settings');
    await page.waitForLoadState('networkidle');
    
    // Look for breadcrumbs
    const breadcrumbs = page.locator('.breadcrumb, [aria-label="breadcrumb"]');
    if (await breadcrumbs.isVisible({ timeout: 2000 }).catch(() => false)) {
      printSuccess('Breadcrumbs found');
      
      const breadcrumbItems = breadcrumbs.locator('li, .breadcrumb-item');
      const itemCount = await breadcrumbItems.count();
      printSuccess(`Breadcrumb items: ${itemCount}`);
    } else {
      printWarning('Breadcrumbs not found');
    }
  });
});

