import { TestBed } from '@angular/core/testing';
import { LyricsParserService } from './lyrics-parser.service';
import { SettingsService } from './settings.service';

describe('LyricsParserService - Hybrid Notation', () => {
  let service: LyricsParserService;
  let mockSettingsService: any;

  beforeEach(() => {
    mockSettingsService = {
      chordNotationPreference: jasmine.createSpy('chordNotationPreference').and.returnValue('diesis')
    };

    TestBed.configureTestingModule({
      providers: [
        LyricsParserService,
        { provide: SettingsService, useValue: mockSettingsService }
      ]
    });
    service = TestBed.inject(LyricsParserService);
  });

  it('should parse simple text with memorize, replay, copy, paste', () => {
    const rawSong = `
{start_verse}{memorize/m1}
Ralle[RE]griamoci, [LA]
non [SOL]c’è spazio alla tri[RE]stezza in questo gior[LA]no [SOL]
{end_verse}

{start_chorus}{copy/rit}
Gloria a [RE]Te Emma[SOL]nue-[LA]le
{end_chorus}

{start_verse}{replay/m1}
Rallegri*amoci,*
Egli *viene a libe*rarci da ogni *male.*
{end_verse}

{start_chorus}{paste/rit}
{end_chorus}
`;

    // 1. Test parsing with chords enabled
    const sections = service.parseAccordi(rawSong);
    expect(sections.length).toBe(4);

    // Verse 1 (memorize)
    expect(sections[0].type).toBe('verse');
    expect(sections[0].lines[0].text).toBe('Rallegriamoci, ');
    expect(sections[0].lines[0].segments[0].chord).toBeUndefined();
    expect(sections[0].lines[0].segments[1].chord).toBe('RE');
    expect(sections[0].lines[0].segments[2].chord).toBe('LA');

    // Chorus 1 (copy)
    expect(sections[1].type).toBe('chorus');
    expect(sections[1].lines[0].text).toBe('Gloria a Te Emmanue-le');
    expect(sections[1].lines[0].segments[0].chord).toBeUndefined();
    expect(sections[1].lines[0].segments[1].chord).toBe('RE');
    expect(sections[1].lines[0].segments[2].chord).toBe('SOL');
    expect(sections[1].lines[0].segments[3].chord).toBe('LA');

    // Verse 2 (replay)
    expect(sections[2].type).toBe('verse');
    expect(sections[2].lines[0].text).toBe('Rallegriamoci,');
    expect(sections[2].lines[0].segments[0].chord).toBeUndefined();
    expect(sections[2].lines[0].segments[1].chord).toBe('RE');
    expect(sections[2].lines[0].segments[2].chord).toBe('LA');

    expect(sections[2].lines[1].text).toBe('Egli viene a liberarci da ogni male.');
    expect(sections[2].lines[1].segments[0].chord).toBeUndefined();
    expect(sections[2].lines[1].segments[1].chord).toBe('SOL');
    expect(sections[2].lines[1].segments[2].chord).toBe('RE');
    expect(sections[2].lines[1].segments[3].chord).toBe('LA');
    expect(sections[2].lines[1].segments[4].chord).toBe('SOL');

    // Chorus 2 (paste)
    expect(sections[3].type).toBe('chorus');
    expect(sections[3].lines[0].text).toBe('Gloria a Te Emmanue-le');
    expect(sections[3].lines[0].segments[0].chord).toBeUndefined();
    expect(sections[3].lines[0].segments[1].chord).toBe('RE');
    expect(sections[3].lines[0].segments[2].chord).toBe('SOL');
    expect(sections[3].lines[0].segments[3].chord).toBe('LA');
  });

  it('should strip chords in text-only mode and remove asterisks', () => {
    const rawSong = `
{start_verse}{memorize/m1}
Ralle[RE]griamoci, [LA]
{end_verse}

{start_verse}{replay/m1}
Rallegri*amoci,*
{end_verse}
`;
    const sections = service.parseText(rawSong);
    expect(sections.length).toBe(2);

    expect(sections[0].lines[0].text).toBe('Rallegriamoci,');
    expect(sections[0].lines[0].segments[0].chord).toBeUndefined();

    expect(sections[1].lines[0].text).toBe('Rallegriamoci,');
    expect(sections[1].lines[0].segments[0].chord).toBeUndefined();
  });
});
