//
//  CableUITestsScreenshotLaunchTests.swift
//  CableUITestsScreenshot
//
//  Created by Stefan Lange-Hegermann on 06.10.25.
//

import XCTest

final class CableUITestsScreenshotLaunchTests: XCTestCase {
    
    
    private func takeScreenshot(name: String,
                                lifetime: XCTAttachment.Lifetime = .keepAlways) {
        let screenshot = XCUIScreen.main.screenshot()
        let attachment = XCTAttachment(screenshot: screenshot)
        attachment.name = name
        attachment.lifetime = lifetime
        add(attachment)
    }
    
    override class var runsForEachTargetApplicationUIConfiguration: Bool {
        false
    }
    
    override func setUpWithError() throws {
        continueAfterFailure = false
    }
    
    
    @MainActor
    func testOnboardingLoadsView() throws {
        let app = XCUIApplication()

        app.launch()
        takeScreenshot(name: "01-OnboardingSystemsView")
        
        let createSystemButton = app.buttons["create-system-button"]
        XCTAssertTrue(createSystemButton.waitForExistence(timeout: 5))
        createSystemButton.tap()
        takeScreenshot(name: "02-OnboardingLoadsView")
        
        let libraryCloseButton = app.buttons["library-view-close-button"]
        let selectComponentButton = app.buttons["select-component-button"]
        XCTAssertTrue(selectComponentButton.waitForExistence(timeout: 5))
        selectComponentButton.tap()
        XCTAssertTrue(libraryCloseButton.waitForExistence(timeout: 5))
        Thread.sleep(forTimeInterval: 10)
        takeScreenshot(name: "04-ComponentSelectorView")
        libraryCloseButton.tap()
        
        let createComponentButton = app.buttons["create-component-button"]
        XCTAssertTrue(createComponentButton.waitForExistence(timeout: 5))
        createComponentButton.tap()
        takeScreenshot(name: "03-LoadEditorView")
    }
    
    func testWithSampleData() throws {
        let app = XCUIApplication()
        app.launchArguments.append("--uitest-sample-data")
        app.launch()

        let systemsCollection = app.collectionViews.firstMatch
        let collectionExists = systemsCollection.waitForExistence(timeout: 3)

        let systemsList: XCUIElement
        if collectionExists {
            systemsList = systemsCollection
        } else {
            let table = app.tables.firstMatch
            XCTAssertTrue(table.waitForExistence(timeout: 3))
            systemsList = table
        }

        let firstSystemCell = systemsList.cells.element(boundBy: 0)
        XCTAssertTrue(firstSystemCell.waitForExistence(timeout: 3))

        takeScreenshot(name: "05-SystemsWithSampleData")

        firstSystemCell.tap()

        let loadsCollection = app.collectionViews["loads-list"]
        let loadsTable = app.tables["loads-list"]

        let loadsElement: XCUIElement
        if loadsCollection.waitForExistence(timeout: 3) {
            loadsElement = loadsCollection
        } else {
            XCTAssertTrue(loadsTable.waitForExistence(timeout: 3))
            loadsElement = loadsTable
        }

        XCTAssertTrue(loadsElement.cells.firstMatch.waitForExistence(timeout: 3))
        Thread.sleep(forTimeInterval: 1)
        takeScreenshot(name: "06-AdventureVanLoads")

        let bomButton = app.buttons["system-bom-button"]
        XCTAssertTrue(bomButton.waitForExistence(timeout: 2))
        bomButton.tap()

        let bomView = app.otherElements["system-bom-view"]
        XCTAssertTrue(bomView.waitForExistence(timeout: 3))
        Thread.sleep(forTimeInterval: 1)
        takeScreenshot(name: "07-AdventureVanBillOfMaterials")
    }
}
