import XCTest
@testable import Rosetta

/// Tests for ChatList bottom inset bug fix.
/// Bug: Unwanted horizontal strip appears at bottom of ChatList (not in Calls/Settings).
/// Fix: keep platform-specific bottom inset (72 on iOS < 26 custom tab bar, 0 on iOS 26+).
final class ChatListBottomInsetTests: XCTestCase {

    var controller: ChatListCollectionController!

    override func setUp() {
        super.setUp()
        controller = ChatListCollectionController()
    }

    override func tearDown() {
        controller = nil
        super.tearDown()
    }

    private var expectedBottomInset: CGFloat {
        if #available(iOS 26, *) {
            return 0
        } else {
            return 72
        }
    }

    // MARK: - RED Phase Tests (These FAIL before fix, PASS after fix)

    /// Test 1: contentInset.bottom should match active tab implementation.
    func testContentInsetBottomMatchesPlatform() {
        // Force view load to trigger setupCollectionView()
        _ = controller.view

        let collectionView = controller.value(forKey: "collectionView") as? UICollectionView
        XCTAssertNotNil(collectionView, "Collection view should be initialized")

        // iOS 26+: native TabView handles inset automatically (0 here).
        // iOS < 26: custom floating tab bar needs 72pt manual inset.
        XCTAssertEqual(
            collectionView?.contentInset.bottom,
            expectedBottomInset,
            "Bottom inset should match platform-specific tab bar behavior"
        )
    }

    /// Test 2: Verify manual inset mode is enabled (UIKit auto-adjust disabled).
    func testContentInsetAdjustmentBehaviorIsNever() {
        _ = controller.view

        let collectionView = controller.value(forKey: "collectionView") as? UICollectionView
        XCTAssertNotNil(collectionView, "Collection view should be initialized")

        XCTAssertEqual(
            collectionView?.contentInsetAdjustmentBehavior,
            .never,
            "Should use manual inset mode for custom tab-bar safe area handling"
        )
    }

    /// Test 3: Verify no excessive bottom inset (must not exceed custom tab bar space).
    func testBottomInsetIsNotExcessive() {
        _ = controller.view

        let collectionView = controller.value(forKey: "collectionView") as? UICollectionView
        XCTAssertNotNil(collectionView, "Collection view should be initialized")

        let maxReasonableInset: CGFloat = 72
        XCTAssertLessThanOrEqual(
            collectionView?.contentInset.bottom ?? 0,
            maxReasonableInset,
            "Bottom inset should not exceed \(maxReasonableInset)pt"
        )
    }

    // MARK: - Integration Tests

    /// Test 4: Verify controller can be instantiated without crashes
    func testControllerInitialization() {
        XCTAssertNotNil(controller, "Controller should initialize successfully")
        _ = controller.view
        XCTAssertNotNil(controller.view, "View should load without crashes")
    }

    /// Test 5: Verify collection view constraints are properly set
    func testCollectionViewConstraints() {
        _ = controller.view

        let collectionView = controller.value(forKey: "collectionView") as? UICollectionView
        XCTAssertNotNil(collectionView, "Collection view should be initialized")

        // Collection view should be edge-to-edge (no custom insets)
        XCTAssertFalse(
            collectionView?.translatesAutoresizingMaskIntoConstraints ?? true,
            "Should use Auto Layout constraints"
        )
    }

    // MARK: - Performance Tests

    /// Test 6: Verify content inset query is fast (not computed on every access)
    func testContentInsetPerformance() {
        _ = controller.view

        let collectionView = controller.value(forKey: "collectionView") as? UICollectionView
        XCTAssertNotNil(collectionView)

        measure {
            for _ in 0..<1000 {
                _ = collectionView?.contentInset.bottom
            }
        }
    }
}
