// Settings page JavaScript

const DEFAULT_SETTINGS = {
  apiUrl: 'http://localhost:8000',
  customerDocsPath: 'D:\\Customer Docs'
};

class SettingsManager {
  constructor() {
    this.loadSettings();
    this.attachEventListeners();
  }

  async loadSettings() {
    try {
      const settings = await chrome.storage.local.get(['apiUrl', 'customerDocsPath']);
      
      // Set values in inputs, use defaults if not set
      document.getElementById('apiUrl').value = settings.apiUrl || DEFAULT_SETTINGS.apiUrl;
      document.getElementById('customerDocsPath').value = settings.customerDocsPath || DEFAULT_SETTINGS.customerDocsPath;
      
    } catch (error) {
      console.error('Error loading settings:', error);
      this.showMessage('Error loading settings', 'error');
    }
  }

  attachEventListeners() {
    document.getElementById('saveBtn').addEventListener('click', () => this.saveSettings());
    document.getElementById('resetBtn').addEventListener('click', () => this.resetSettings());
  }

  async saveSettings() {
    try {
      const apiUrl = document.getElementById('apiUrl').value.trim();
      const customerDocsPath = document.getElementById('customerDocsPath').value.trim();
      
      // Validate inputs
      if (!apiUrl) {
        this.showMessage('API Server URL is required', 'error');
        return;
      }
      
      if (!customerDocsPath) {
        this.showMessage('Customer Documents Path is required', 'error');
        return;
      }
      
      // Validate URL format
      try {
        new URL(apiUrl);
      } catch (e) {
        this.showMessage('Invalid API Server URL format', 'error');
        return;
      }
      
      // Save to Chrome storage
      await chrome.storage.local.set({
        apiUrl: apiUrl,
        customerDocsPath: customerDocsPath
      });
      
      this.showMessage('Settings saved successfully!', 'success');
      
      // Close the settings tab after a short delay
      setTimeout(() => {
        window.close();
      }, 1500);
      
    } catch (error) {
      console.error('Error saving settings:', error);
      this.showMessage('Error saving settings', 'error');
    }
  }

  async resetSettings() {
    try {
      // Reset to default values
      document.getElementById('apiUrl').value = DEFAULT_SETTINGS.apiUrl;
      document.getElementById('customerDocsPath').value = DEFAULT_SETTINGS.customerDocsPath;
      
      // Save defaults to storage
      await chrome.storage.local.set(DEFAULT_SETTINGS);
      
      this.showMessage('Settings reset to defaults', 'success');
      
    } catch (error) {
      console.error('Error resetting settings:', error);
      this.showMessage('Error resetting settings', 'error');
    }
  }

  showMessage(text, type) {
    const messageEl = document.getElementById('statusMessage');
    messageEl.textContent = text;
    messageEl.className = `status-message ${type}`;
    
    // Auto-hide after 3 seconds
    setTimeout(() => {
      messageEl.className = 'status-message';
    }, 3000);
  }
}

// Initialize when DOM is ready
document.addEventListener('DOMContentLoaded', () => {
  new SettingsManager();
});