import XCTest

/// Phase 1 — Seed tests run sequentially before parallel suites.
/// Ensures the backend is reachable and required test accounts exist.
final class AAA_SeedTests: XCTestCase {

    override func setUp() {
        super.setUp()
        continueAfterFailure = false
    }

    // MARK: - Gate Check

    func testSeed01_backendReachable() throws {
        guard TestAccountAPIClient.isBackendReachable() else {
            throw XCTSkip("Backend is not reachable at \(TestAccountAPIClient.baseURL) — skipping all seed tests")
        }
    }

    // MARK: - Test User

    func testSeed02_ensureTestUserExists() {
        let username = "testuser"
        let password = "TestPass123!"
        let email = "\(username)@honeydue.com"

        // Try logging in first — account may already exist
        if let _ = TestAccountAPIClient.login(username: username, password: password) {
            return // already exists and credentials work
        }

        // Create and verify the account
        let session = TestAccountAPIClient.createVerifiedAccount(
            username: username,
            email: email,
            password: password
        )
        XCTAssertNotNil(session, "Failed to create verified test user '\(username)'")
    }

    // MARK: - Admin User

    func testSeed03_ensureAdminExists() {
        let username = "admin"
        let password = "Test1234"
        let email = "\(username)@honeydue.com"

        if let _ = TestAccountAPIClient.login(username: username, password: password) {
            return
        }

        let session = TestAccountAPIClient.createVerifiedAccount(
            username: username,
            email: email,
            password: password
        )
        XCTAssertNotNil(session, "Failed to create verified admin user '\(username)'")
    }
}
