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

declare global {
  interface HTMLElementTagNameMap {
    'sz-traffic-card': SzTrafficCard;
  }
}

export interface ITrafficData {
  requests: number;
  errors: number;
  errorPercent: number;
  avgResponse: number;
  reqPerMin: number;
  status2xx: number;
  status3xx: number;
  status4xx: number;
  status5xx: number;
}

@customElement('sz-traffic-card')
export class SzTrafficCard extends DeesElement {
  public static demo = () => html`
    <div style="padding: 24px; max-width: 500px;">
      <sz-traffic-card
        .data=${{
          requests: 1250,
          errors: 15,
          errorPercent: 1.2,
          avgResponse: 145,
          reqPerMin: 21,
          status2xx: 85,
          status3xx: 5,
          status4xx: 8,
          status5xx: 2,
        }}
      ></sz-traffic-card>
    </div>
  `;

  public static demoGroups = ['Dashboard'];

  @property({ type: Object })
  public accessor data: ITrafficData = {
    requests: 0,
    errors: 0,
    errorPercent: 0,
    avgResponse: 0,
    reqPerMin: 0,
    status2xx: 0,
    status3xx: 0,
    status4xx: 0,
    status5xx: 0,
  };

  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;
      }

      .metrics {
        display: grid;
        grid-template-columns: repeat(2, 1fr);
        gap: 16px;
        margin-bottom: 16px;
      }

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

      .metric-label {
        font-size: 13px;
        color: ${cssManager.bdTheme('#71717a', '#a1a1aa')};
      }

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

      .status-bar-container {
        padding-top: 12px;
        border-top: 1px solid ${cssManager.bdTheme('#f4f4f5', '#27272a')};
      }

      .status-bar {
        display: flex;
        height: 8px;
        border-radius: 4px;
        overflow: hidden;
        background: ${cssManager.bdTheme('#f4f4f5', '#27272a')};
        margin-bottom: 8px;
      }

      .status-segment {
        height: 100%;
        transition: width 300ms ease;
      }

      .status-2xx {
        background: ${cssManager.bdTheme('#22c55e', '#22c55e')};
      }

      .status-3xx {
        background: ${cssManager.bdTheme('#3b82f6', '#60a5fa')};
      }

      .status-4xx {
        background: ${cssManager.bdTheme('#facc15', '#facc15')};
      }

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

      .status-legend {
        display: flex;
        justify-content: space-between;
      }

      .legend-item {
        font-size: 12px;
        color: ${cssManager.bdTheme('#71717a', '#a1a1aa')};
      }
    `,
  ];

  public render(): TemplateResult {
    const total = this.data.status2xx + this.data.status3xx + this.data.status4xx + this.data.status5xx;
    const p2xx = total > 0 ? (this.data.status2xx / total) * 100 : 0;
    const p3xx = total > 0 ? (this.data.status3xx / total) * 100 : 0;
    const p4xx = total > 0 ? (this.data.status4xx / total) * 100 : 0;
    const p5xx = total > 0 ? (this.data.status5xx / total) * 100 : 0;

    return html`
      <div class="card">
        <div class="header">
          <div class="title">Traffic (Last Hour)</div>
          <div class="subtitle">Request metrics from access logs</div>
        </div>

        <div class="metrics">
          <div class="metric">
            <span class="metric-label">Requests</span>
            <span class="metric-value">${this.formatNumber(this.data.requests)}</span>
          </div>
          <div class="metric">
            <span class="metric-label">Errors</span>
            <span class="metric-value">${this.data.errors} (${this.data.errorPercent}%)</span>
          </div>
          <div class="metric">
            <span class="metric-label">Avg Response</span>
            <span class="metric-value">${this.data.avgResponse}ms</span>
          </div>
          <div class="metric">
            <span class="metric-label">Req/min</span>
            <span class="metric-value">${this.data.reqPerMin}</span>
          </div>
        </div>

        <div class="status-bar-container">
          <div class="status-bar">
            <div class="status-segment status-2xx" style="width: ${p2xx}%"></div>
            <div class="status-segment status-3xx" style="width: ${p3xx}%"></div>
            <div class="status-segment status-4xx" style="width: ${p4xx}%"></div>
            <div class="status-segment status-5xx" style="width: ${p5xx}%"></div>
          </div>
          <div class="status-legend">
            <span class="legend-item">2xx</span>
            <span class="legend-item">3xx</span>
            <span class="legend-item">4xx</span>
            <span class="legend-item">5xx</span>
          </div>
        </div>
      </div>
    `;
  }

  private formatNumber(num: number): string {
    if (num >= 1000000) return (num / 1000000).toFixed(1) + 'M';
    if (num >= 1000) return (num / 1000).toFixed(1) + 'K';
    return num.toString();
  }
}
