export default class RegressionTesting extends HTMLElement {
  constructor() {
    super();
    this.regressions = [];
    this.isRunning = false;
  }

  connectedCallback() {
    this.render();
    this.setupEventListeners();
  }

  render() {
    this.innerHTML = `
      <div style="padding: 24px; height: 100%; overflow-y: auto;">
        <h1 style="margin: 0 0 8px 0; font-size: 24px;">Visual Regression Testing</h1>
        <p style="margin: 0 0 24px 0; color: var(--vscode-text-dim);">Detect visual changes in design system components</p>

        <div style="background: var(--vscode-sidebar); border: 1px solid var(--vscode-border); border-radius: 4px; padding: 16px; margin-bottom: 24px;">
          <label style="display: block; font-size: 12px; font-weight: 500; margin-bottom: 8px;">Components to Test</label>
          <div style="display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-bottom: 12px;">
            <label style="display: flex; align-items: center; gap: 6px; cursor: pointer; font-size: 12px;">
              <input type="checkbox" checked /> Buttons
            </label>
            <label style="display: flex; align-items: center; gap: 6px; cursor: pointer; font-size: 12px;">
              <input type="checkbox" checked /> Inputs
            </label>
            <label style="display: flex; align-items: center; gap: 6px; cursor: pointer; font-size: 12px;">
              <input type="checkbox" checked /> Cards
            </label>
            <label style="display: flex; align-items: center; gap: 6px; cursor: pointer; font-size: 12px;">
              <input type="checkbox" checked /> Modals
            </label>
          </div>
          <button id="run-tests-btn" style="width: 100%; padding: 8px; background: var(--vscode-button-background); color: var(--vscode-button-foreground); border: none; border-radius: 4px; cursor: pointer; font-weight: 500; font-size: 12px;">Run Tests</button>
        </div>

        <div id="progress-container" style="display: none; margin-bottom: 24px;">
          <div style="margin-bottom: 8px; font-size: 12px; color: var(--vscode-text-dim);">Testing... <span id="progress-count">0/4</span></div>
          <div style="width: 100%; height: 6px; background: var(--vscode-bg); border-radius: 3px; overflow: hidden;">
            <div id="progress-bar" style="width: 0%; height: 100%; background: #0066CC; transition: width 0.3s;"></div>
          </div>
        </div>

        <div id="results-container" style="display: none;"></div>
      </div>
    `;
  }

  setupEventListeners() {
    this.querySelector('#run-tests-btn').addEventListener('click', () => this.runTests());
  }

  async runTests() {
    this.isRunning = true;
    this.querySelector('#progress-container').style.display = 'block';
    this.querySelector('#results-container').style.display = 'none';

    const components = ['Buttons', 'Inputs', 'Cards', 'Modals'];
    this.regressions = [];

    for (let i = 0; i < components.length; i++) {
      this.querySelector('#progress-count').textContent = (i + 1) + '/4';
      this.querySelector('#progress-bar').style.width = ((i + 1) / 4 * 100) + '%';
      await new Promise(resolve => setTimeout(resolve, 600));

      if (Math.random() > 0.7) {
        this.regressions.push({
          component: components[i],
          severity: Math.random() > 0.5 ? 'critical' : 'minor'
        });
      }
    }

    this.renderResults();
    this.querySelector('#progress-container').style.display = 'none';
    this.querySelector('#results-container').style.display = 'block';
  }

  renderResults() {
    const container = this.querySelector('#results-container');
    const passed = 4 - this.regressions.length;

    let html = `<div style="display: grid; grid-template-columns: 1fr 1fr; gap: 12px; margin-bottom: 24px;">
      <div style="background: var(--vscode-sidebar); padding: 12px; border-radius: 4px; text-align: center; border: 2px solid #4CAF50;">
        <div style="font-size: 24px; font-weight: 600; color: #4CAF50;">${passed}</div>
        <div style="font-size: 11px; color: var(--vscode-text-dim);">Passed</div>
      </div>
      <div style="background: var(--vscode-sidebar); padding: 12px; border-radius: 4px; text-align: center; border: 2px solid ${this.regressions.length > 0 ? '#F44336' : '#4CAF50'};">
        <div style="font-size: 24px; font-weight: 600; color: ${this.regressions.length > 0 ? '#F44336' : '#4CAF50'};">${this.regressions.length}</div>
        <div style="font-size: 11px; color: var(--vscode-text-dim);">Regressions</div>
      </div>
    </div>`;

    if (this.regressions.length === 0) {
      html += `<div style="background: var(--vscode-sidebar); border: 1px solid var(--vscode-border); border-radius: 4px; padding: 24px; text-align: center;"><div style="font-size: 20px; margin-bottom: 8px;">All Tests Passed</div></div>`;
    } else {
      html += `<h2 style="margin: 0 0 12px 0; font-size: 14px;">Regressions Found</h2>`;
      for (let reg of this.regressions) {
        const color = reg.severity === 'critical' ? '#F44336' : '#FF9800';
        html += `<div style="background: var(--vscode-sidebar); border: 1px solid var(--vscode-border); border-radius: 4px; padding: 12px; margin-bottom: 12px;">
          <div style="display: flex; justify-content: space-between; margin-bottom: 8px;">
            <div style="font-weight: 500;">${reg.component}</div>
            <span style="padding: 2px 8px; background: ${color}; color: white; border-radius: 2px; font-size: 10px;">${reg.severity}</span>
          </div>
          <button style="width: 100%; padding: 6px; background: var(--vscode-button-background); color: var(--vscode-button-foreground); border: none; border-radius: 3px; cursor: pointer; font-size: 10px;">Approve as Baseline</button>
        </div>`;
      }
    }

    container.innerHTML = html;
  }
}

customElements.define('ds-regression-testing', RegressionTesting);
