import { ComponentFixture, TestBed } from '@angular/core/testing';
import { DashboardSummaryComponent } from './dashboard-summary.component';
import { AgentSummary, SystemHealth } from '../../models/agent.model';

describe('DashboardSummaryComponent', () => {
  let component: DashboardSummaryComponent;
  let fixture: ComponentFixture<DashboardSummaryComponent>;

  const mockSummary: AgentSummary = {
    total: 7,
    active: 4,
    idle: 1,
    thinking: 1,
    error: 1,
  };

  const mockHealthy: SystemHealth = {
    connected: true,
    status: 'healthy',
  };

  beforeEach(async () => {
    await TestBed.configureTestingModule({
      imports: [DashboardSummaryComponent],
    }).compileComponents();

    fixture = TestBed.createComponent(DashboardSummaryComponent);
    component = fixture.componentInstance;
  });

  it('should create', () => {
    expect(component).toBeTruthy();
  });

  it('should default to zeroed summary', () => {
    const summary = component.summary();
    expect(summary.total).toBe(0);
    expect(summary.active).toBe(0);
    expect(summary.idle).toBe(0);
    expect(summary.thinking).toBe(0);
    expect(summary.error).toBe(0);
  });

  it('should default to disconnected/down health', () => {
    const health = component.health();
    expect(health.connected).toBe(false);
    expect(health.status).toBe('down');
  });

  it('should update summary data', () => {
    component.updateSummary(mockSummary);
    expect(component.summary()).toEqual(mockSummary);
  });

  it('should update health data', () => {
    component.updateHealth(mockHealthy);
    expect(component.health()).toEqual(mockHealthy);
  });

  it('should compute hasErrors correctly', () => {
    expect(component.hasErrors()).toBe(false);
    component.updateSummary({ ...mockSummary, error: 2 });
    expect(component.hasErrors()).toBe(true);
  });

  it('should compute connectionColor correctly', () => {
    expect(component.connectionColor()).toBe('disconnected');
    component.updateHealth({ connected: true, status: 'healthy' });
    expect(component.connectionColor()).toBe('connected');
  });

  it('should compute statusLabel for each state', () => {
    component.updateHealth({ connected: true, status: 'healthy' });
    expect(component.statusLabel()).toBe('All Systems Go');

    component.updateHealth({ connected: true, status: 'degraded' });
    expect(component.statusLabel()).toBe('Degraded');

    component.updateHealth({ connected: false, status: 'down' });
    expect(component.statusLabel()).toBe('Offline');
  });

  it('should render summary values in template', () => {
    component.updateSummary(mockSummary);
    component.updateHealth(mockHealthy);
    fixture.detectChanges();

    const compiled = fixture.nativeElement as HTMLElement;
    expect(compiled.textContent).toContain('4 / 7');
    expect(compiled.textContent).toContain('Active');
    expect(compiled.textContent).toContain('All Systems Go');
  });

  it('should render status breakdown chips', () => {
    component.updateSummary(mockSummary);
    fixture.detectChanges();

    const compiled = fixture.nativeElement as HTMLElement;
    expect(compiled.textContent).toContain('4');  // active count
    expect(compiled.textContent).toContain('1');   // idle count (multiple)
    expect(compiled.textContent).toContain('Error');
  });
});