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

declare global {
  interface HTMLElementTagNameMap {
    'sz-platform-services-card': SzPlatformServicesCard;
  }
}

export interface IPlatformService {
  name: string;
  status: string;
  running: boolean;
  url?: string;
}

@customElement('sz-platform-services-card')
export class SzPlatformServicesCard extends DeesElement {
  public static demo = () => html`
    <div style="padding: 24px; max-width: 500px;">
      <sz-platform-services-card
        .services=${[
          { name: 'MongoDB', status: '1 DB', running: true },
          { name: 'S3 Storage (MinIO)', status: '1 bucket', running: true },
          { name: 'Caddy Reverse Proxy', status: 'Running', running: true },
          { name: 'ClickHouse', status: '1 DB', running: true },
        ]}
      ></sz-platform-services-card>
    </div>
  `;

  public static demoGroups = ['Platform'];

  @property({ type: Array })
  public accessor services: IPlatformService[] = [];

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

      .card {
        background: ${cssManager.bdTheme('#ffffff', '#09090b')};
        border: 1px solid ${cssManager.bdTheme('#e4e4e7', '#27272a')};
        border-radius: 8px;
        padding: 20px;
        height: 100%;
        box-sizing: border-box;
      }

      .header {
        margin-bottom: 16px;
      }

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

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

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

      .service-item {
        display: flex;
        justify-content: space-between;
        align-items: center;
        padding: 8px 0;
        cursor: pointer;
        transition: opacity 200ms ease;
      }

      .service-item:hover {
        opacity: 0.8;
      }

      .service-left {
        display: flex;
        align-items: center;
        gap: 10px;
      }

      .status-dot {
        width: 8px;
        height: 8px;
        border-radius: 50%;
        flex-shrink: 0;
      }

      .status-dot.running {
        background: ${cssManager.bdTheme('#22c55e', '#22c55e')};
        box-shadow: 0 0 6px ${cssManager.bdTheme('rgba(34, 197, 94, 0.4)', 'rgba(34, 197, 94, 0.4)')};
      }

      .status-dot.stopped {
        background: ${cssManager.bdTheme('#ef4444', '#ef4444')};
      }

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

      .service-status {
        font-size: 13px;
        color: ${cssManager.bdTheme('#71717a', '#a1a1aa')};
      }
    `,
  ];

  public render(): TemplateResult {
    return html`
      <div class="card">
        <div class="header">
          <div class="title">Platform Services</div>
          <div class="subtitle">Infrastructure status</div>
        </div>

        <div class="services-list">
          ${this.services.map(
            (service) => html`
              <div class="service-item" @click=${() => this.handleServiceClick(service)}>
                <div class="service-left">
                  <div class="status-dot ${service.running ? 'running' : 'stopped'}"></div>
                  <span class="service-name">${service.name}</span>
                </div>
                <span class="service-status">${service.status}</span>
              </div>
            `
          )}
        </div>
      </div>
    `;
  }

  private handleServiceClick(service: IPlatformService) {
    this.dispatchEvent(
      new CustomEvent('service-click', {
        detail: service,
        bubbles: true,
        composed: true,
      })
    );
  }
}
