import { Component, ChangeDetectionStrategy, inject, OnInit } from '@angular/core';
import { RouterOutlet, RouterLink, RouterLinkActive } from '@angular/router';
import { MatToolbarModule } from '@angular/material/toolbar';
import { MatButtonModule } from '@angular/material/button';
import { MatIconModule } from '@angular/material/icon';
import { ConnectivityService } from '@core/services';
import { ConnectivityOverlayComponent } from '@shared/index';

@Component({
  selector: 'app-root',
  standalone: true,
  imports: [
    RouterOutlet, RouterLink, RouterLinkActive,
    MatToolbarModule, MatButtonModule, MatIconModule,
    ConnectivityOverlayComponent
  ],
  changeDetection: ChangeDetectionStrategy.OnPush,
  template: `
    <!-- Overlay de conectividad - bloquea UI si no hay conexión -->
    <app-connectivity-overlay />

    <div class="app-container">
      <header class="app-header">
        <a routerLink="/" class="logo">
          <span class="logo-icon">S</span>
          <span class="logo-text">Estudiantes</span>
        </a>
        <nav class="nav-links">
          <a routerLink="/students" routerLinkActive="active" class="nav-link">
            Estudiantes
          </a>
        </nav>
      </header>
      <main class="app-main">
        <router-outlet />
      </main>
    </div>
  `,
  styles: [`
    .app-container {
      min-height: 100vh;
      display: flex;
      flex-direction: column;
      background: var(--bg-primary);
    }

    .app-header {
      display: flex;
      align-items: center;
      justify-content: space-between;
      padding: 0 2rem;
      height: 64px;
      background: rgba(255, 255, 255, 0.72);
      backdrop-filter: saturate(180%) blur(20px);
      border-bottom: 1px solid var(--border-light);
      position: sticky;
      top: 0;
      z-index: 100;
    }

    .logo {
      display: flex;
      align-items: center;
      gap: 0.75rem;
      text-decoration: none;
      color: var(--text-primary);
    }

    .logo-icon {
      width: 32px;
      height: 32px;
      background: linear-gradient(135deg, #007AFF 0%, #5856D6 100%);
      border-radius: 8px;
      display: flex;
      align-items: center;
      justify-content: center;
      color: white;
      font-weight: 600;
      font-size: 1.125rem;
    }

    .logo-text {
      font-weight: 600;
      font-size: 1.125rem;
      letter-spacing: -0.02em;
    }

    .nav-links {
      display: flex;
      gap: 0.5rem;
    }

    .nav-link {
      padding: 0.5rem 1rem;
      border-radius: 8px;
      text-decoration: none;
      color: var(--text-secondary);
      font-weight: 500;
      font-size: 0.875rem;
      transition: all 0.2s ease;
    }

    .nav-link:hover {
      background: var(--bg-secondary);
      color: var(--text-primary);
    }

    .nav-link.active {
      background: var(--bg-secondary);
      color: var(--accent);
    }

    .app-main {
      flex: 1;
      padding: 2rem;
      max-width: 1200px;
      width: 100%;
      margin: 0 auto;
    }

    @media (max-width: 600px) {
      .app-header {
        padding: 0 1rem;
      }

      .logo-text {
        display: none;
      }

      .app-main {
        padding: 1rem;
      }
    }
  `],
})
export class AppComponent implements OnInit {
  private connectivity = inject(ConnectivityService);

  ngOnInit(): void {
    // Iniciar monitoreo de conectividad cada 5 segundos
    this.connectivity.startMonitoring();
  }
}
