import XCTest
@testable import PortfolioJournal

@MainActor
final class SettingsViewModelTests: XCTestCase {
    override func tearDown() {
        UserDefaults.standard.removeObject(forKey: "backupsEnabled")
        UserDefaults.standard.removeObject(forKey: "backupRetentionCount")
        UserDefaults.standard.removeObject(forKey: "debugPremiumOverride")
        super.tearDown()
    }

    func testSetBackupsEnabledNonPremiumShowsPaywall() {
        let iap = IAPService()
#if DEBUG
        iap.setPremiumForTesting(false)
#endif
        let viewModel = SettingsViewModel(iapService: iap)

        viewModel.setBackupsEnabled(true)

        XCTAssertFalse(viewModel.backupsEnabled)
        XCTAssertTrue(viewModel.showingPaywall)
    }

    func testSetBackupsEnabledPremiumPersistsFlag() {
#if DEBUG
        UserDefaults.standard.set(true, forKey: "debugPremiumOverride")
#endif
        let iap = IAPService()
#if DEBUG
        iap.setDebugPremiumOverride(true)
#endif
        let viewModel = SettingsViewModel(iapService: iap)

        viewModel.setBackupsEnabled(true)

        XCTAssertTrue(viewModel.backupsEnabled)
        XCTAssertFalse(viewModel.showingPaywall)
        XCTAssertTrue(UserDefaults.standard.bool(forKey: "backupsEnabled"))
    }
}
