Feature: Hosted Session Automation
  As a league organizer using the GridPilot companion app
  I want to automate the iRacing hosted session creation workflow
  So that I can quickly set up race sessions without manual data entry

  Background:
    Given the companion app is running
    And I am authenticated with iRacing
    And I have a valid session configuration

  Scenario: Complete 18-step automation workflow
    Given I have a session configuration with:
      | field        | value                |
      | sessionName  | League Race Week 1   |
      | trackId      | spa                  |
      | carIds       | dallara-f3           |
    When I start the automation session
    Then the session should be created with state "PENDING"
    And the current step should be 1
    
    When the automation progresses through all 18 steps
    Then step 1 should navigate to "Hosted Racing"
    And step 2 should click "Create a Race"
    And step 3 should fill "Race Information"
    And step 4 should configure "Server Details"
    And step 5 should access "Set Admins"
    And step 6 should handle "Add an Admin" modal
    And step 7 should set "Time Limits"
    And step 8 should access "Set Cars"
    And step 9 should handle "Add a Car" modal
    And step 10 should configure "Set Car Classes"
    And step 11 should access "Set Track"
    And step 12 should handle "Add a Track" modal
    And step 13 should configure "Track Options"
    And step 14 should set "Time of Day"
    And step 15 should configure "Weather"
    And step 16 should set "Race Options"
    And step 17 should configure "Team Driving"
    And step 18 should reach "Track Conditions"
    
    And the session should stop at step 18
    And the session state should be "STOPPED_AT_STEP_18"
    And a manual submit warning should be displayed

  Scenario: Modal step handling (step 6 - Add Admin)
    Given I have started an automation session
    And the automation has reached step 6
    When the "Add an Admin" modal appears
    Then the automation should detect the modal
    And the automation should wait for modal content to load
    And the automation should fill admin fields
    And the automation should close the modal
    And the automation should transition to step 7

  Scenario: Modal step handling (step 9 - Add Car)
    Given I have started an automation session
    And the automation has reached step 9
    When the "Add a Car" modal appears
    Then the automation should detect the modal
    And the automation should select the car "dallara-f3"
    And the automation should confirm the selection
    And the automation should close the modal
    And the automation should transition to step 10

  Scenario: Modal step handling (step 12 - Add Track)
    Given I have started an automation session
    And the automation has reached step 12
    When the "Add a Track" modal appears
    Then the automation should detect the modal
    And the automation should select the track "spa"
    And the automation should confirm the selection
    And the automation should close the modal
    And the automation should transition to step 13

  Scenario: Safety checkpoint at step 18
    Given I have started an automation session
    And the automation has progressed to step 17
    When the automation transitions to step 18
    Then the automation should automatically stop
    And the session state should be "STOPPED_AT_STEP_18"
    And the current step should be 18
    And no submit action should be executed
    And a notification should inform the user to review before submitting

  Scenario: Pause and resume automation
    Given I have started an automation session
    And the automation is at step 5
    When I pause the automation
    Then the session state should be "PAUSED"
    And the current step should remain 5
    
    When I resume the automation
    Then the session state should be "IN_PROGRESS"
    And the automation should continue from step 5

  Scenario: Automation failure handling
    Given I have started an automation session
    And the automation is at step 8
    When a browser automation error occurs
    Then the session should transition to "FAILED" state
    And an error message should be recorded
    And the session should have a completedAt timestamp
    And the user should be notified of the failure

  Scenario: Invalid configuration rejection
    Given I have a session configuration with:
      | field        | value     |
      | sessionName  |           |
      | trackId      | spa       |
      | carIds       | dallara-f3|
    When I attempt to start the automation session
    Then the session creation should fail
    And an error message should indicate "Session name cannot be empty"
    And no session should be persisted

  Scenario: Sequential step progression enforcement
    Given I have started an automation session
    And the automation is at step 5
    When I attempt to skip directly to step 7
    Then the transition should be rejected
    And an error message should indicate "Cannot skip steps"
    And the current step should remain 5

  Scenario: Backward step prevention
    Given I have started an automation session
    And the automation has reached step 10
    When I attempt to move back to step 9
    Then the transition should be rejected
    And an error message should indicate "Cannot move backward"
    And the current step should remain 10

  Scenario: Multiple car selection
    Given I have a session configuration with:
      | field        | value                                    |
      | sessionName  | Multi-class Race                         |
      | trackId      | spa                                      |
      | carIds       | dallara-f3,porsche-911-gt3,bmw-m4-gt4   |
    When I start the automation session
    And the automation reaches step 9
    Then all three cars should be added via the modal
    And the automation should handle the modal three times
    And the automation should transition to step 10

  Scenario: Session state persistence
    Given I have started an automation session
    And the automation has reached step 12
    When the application restarts
    Then the session should be recoverable from storage
    And the session state should be "IN_PROGRESS"
    And the current step should be 12
    And the session configuration should be intact

  Scenario: Concurrent session prevention
    Given I have started an automation session
    And the session is in progress
    When I attempt to start another automation session
    Then the second session creation should be queued or rejected
    And a warning should inform about the active session

  Scenario: Elapsed time tracking
    Given I have started an automation session
    When the automation runs for 5 seconds
    And I query the session status
    Then the elapsed time should be approximately 5000 milliseconds
    And the elapsed time should increase while in progress

  Scenario: Complete workflow with realistic timings
    Given I have a session configuration
    When I start the automation session
    Then each step should take between 200ms and 1000ms
    And modal steps should take longer than regular steps
    And the total workflow should complete in under 30 seconds
    And the session should stop at step 18 without submitting