import { seededUsers } from '../../support/seeded-users.generated';

describe('auth: login', () => {
  it('should login a user with username and password', () => {
    cy.visit('/');
    cy.get('input#email').type(seededUsers.owner.email);
    cy.get('input#password').type(seededUsers.owner.password);
    cy.get('form')
      .filter(':visible')
      .first()
      .within(() => {
        cy.contains('button', /^Sign In$/)
          .should('be.enabled')
          .click();
      });
    cy.location('pathname', { timeout: 30000 }).should((pathname) => {
      expect(pathname).to.satisfy(
        (p: string) =>
          p.includes('/tenants/') || p.includes('/onboarding/create-tenant'),
      );
    });

    cy.location('pathname').then((pathname) => {
      if (pathname.includes('/onboarding/create-tenant')) {
        const ts = Date.now();
        const tenantName = `CypressLoginTenant${String(ts).slice(-6)}`;
        cy.get('input#tenant-name')
          .filter(':visible')
          .first()
          .clear()
          .type(tenantName);
        cy.contains('button', 'Get started').click();
      }
    });
    cy.location('pathname', { timeout: 30000 }).should('include', '/tenants/');
    cy.get('[data-cy="v1-sidebar"]', { timeout: 30000 }).should('be.visible');
  });
});
