//
//  WildPhotographerReportTests.swift
//  suixinkanTests
//

import XCTest
@testable import suixinkan

/// 举报摄影师模块测试，覆盖 Mock 数据、提交校验、列表筛选和补充证据。
final class WildPhotographerReportTests: XCTestCase {
    func testSeedDataLoadsReportsAndRiskPoints() {
        let viewModel = WildPhotographerReportHomeViewModel()

        XCTAssertFalse(viewModel.reports.isEmpty)
        XCTAssertFalse(viewModel.riskPoints.isEmpty)
        XCTAssertEqual(viewModel.reports.count, WildPhotographerReportMockStore.seedReports.count)
    }

    func testSubmitReportGeneratesDynamicIDAndInsertsFirst() {
        let viewModel = WildPhotographerReportHomeViewModel()
        let beforeCount = viewModel.reports.count

        let record = viewModel.submitReport(
            reportType: .suspectedWild,
            description: "测试举报说明",
            location: "测试景区 · 测试点位",
            contact: "13800138000",
            imageCount: 1,
            videoCount: 0
        )

        XCTAssertEqual(viewModel.reports.count, beforeCount + 1)
        XCTAssertEqual(viewModel.reports.first?.id, record.id)
        XCTAssertTrue(record.id.hasPrefix("JB"))
        XCTAssertEqual(record.status, .pending)
        XCTAssertEqual(record.phoneNumber, "13800138000")
    }

    func testSubmitFailsWithoutEvidence() {
        let homeViewModel = WildPhotographerReportHomeViewModel()
        let viewModel = WildPhotographerReportSubmitViewModel(homeViewModel: homeViewModel)
        viewModel.removeAttachment(viewModel.images[0])
        viewModel.removeAttachment(viewModel.videos[0])
        viewModel.updateDescription("有说明")
        viewModel.applyResolvedLocation("测试位置")

        viewModel.submitReport()

        XCTAssertEqual(viewModel.validationMessage, "请至少上传一项现场证据。")
        XCTAssertNil(viewModel.submitResult)
    }

    func testSubmitFailsWithoutDescription() {
        let homeViewModel = WildPhotographerReportHomeViewModel()
        let viewModel = WildPhotographerReportSubmitViewModel(homeViewModel: homeViewModel)
        viewModel.updateDescription("   ")
        viewModel.applyResolvedLocation("测试位置")

        viewModel.submitReport()

        XCTAssertEqual(viewModel.validationMessage, "请填写举报说明。")
        XCTAssertNil(viewModel.submitResult)
    }

    func testSubmitFailsWithoutConfirmedLocation() {
        let homeViewModel = WildPhotographerReportHomeViewModel()
        let viewModel = WildPhotographerReportSubmitViewModel(homeViewModel: homeViewModel)
        viewModel.updateDescription("测试说明")

        viewModel.submitReport()

        XCTAssertEqual(viewModel.validationMessage, "请先更新举报位置后再提交举报。")
        XCTAssertNil(viewModel.submitResult)
    }

    func testSubmitSuccessCreatesRecord() {
        let homeViewModel = WildPhotographerReportHomeViewModel()
        let viewModel = WildPhotographerReportSubmitViewModel(homeViewModel: homeViewModel)
        viewModel.updateDescription("测试说明")
        viewModel.applyResolvedLocation("测试景区 · 测试点")

        viewModel.submitReport()

        XCTAssertNil(viewModel.validationMessage)
        XCTAssertNotNil(viewModel.submitResult)
        XCTAssertEqual(homeViewModel.reports.first?.id, viewModel.submitResult?.record.id)
    }

    func testListFilterReturnsMatchingStatus() {
        let homeViewModel = WildPhotographerReportHomeViewModel()
        let viewModel = WildPhotographerReportListViewModel(homeViewModel: homeViewModel)

        viewModel.selectFilter(.pending)
        XCTAssertTrue(viewModel.filteredReports.allSatisfy { $0.status == .pending })

        viewModel.selectFilter(.processing)
        XCTAssertTrue(viewModel.filteredReports.allSatisfy { $0.status == .processing })

        viewModel.selectFilter(.processed)
        XCTAssertTrue(viewModel.filteredReports.allSatisfy { $0.status == .processed })

        viewModel.selectFilter(.all)
        XCTAssertEqual(viewModel.filteredReports.count, homeViewModel.reports.count)
    }

    func testSupplementEvidenceValidationAndSuccess() {
        let homeViewModel = WildPhotographerReportHomeViewModel()
        let reportID = homeViewModel.reports[0].id
        let viewModel = WildReportSupplementEvidenceViewModel(reportID: reportID, homeViewModel: homeViewModel)

        viewModel.submit()
        XCTAssertEqual(viewModel.validationMessage, "请至少填写补充说明或上传一项证据。")

        viewModel.updateText("补充说明")
        viewModel.addImage()
        viewModel.submit()

        XCTAssertTrue(viewModel.didSubmit)
        XCTAssertEqual(homeViewModel.supplementaryEvidences(for: reportID).first?.text, "补充说明")
        XCTAssertEqual(homeViewModel.supplementaryEvidences(for: reportID).first?.imageCount, 1)
    }
}
