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

declare global {
  interface HTMLElementTagNameMap {
    'sz-registry-external-view': SzRegistryExternalView;
  }
}

export interface IExternalRegistry {
  id: string;
  name: string;
  url: string;
  username: string;
  type: 'dockerhub' | 'ghcr' | 'gcr' | 'ecr' | 'custom';
}

@customElement('sz-registry-external-view')
export class SzRegistryExternalView extends DeesElement {
  public static demo = () => html`
    <div style="padding: 24px; max-width: 800px;">
      <sz-registry-external-view
        .registries=${[
          { id: '1', name: 'Docker Hub', url: 'docker.io', username: 'myuser', type: 'dockerhub' },
          { id: '2', name: 'GitHub Container Registry', url: 'ghcr.io', username: 'myorg', type: 'ghcr' },
        ]}
      ></sz-registry-external-view>
    </div>
  `;

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

  @property({ type: Array })
  public accessor registries: IExternalRegistry[] = [];

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

      .header {
        display: flex;
        justify-content: space-between;
        align-items: flex-start;
        margin-bottom: 24px;
      }

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

      .header-title {
        font-size: 18px;
        font-weight: 600;
        color: ${cssManager.bdTheme('#18181b', '#fafafa')};
      }

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

      .add-button {
        display: inline-flex;
        align-items: center;
        gap: 6px;
        padding: 8px 14px;
        background: ${cssManager.bdTheme('#18181b', '#fafafa')};
        border: none;
        border-radius: 6px;
        font-size: 13px;
        font-weight: 500;
        color: ${cssManager.bdTheme('#fafafa', '#18181b')};
        cursor: pointer;
        transition: all 200ms ease;
      }

      .add-button:hover {
        opacity: 0.9;
      }

      .registry-list {
        display: flex;
        flex-direction: column;
        gap: 12px;
      }

      .registry-item {
        display: flex;
        justify-content: space-between;
        align-items: center;
        padding: 16px;
        background: ${cssManager.bdTheme('#ffffff', '#09090b')};
        border: 1px solid ${cssManager.bdTheme('#e4e4e7', '#27272a')};
        border-radius: 8px;
      }

      .registry-info {
        display: flex;
        align-items: center;
        gap: 12px;
      }

      .registry-icon {
        width: 40px;
        height: 40px;
        background: ${cssManager.bdTheme('#f4f4f5', '#18181b')};
        border-radius: 8px;
        display: flex;
        align-items: center;
        justify-content: center;
        color: ${cssManager.bdTheme('#71717a', '#a1a1aa')};
      }

      .registry-details {
        display: flex;
        flex-direction: column;
        gap: 2px;
      }

      .registry-name {
        font-size: 14px;
        font-weight: 500;
        color: ${cssManager.bdTheme('#18181b', '#fafafa')};
      }

      .registry-url {
        font-size: 13px;
        font-family: monospace;
        color: ${cssManager.bdTheme('#71717a', '#a1a1aa')};
      }

      .registry-actions {
        display: flex;
        gap: 8px;
      }

      .action-button {
        padding: 6px 12px;
        background: transparent;
        border: 1px solid ${cssManager.bdTheme('#e4e4e7', '#27272a')};
        border-radius: 4px;
        font-size: 12px;
        color: ${cssManager.bdTheme('#71717a', '#a1a1aa')};
        cursor: pointer;
        transition: all 200ms ease;
      }

      .action-button:hover {
        background: ${cssManager.bdTheme('#f4f4f5', '#18181b')};
        color: ${cssManager.bdTheme('#18181b', '#fafafa')};
      }

      .action-button.delete {
        color: ${cssManager.bdTheme('#dc2626', '#ef4444')};
        border-color: ${cssManager.bdTheme('#fee2e2', 'rgba(239, 68, 68, 0.3)')};
      }

      .action-button.delete:hover {
        background: ${cssManager.bdTheme('#fee2e2', 'rgba(239, 68, 68, 0.2)')};
      }

      .empty-state {
        background: ${cssManager.bdTheme('#ffffff', '#09090b')};
        border: 1px solid ${cssManager.bdTheme('#e4e4e7', '#27272a')};
        border-radius: 8px;
        padding: 48px 24px;
        text-align: center;
      }

      .empty-icon {
        width: 48px;
        height: 48px;
        margin: 0 auto 16px;
        color: ${cssManager.bdTheme('#a1a1aa', '#52525b')};
      }

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

      .empty-description {
        font-size: 14px;
        color: ${cssManager.bdTheme('#71717a', '#a1a1aa')};
        margin-bottom: 16px;
      }

      .empty-button {
        display: inline-flex;
        align-items: center;
        gap: 6px;
        padding: 8px 14px;
        background: ${cssManager.bdTheme('#ffffff', '#09090b')};
        border: 1px solid ${cssManager.bdTheme('#e4e4e7', '#27272a')};
        border-radius: 6px;
        font-size: 13px;
        font-weight: 500;
        color: ${cssManager.bdTheme('#18181b', '#fafafa')};
        cursor: pointer;
        transition: all 200ms ease;
      }

      .empty-button:hover {
        background: ${cssManager.bdTheme('#f4f4f5', '#18181b')};
      }
    `,
  ];

  public render(): TemplateResult {
    return html`
      <div class="header">
        <div class="header-info">
          <div class="header-title">External Registries</div>
          <div class="header-subtitle">Add credentials for private Docker registries</div>
        </div>
        <button class="add-button" @click=${() => this.handleAdd()}>Add Registry</button>
      </div>

      ${this.registries.length > 0 ? html`
        <div class="registry-list">
          ${this.registries.map(registry => html`
            <div class="registry-item">
              <div class="registry-info">
                <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="registry-details">
                  <div class="registry-name">${registry.name}</div>
                  <div class="registry-url">${registry.url} (${registry.username})</div>
                </div>
              </div>
              <div class="registry-actions">
                <button class="action-button" @click=${() => this.handleEdit(registry)}>Edit</button>
                <button class="action-button delete" @click=${() => this.handleDelete(registry)}>Delete</button>
              </div>
            </div>
          `)}
        </div>
      ` : html`
        <div class="empty-state">
          <svg class="empty-icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5">
            <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>
            <polyline points="3.27 6.96 12 12.01 20.73 6.96"></polyline>
            <line x1="12" y1="22.08" x2="12" y2="12"></line>
          </svg>
          <div class="empty-title">No external registries</div>
          <div class="empty-description">Add credentials for Docker Hub, GitHub Container Registry, or other private registries.</div>
          <button class="empty-button" @click=${() => this.handleAdd()}>Add External Registry</button>
        </div>
      `}
    `;
  }

  private handleAdd() {
    this.dispatchEvent(new CustomEvent('add', { bubbles: true, composed: true }));
  }

  private handleEdit(registry: IExternalRegistry) {
    this.dispatchEvent(new CustomEvent('edit', { detail: registry, bubbles: true, composed: true }));
  }

  private handleDelete(registry: IExternalRegistry) {
    this.dispatchEvent(new CustomEvent('delete', { detail: registry, bubbles: true, composed: true }));
  }
}
