import XCTest
@testable import AIStudyApp

final class ReviewPlanViewModelTests: XCTestCase {

    var vm: ReviewPlanViewModel!

    override func setUp() {
        super.setUp()
        vm = ReviewPlanViewModel()
    }

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

    func testInitialState_hasTasks() {
        XCTAssertFalse(vm.todayTasks.isEmpty)
        XCTAssertFalse(vm.tomorrowTasks.isEmpty)
        XCTAssertFalse(vm.weekTasks.isEmpty)
    }

    func testTotalCount_sumsAllTasks() {
        let expected = vm.todayTasks.count + vm.tomorrowTasks.count + vm.weekTasks.count
        XCTAssertEqual(vm.totalCount, expected)
    }

    func testToggleTask_changesStatusToCompleted() {
        guard let task = vm.todayTasks.first(where: { $0.status == .pending }) else {
            XCTFail("Expected at least one pending task")
            return
        }
        vm.toggleTask(task)
        let toggled = vm.todayTasks.first(where: { $0.id == task.id })
        XCTAssertEqual(toggled?.status, .completed)
    }

    func testToggleTask_togglingBackChangesToPending() {
        guard let task = vm.todayTasks.first(where: { $0.status == .completed }) else {
            XCTFail("Expected at least one completed task")
            return
        }
        vm.toggleTask(task)
        let toggled = vm.todayTasks.first(where: { $0.id == task.id })
        XCTAssertEqual(toggled?.status, .pending)
    }

    func testToggleTask_doesNothingForUnknownTask() {
        let before = vm.totalCount
        let fake = ReviewTask(
            id: "non-existent", userId: "", lessonId: "",
            sourceSessionId: "", reviewType: .recall,
            scheduledAt: "", completedAt: nil, status: .pending
        )
        vm.toggleTask(fake)
        XCTAssertEqual(vm.totalCount, before)
    }
}
