/// <reference types="cypress" />

import makeFakeUser from '../../factories/user';

describe('Profile update', () => {
  beforeEach(() => {
    // Log in to the application or navigate to the account page
    // where the change email feature is available
    const currentUser = makeFakeUser();
    cy.stubAuthenticate({ ...currentUser });
    cy.intercept('GET', /\S+\/users\/\S+\/profile/, {
      statusCode: 200,
      body: {
        first_name: 'David',
        last_name: 'Wilson',
        name: 'David Wilson',
        city: 'Barstow',
        state: 'CA',
        country: 'US',
        location: 'Barstow, CA',
        photo_url: null,
        biography: 'This is the musician biography. It is a long form text.',
        virtual_band: true,
        virtual_band_commitment: 2,
        traditional_band: false,
        traditional_band_commitment: null,
        traditional_band_touring: false,
        paid_sessions: true,
        paid_sessions_hourly_rate: 19900,
        paid_sessions_daily_rate: 200000,
        free_sessions: true,
        cowriting: true,
        cowriting_purpose: 2,
        subscribe_email: true,
        genres: [
          {
            genre_id: 'asian',
            player_type: 'JamRuby::User',
            genre_type: 'profile'
          },
          {
            genre_id: 'blues',
            player_type: 'JamRuby::User',
            genre_type: 'profile'
          },
          {
            genre_id: 'dance',
            player_type: 'JamRuby::User',
            genre_type: 'profile'
          }
        ],
        instruments: [
          {
            description: 'Acoustic Guitar',
            proficiency_level: 3,
            priority: 0,
            instrument_id: 'acoustic guitar'
          },
          {
            description: 'Bass Guitar',
            proficiency_level: 3,
            priority: 2,
            instrument_id: 'bass guitar'
          },
          {
            description: 'Violin',
            proficiency_level: 1,
            priority: 0,
            instrument_id: 'violin'
          },
          {
            description: 'Banjo',
            proficiency_level: 1,
            priority: 1,
            instrument_id: 'banjo'
          }
        ]
      }
    }).as('getProfile');

    cy.intercept('PUT', /\S+\/users\/\S+/, {
      statusCode: 200
    }).as('updateProfile');

    cy.visit('/profile');
  });

  it('should render the profile form with persisted data', () => {
    // Assert that the profile form is rendered
    cy.wait('@getProfile');
    cy.get('[data-testid=edit_profile_form]').within($form => {
      cy.get('[data-testid=firstName]').should('have.value', 'David');
      cy.get('[data-testid=lastName]').should('have.value', 'Wilson');
      cy.get('[data-testid=biography]').should('contain', 'This is the musician biography. It is a long form text');

      cy.get('[data-testid=subscribeEmail]').should('be.checked');
      cy.get('[data-testid=virtualBand]').should('be.checked');
      cy.get('[data-testid=traditionalBand]').should('not.be.checked');
      cy.get('[data-testid=cowriting]').should('be.checked');
      cy.get('[data-testid=instruments] input:checked').should('have.length', 4);
      cy.get('[data-testid=genres] input:checked').should('have.length', 3);
    });
  });

  it.only('should update the profile', () => {
    // Update the profile form

    cy.get('[data-testid=edit_profile_form]').within($form => {
      // Update the first name
      cy.get('[data-testid=firstName]')
        .clear()
        .type('Seth');

      // Update the last name
      cy.get('[data-testid=lastName]')
        .clear()
        .type('Call');

      cy.wait(2000);
    });

    cy.reload();

    cy.get('[data-testid=firstName]').should('have.value', 'Seth');
  });
});
