import {
  DeesElement,
  customElement,
  html,
  css,
  cssManager,
  property,
  type TemplateResult,
} from '@design.estate/dees-element';

declare global {
  interface HTMLElementTagNameMap {
    'sz-registry-advertisement': SzRegistryAdvertisement;
  }
}

@customElement('sz-registry-advertisement')
export class SzRegistryAdvertisement extends DeesElement {
  public static demo = () => html`
    <div style="padding: 24px; max-width: 800px;">
      <sz-registry-advertisement
        status="running"
        registryUrl="localhost:3000/v2"
      ></sz-registry-advertisement>
    </div>
  `;

  public static demoGroups = ['Auth & Settings'];

  @property({ type: String })
  public accessor status: 'running' | 'stopped' = 'stopped';

  @property({ type: String })
  public accessor registryUrl: string = '';

  public static styles = [
    cssManager.defaultStyles,
    css`
      :host {
        display: block;
      }

      .card {
        background: ${cssManager.bdTheme('#ffffff', '#09090b')};
        border: 1px solid ${cssManager.bdTheme('#e4e4e7', '#27272a')};
        border-radius: 8px;
        overflow: hidden;
      }

      .card-header {
        display: flex;
        align-items: center;
        gap: 12px;
        padding: 16px;
        border-bottom: 1px solid ${cssManager.bdTheme('#e4e4e7', '#27272a')};
      }

      .registry-icon {
        width: 40px;
        height: 40px;
        background: ${cssManager.bdTheme('#dbeafe', 'rgba(59, 130, 246, 0.2)')};
        border-radius: 8px;
        display: flex;
        align-items: center;
        justify-content: center;
        color: ${cssManager.bdTheme('#2563eb', '#60a5fa')};
      }

      .header-info {
        flex: 1;
      }

      .header-title {
        display: flex;
        align-items: center;
        gap: 8px;
        font-size: 16px;
        font-weight: 600;
        color: ${cssManager.bdTheme('#18181b', '#fafafa')};
      }

      .default-badge {
        padding: 2px 8px;
        background: ${cssManager.bdTheme('#dbeafe', 'rgba(59, 130, 246, 0.2)')};
        color: ${cssManager.bdTheme('#2563eb', '#60a5fa')};
        border-radius: 4px;
        font-size: 11px;
        font-weight: 600;
      }

      .header-subtitle {
        font-size: 13px;
        color: ${cssManager.bdTheme('#71717a', '#a1a1aa')};
        margin-top: 2px;
      }

      .card-content {
        padding: 16px;
      }

      .info-grid {
        display: grid;
        grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
        gap: 16px;
        margin-bottom: 24px;
      }

      .info-item {
        display: flex;
        flex-direction: column;
        gap: 4px;
      }

      .info-label {
        font-size: 12px;
        font-weight: 500;
        color: ${cssManager.bdTheme('#71717a', '#a1a1aa')};
        text-transform: uppercase;
        letter-spacing: 0.05em;
      }

      .info-value {
        font-size: 14px;
        color: ${cssManager.bdTheme('#18181b', '#fafafa')};
      }

      .status-badge {
        display: inline-flex;
        align-items: center;
        padding: 2px 8px;
        border-radius: 9999px;
        font-size: 12px;
        font-weight: 500;
      }

      .status-badge.running {
        background: ${cssManager.bdTheme('#dcfce7', 'rgba(34, 197, 94, 0.2)')};
        color: ${cssManager.bdTheme('#16a34a', '#22c55e')};
      }

      .status-badge.stopped {
        background: ${cssManager.bdTheme('#fee2e2', 'rgba(239, 68, 68, 0.2)')};
        color: ${cssManager.bdTheme('#dc2626', '#ef4444')};
      }

      .manage-link {
        color: ${cssManager.bdTheme('#2563eb', '#60a5fa')};
        text-decoration: none;
        cursor: pointer;
      }

      .manage-link:hover {
        text-decoration: underline;
      }

      .quick-start {
        background: ${cssManager.bdTheme('#f4f4f5', '#18181b')};
        border-radius: 6px;
        padding: 16px;
      }

      .quick-start-title {
        font-size: 14px;
        font-weight: 600;
        color: ${cssManager.bdTheme('#18181b', '#fafafa')};
        margin-bottom: 8px;
      }

      .quick-start-desc {
        font-size: 13px;
        color: ${cssManager.bdTheme('#71717a', '#a1a1aa')};
        margin-bottom: 12px;
      }

      .code-block {
        background: ${cssManager.bdTheme('#18181b', '#0a0a0a')};
        border-radius: 6px;
        padding: 12px;
        font-family: monospace;
        font-size: 13px;
        overflow-x: auto;
      }

      .code-line {
        color: ${cssManager.bdTheme('#a1a1aa', '#71717a')};
        margin-bottom: 4px;
      }

      .code-line:last-child {
        margin-bottom: 0;
      }

      .code-comment {
        color: ${cssManager.bdTheme('#6b7280', '#52525b')};
      }

      .code-command {
        color: ${cssManager.bdTheme('#fafafa', '#e4e4e7')};
      }
    `,
  ];

  public render(): TemplateResult {
    return html`
      <div class="card">
        <div class="card-header">
          <div class="registry-icon">
            <svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
              <path d="M21 16V8a2 2 0 0 0-1-1.73l-7-4a2 2 0 0 0-2 0l-7 4A2 2 0 0 0 3 8v8a2 2 0 0 0 1 1.73l7 4a2 2 0 0 0 2 0l7-4A2 2 0 0 0 21 16z"></path>
            </svg>
          </div>
          <div class="header-info">
            <div class="header-title">
              Registry Advertisement
              <span class="default-badge">Default</span>
            </div>
            <div class="header-subtitle">Container registry for your services</div>
          </div>
        </div>

        <div class="card-content">
          <div class="info-grid">
            <div class="info-item">
              <span class="info-label">Status</span>
              <span class="info-value">
                <span class="status-badge ${this.status}">${this.status === 'running' ? 'Running' : 'Stopped'}</span>
              </span>
            </div>
            <div class="info-item">
              <span class="info-label">Registry URL</span>
              <span class="info-value">${this.registryUrl}</span>
            </div>
            <div class="info-item">
              <span class="info-label">Authentication</span>
              <span class="info-value">
                <a class="manage-link" @click=${() => this.handleManageTokens()}>Manage Tokens</a>
              </span>
            </div>
          </div>

          <div class="quick-start">
            <div class="quick-start-title">Quick Start</div>
            <div class="quick-start-desc">To push images to the registry, use a CI or Global token:</div>
            <div class="code-block">
              <div class="code-line code-comment"># Login to the registry</div>
              <div class="code-line code-command">docker login ${this.registryUrl.split('/')[0]} -u registry -p YOUR_TOKEN</div>
              <div class="code-line code-comment"># Tag and push your image</div>
              <div class="code-line code-command">docker tag myapp ${this.registryUrl.split('/')[0]}/myservice:latest</div>
              <div class="code-line code-command">docker push ${this.registryUrl.split('/')[0]}/myservice:latest</div>
            </div>
          </div>
        </div>
      </div>
    `;
  }

  private handleManageTokens() {
    this.dispatchEvent(new CustomEvent('manage-tokens', { bubbles: true, composed: true }));
  }
}
