import { ComponentFixture, TestBed } from '@angular/core/testing';

import { MyProfileUpdateFormComponent } from './my-profile-update-form.component';
import { provideRouter } from '@angular/router';
import { ToastrService } from 'ngx-toastr';
import { FormBuilder } from '@angular/forms';
import { PROFILE_REPOSITORY_TOKEN } from '@app/infrastructure/profiles/profile-repository.token';
import { ProfileRepository } from '@app/domain/profiles/profile.repository';
import { of } from 'rxjs';
import { Profile } from '@app/domain/profiles/profile.model';
import { SectorRepository } from '@app/domain/sectors/sector.repository';
import { Sector } from '@app/domain/sectors/sector.model';
import { SECTOR_REPOSITORY_TOKEN } from '@app/infrastructure/sectors/sector-repository.token';
import { mockToastR } from '@app/testing/toastr.mock';
import { ProjectRepository } from '@app/domain/projects/project.repository';
import { mockProfileRepo } from '@app/testing/profile.mock';
import { mockSectorRepo } from '@app/testing/sector.mock';

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

  let mockToastrService: jest.Mocked<Partial<ToastrService>> = mockToastR;
  let mockProfileRepository: jest.Mocked<Partial<ProfileRepository>> = mockProfileRepo;
  let mockSectorRepository: jest.Mocked<Partial<SectorRepository>> = mockSectorRepo;

  const mockProfileData = {
    profession: '',
    secteur: '',
    bio: '',
    apropos: '',
    reseaux: { facebook: '', github: '', instagram: '', linkedIn: '', web: '', x: '', youTube: '' },
  };

  beforeEach(async () => {
    await TestBed.configureTestingModule({
      imports: [MyProfileUpdateFormComponent],
      providers: [
        FormBuilder,
        provideRouter([]),
        { provide: ToastrService, useValue: mockToastrService },
        { provide: PROFILE_REPOSITORY_TOKEN, useValue: mockProfileRepository },
        { provide: SECTOR_REPOSITORY_TOKEN, useValue: mockSectorRepository },
      ],
    }).compileComponents();

    fixture = TestBed.createComponent(MyProfileUpdateFormComponent);
    component = fixture.componentInstance;
    fixture.detectChanges();

    await fixture.whenStable();
  });

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

  it('should submit invalid update form', () => {
    component.profileForm.setValue(mockProfileData);

    const spyUpdateProfile = jest.spyOn(component, 'onSubmit');
    component.onSubmit();
    expect(component.profileForm.valid).toEqual(false);
    expect(spyUpdateProfile).toHaveBeenCalled();
  });

  it('should submit valid update form', () => {
    mockProfileData.profession = 'developer';
    mockProfileData.secteur = 'technology';
    mockProfileData.bio = 'A passionate developer';
    mockProfileData.apropos = 'About me';
    component.profileForm.setValue(mockProfileData);

    const spyUpdateProfile = jest.spyOn(component, 'onSubmit');
    component.onSubmit();
    expect(component.profileForm.valid).toEqual(true);
    expect(spyUpdateProfile).toHaveBeenCalled();
  });
});
