import { TestBed } from '@angular/core/testing';
import { provideRouter } from '@angular/router';
import { signal } from '@angular/core';
import { AUTH_API_URL } from '@marketplaces/auth';
import { AdminLayoutComponent } from './admin-layout.component';
import { AdminOrderWatcherService } from './services/admin-order-watcher.service';
import { AdminOrder } from '../orders/models/admin-order.model';

function makeOrder(id: string, orderNumber: string): AdminOrder {
  return {
    id,
    orderNumber,
    status: 'pending',
    customer: { name: 'Test Customer', email: 't@example.com', phone: '' },
    payment: { method: 'card', status: 'paid', amount: 500, currency: 'RUB' },
    shipping: { address: '', method: '', trackingNumber: '' },
    items: [],
    total: 500,
    currency: 'RUB',
    notes: '',
    internalNotes: '',
    timeline: [],
    archived: false,
    createdAt: '2026-08-15T10:00:00.000Z',
    updatedAt: '2026-08-15T10:00:00.000Z',
  };
}

describe('AdminLayoutComponent notifications bell', () => {
  let watcherStub: {
    recentOrders: ReturnType<typeof signal<AdminOrder[]>>;
    unreadCount: ReturnType<typeof signal<number>>;
    start: jasmine.Spy;
    stop: jasmine.Spy;
    markAllSeen: jasmine.Spy;
  };

  beforeEach(() => {
    watcherStub = {
      recentOrders: signal<AdminOrder[]>([makeOrder('o1', '1001')]),
      unreadCount: signal(1),
      start: jasmine.createSpy('start'),
      stop: jasmine.createSpy('stop'),
      markAllSeen: jasmine.createSpy('markAllSeen'),
    };

    TestBed.configureTestingModule({
      imports: [AdminLayoutComponent],
      providers: [
        provideRouter([]),
        { provide: AUTH_API_URL, useValue: 'https://test.local' },
        { provide: AdminOrderWatcherService, useValue: watcherStub },
      ],
    });
  });

  it('starts the watcher once on construction', () => {
    TestBed.createComponent(AdminLayoutComponent);
    expect(watcherStub.start).toHaveBeenCalledTimes(1);
  });

  it('exposes unreadCount and recentOrders from the watcher', () => {
    const fixture = TestBed.createComponent(AdminLayoutComponent);
    const component = fixture.componentInstance;
    expect(component.unreadCount()).toBe(1);
    expect(component.recentOrders().map(o => o.id)).toEqual(['o1']);
  });

  it('marks orders seen when the notifications panel opens', () => {
    const fixture = TestBed.createComponent(AdminLayoutComponent);
    const component = fixture.componentInstance;
    component.toggleNotifications();
    expect(component.notificationsOpen()).toBe(true);
    expect(watcherStub.markAllSeen).toHaveBeenCalledTimes(1);

    component.toggleNotifications();
    expect(component.notificationsOpen()).toBe(false);
    expect(watcherStub.markAllSeen).toHaveBeenCalledTimes(1);
  });

  it('stops the watcher when the component is destroyed', () => {
    const fixture = TestBed.createComponent(AdminLayoutComponent);
    fixture.detectChanges();
    fixture.destroy();
    expect(watcherStub.stop).toHaveBeenCalledTimes(1);
  });
});
