import { chromium, FullConfig, Page } from '@playwright/test';
import {user1, user2 } from '../data/users';

async function signup(page: Page, email:string, password:string, first_name:string, Last_name:string){
  await page.goto('http://www.jamkazam.local:3000/sigup');
  await page.fill('input#jam_ruby_user_first_name', first_name);  
  await page.fill('input#jam_ruby_user_last_name', Last_name);
  await page.fill('input#jam_ruby_user_email', email);
  await page.fill('input#jam_ruby_user_password', password);
  await page.fill('input#jam_ruby_user_password_confirmation', password);
  await page.locator('input#jam_ruby_user_terms_of_service').check();
  await page.click('input[type="submit"]');
}

async function login(page: Page, email: string, password: string){
  await page.goto('http://www.jamkazam.local:3000/signin');   
  await page.fill('input#session_email', email);  
  await page.fill('input#session_password', password);  
  await page.click('input[type="submit"]');
}

async function globalSetup(config: FullConfig) {
  const browser = await chromium.launch();

  const page1 = await browser.newPage();
  // Skip signup - use existing user1 account
  // await signup(page1, user1.email, user1.password, user1.first_name, user1.last_name)
  // ... log in user1
  await login(page1, user1.email, user1.password)
  await page1.context().storageState({ path: 'test/storageState/user1.json' });

  const page2 = await browser.newPage();
  // Skip signup - use existing user2 account
  // await signup(page2, user2.email, user2.password, user2.first_name, user2.last_name)
  // ... log in
  await login(page2, user2.email, user2.password)
  await page2.context().storageState({ path: 'test/storageState/user2.json' });

  await browser.close();
}

async function globalTeardown(){
  //TODO: delete created users
}

export default globalSetup;