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 { MatMenuModule } from '@angular/material/menu';
import { ConnectivityService, AuthService } from '@core/services';
import { ConnectivityOverlayComponent } from '@shared/index';

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

    <div class="app-container">
      @if (authService.isAuthenticated()) {
        <header class="app-header">
          <a [routerLink]="authService.isAdmin() ? '/admin' : '/dashboard'" class="logo">
            <span class="logo-icon">S</span>
            <span class="logo-text">Estudiantes</span>
          </a>
          <nav class="nav-links">
            @if (authService.isAdmin()) {
              <a routerLink="/admin" routerLinkActive="active" class="nav-link">
                Panel Admin
              </a>
              <a routerLink="/students" routerLinkActive="active" class="nav-link">
                Gestion Estudiantes
              </a>
            } @else {
              <a routerLink="/dashboard" routerLinkActive="active" class="nav-link">
                Mi Portal
              </a>
              @if (authService.studentId()) {
                <a [routerLink]="['/enrollment', authService.studentId()]" routerLinkActive="active" class="nav-link">
                  Mis Materias
                </a>
                <a [routerLink]="['/classmates', authService.studentId()]" routerLinkActive="active" class="nav-link">
                  Compañeros
                </a>
              }
            }
          </nav>
          <div class="user-menu">
            <button class="user-button" [matMenuTriggerFor]="userMenu">
              <span class="user-avatar">{{ getUserInitial() }}</span>
              <span class="user-name">{{ authService.user()?.username }}</span>
              <mat-icon>expand_more</mat-icon>
            </button>
            <mat-menu #userMenu="matMenu">
              <div class="menu-header">
                <strong>{{ authService.user()?.username }}</strong>
                <span class="role-badge">{{ authService.user()?.role }}</span>
              </div>
              <button mat-menu-item (click)="logout()">
                <mat-icon>logout</mat-icon>
                Cerrar sesion
              </button>
            </mat-menu>
          </div>
        </header>
      }
      <main class="app-main" [class.full-height]="!authService.isAuthenticated()">
        <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;
    }

    .user-menu {
      display: flex;
      align-items: center;
    }

    .user-button {
      display: flex;
      align-items: center;
      gap: 0.5rem;
      padding: 0.375rem 0.75rem;
      border: none;
      background: var(--bg-secondary);
      border-radius: 9999px;
      cursor: pointer;
      transition: background 0.2s;
    }

    .user-button:hover {
      background: var(--border-light);
    }

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

    .user-name {
      font-weight: 500;
      font-size: 0.875rem;
      color: var(--text-primary);
    }

    .menu-header {
      padding: 0.75rem 1rem;
      border-bottom: 1px solid var(--border-light);
      display: flex;
      flex-direction: column;
      gap: 0.25rem;
    }

    .role-badge {
      font-size: 0.75rem;
      color: var(--text-secondary);
    }

    .full-height {
      padding: 0 !important;
      max-width: none !important;
    }

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

      .logo-text, .user-name {
        display: none;
      }

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

  ngOnInit(): void {
    this.connectivity.startMonitoring();
  }

  getUserInitial(): string {
    const username = this.authService.user()?.username;
    return username ? username.charAt(0).toUpperCase() : '?';
  }

  logout(): void {
    this.authService.logout();
  }
}
