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

declare global {
  interface HTMLElementTagNameMap {
    'opencdn-mainpage': OpencdnMainpage;
  }
}

@customElement('opencdn-mainpage')
export class OpencdnMainpage extends DeesElement {
  @property({ type: String })
  public accessor version: string = '1.0.0';

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

  @property({ type: Array })
  public accessor allowedPackages: string[] = [];

  @state()
  private accessor currentView: 'home' | 'peek' | 'docs' = 'home';

  // Shadcn-like color palette
  public static styles = [
    css`
      :host {
        --background: #09090b;
        --foreground: #fafafa;
        --card: #09090b;
        --card-foreground: #fafafa;
        --muted: #27272a;
        --muted-foreground: #a1a1aa;
        --border: #27272a;
        --input: #27272a;
        --primary: #fafafa;
        --primary-foreground: #18181b;
        --secondary: #27272a;
        --secondary-foreground: #fafafa;
        --accent: #27272a;
        --accent-foreground: #fafafa;
        --ring: #d4d4d8;
        --radius: 0.5rem;

        display: block;
        min-height: 100vh;
        background: var(--background);
        font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', sans-serif;
        color: var(--foreground);
        -webkit-font-smoothing: antialiased;
      }

      * {
        box-sizing: border-box;
      }

      .container {
        max-width: 700px;
        margin: 0 auto;
        padding: 60px 24px;
        min-height: 100vh;
        display: flex;
        flex-direction: column;
      }

      .header {
        text-align: center;
        margin-bottom: 48px;
      }

      .logo {
        display: inline-flex;
        align-items: center;
        gap: 14px;
        margin-bottom: 16px;
      }

      .logo-icon {
        width: 48px;
        height: 48px;
        background: var(--foreground);
        border-radius: 12px;
        display: flex;
        align-items: center;
        justify-content: center;
      }

      .logo-icon dees-icon {
        --dees-icon-color: var(--background);
      }

      .logo-text {
        font-size: 36px;
        font-weight: 600;
        color: var(--foreground);
        letter-spacing: -0.02em;
      }

      .tagline {
        color: var(--muted-foreground);
        font-size: 16px;
        font-weight: 400;
      }

      .content {
        flex: 1;
        display: flex;
        flex-direction: column;
        gap: 24px;
      }

      .card {
        background: var(--card);
        border: 1px solid var(--border);
        border-radius: var(--radius);
        padding: 24px;
      }

      .usage-box {
        background: var(--muted);
        border-radius: var(--radius);
        padding: 20px 24px;
        border: 1px solid var(--border);
      }

      .usage-title {
        color: var(--muted-foreground);
        font-size: 14px;
        margin-bottom: 12px;
        text-align: center;
      }

      .usage-code {
        font-family: 'SF Mono', 'Fira Code', 'Fira Mono', Menlo, monospace;
        font-size: 14px;
        color: var(--foreground);
        background: var(--background);
        padding: 14px 18px;
        border-radius: calc(var(--radius) - 2px);
        text-align: center;
        border: 1px solid var(--border);
      }

      .nav-buttons {
        display: flex;
        gap: 12px;
        justify-content: center;
        flex-wrap: wrap;
      }

      .nav-buttons dees-button {
        --dees-button-width: auto;
      }

      .packages-section {
        margin-top: 8px;
      }

      .packages-title {
        color: var(--foreground);
        font-size: 14px;
        font-weight: 500;
        margin-bottom: 12px;
        display: flex;
        align-items: center;
        gap: 8px;
      }

      .packages-list {
        display: flex;
        flex-wrap: wrap;
        gap: 8px;
      }

      .package-tag {
        background: var(--secondary);
        color: var(--muted-foreground);
        padding: 8px 14px;
        border-radius: calc(var(--radius) - 2px);
        font-family: 'SF Mono', 'Fira Code', monospace;
        font-size: 13px;
        border: 1px solid var(--border);
        cursor: pointer;
        transition: all 0.15s ease;
      }

      .package-tag:hover {
        background: var(--accent);
        color: var(--foreground);
        border-color: var(--ring);
      }

      .footer {
        display: flex;
        justify-content: space-between;
        align-items: center;
        padding-top: 32px;
        margin-top: auto;
        border-top: 1px solid var(--border);
        font-size: 13px;
        color: var(--muted-foreground);
      }

      .status {
        display: inline-flex;
        align-items: center;
        gap: 8px;
        color: #22c55e;
        font-size: 13px;
        font-weight: 500;
      }

      .status-dot {
        width: 8px;
        height: 8px;
        background: #22c55e;
        border-radius: 50%;
        animation: pulse 2s infinite;
      }

      @keyframes pulse {
        0%, 100% { opacity: 1; }
        50% { opacity: 0.5; }
      }

      .version {
        font-family: 'SF Mono', monospace;
        color: var(--muted-foreground);
        font-size: 12px;
      }

      .mode-badge {
        padding: 4px 10px;
        border-radius: calc(var(--radius) - 2px);
        font-size: 11px;
        font-weight: 500;
        text-transform: uppercase;
        letter-spacing: 0.05em;
      }

      .mode-badge.dev {
        background: rgba(34, 197, 94, 0.1);
        color: #22c55e;
      }

      .mode-badge.prod {
        background: rgba(239, 68, 68, 0.1);
        color: #ef4444;
      }

      .footer-right {
        display: flex;
        align-items: center;
        gap: 16px;
      }

      .footer-link {
        color: var(--muted-foreground);
        text-decoration: none;
        transition: color 0.15s ease;
        font-size: 13px;
      }

      .footer-link:hover {
        color: var(--foreground);
      }

      .empty-state {
        text-align: center;
        padding: 40px 20px;
        color: var(--muted-foreground);
        display: flex;
        flex-direction: column;
        align-items: center;
        gap: 16px;
      }
    `,
  ];

  public render(): TemplateResult {
    return html`
      <div class="container">
        <div class="header">
          <div class="logo">
            <div class="logo-icon">
              <dees-icon .icon=${'lucide:package'} .iconSize=${24}></dees-icon>
            </div>
            <span class="logo-text">opencdn</span>
          </div>
          <p class="tagline">Serve files directly from npm packages</p>
        </div>

        <div class="content">
          <div class="usage-box">
            <div class="usage-title">Request files using:</div>
            <div class="usage-code">/@scope/package/path/to/file.js?version=1.0.0</div>
          </div>

          ${this.mode === 'dev' ? html`
            <div class="nav-buttons">
              <dees-button type="highlighted" @click=${() => window.location.href = '/peek/'}>
                <dees-icon .icon=${'lucide:folder-open'} .iconSize=${16} slot="iconLeft"></dees-icon>
                Browse Packages
              </dees-button>
              <dees-button @click=${() => window.location.href = '/readme/'}>
                <dees-icon .icon=${'lucide:book-open'} .iconSize=${16} slot="iconLeft"></dees-icon>
                Documentation
              </dees-button>
            </div>
          ` : ''}

          ${this.allowedPackages.length > 0 ? html`
            <div class="packages-section">
              <div class="packages-title">
                <dees-icon .icon=${'lucide:boxes'} .iconSize=${16}></dees-icon>
                Available Packages
              </div>
              <div class="packages-list">
                ${this.allowedPackages.map(pkg => html`
                  <span class="package-tag" @click=${() => window.location.href = `/peek/${pkg}`}>
                    ${pkg}
                  </span>
                `)}
              </div>
            </div>
          ` : html`
            <div class="empty-state">
              <dees-icon .icon=${'lucide:package-x'} .iconSize=${48} style="opacity: 0.5;"></dees-icon>
              <p>No packages configured</p>
            </div>
          `}
        </div>

        <div class="footer">
          <div style="display: flex; align-items: center; gap: 12px;">
            <span class="status">
              <span class="status-dot"></span>
              Online
            </span>
            <span class="version">v${this.version}</span>
            <span class="mode-badge ${this.mode}">${this.mode}</span>
          </div>
          <div class="footer-right">
            <a href="https://task.vc" target="_blank" class="footer-link">Task Venture Capital</a>
          </div>
        </div>
      </div>
    `;
  }
}
