Feature: Signup and login
  As a visitor
  I want to create a profile or log into an existing one
  So that I can access my household's batch-cooking planning

  Background:
    Given I am not signed in

  Scenario: Signing up creates a profile and starts the onboarding wizard
    Given the signup request will succeed
    And the diets reference list is empty
    When I sign up with:
      | firstName | Alice                        |
      | lastName  | Martin                       |
      | email     | alice@example.com            |
      | password  | correct-horse-battery-staple |
    Then the signup request should have been made
    And the URL should include "/onboarding/regime"
    And I should see "Étape 1 sur 3"

  Scenario: Signing up shows a client-side validation error without calling the API
    Given the signup request is being watched
    When I sign up with:
      | firstName | A             |
      | lastName  | B             |
      | email     | a@example.com |
      | password  | short         |
    Then I should see "8 caractères minimum"
    And the signup request should not have been made

  Scenario: Signing up shows the API's error when the email is already taken
    Given the signup request will fail because the email is already used
    When I sign up with:
      | firstName | Alice                        |
      | lastName  | Martin                       |
      | email     | alice@example.com            |
      | password  | correct-horse-battery-staple |
    Then the signup request should have been made
    And I should see "Cet email est déjà utilisé"

  Scenario: Logging in lands on the home page
    Given the login request will succeed
    And the planning request returns nothing
    When I log in with email "alice@example.com" and password "correct-horse-battery-staple"
    Then the login request should have been made
    And I should see "Bonjour Alice"

  Scenario: Logging in shows an error on invalid credentials
    Given the login request will fail because the credentials are invalid
    When I log in with email "alice@example.com" and password "wrong-password"
    Then the login request should have been made
    And I should see "Email ou mot de passe incorrect"

  Scenario: Logging out returns to the login page
    Given I am signed in as "Alice" "Martin"
    And my household id is 1
    And the planning request returns nothing
    And the logout request will succeed
    When I visit "/"
    And I open the account menu
    And I click the button "Se déconnecter"
    Then the logout request should have been made
    And the URL should include "/login"
