//
//  lkfcBerichtTests.swift
//  LKF
//
//  Created by Alexander Holzer on 15.02.26.
//  Copyright © 2026 Alexander Holzer. All rights reserved.
//

import XCTest
@testable import lkfc

final class lkfcBerichtTests: XCTestCase {
    
    override func setUpWithError() throws {
        // Put setup code here. This method is called before the invocation of each test method in the class.
    }
    
    override func tearDownWithError() throws {
        // Put teardown code here. This method is called after the invocation of each test method in the class.
    }
    
    static var bericht1 = [
        "B01|2025|K901|K|E|D",
        "B02|2025|K901|K|K01|12159141|11004069",
        "B02|2025|K901|K|K03|11138836|10741347",
        "B02|2025|K901|K|K05|24866361|18296357",
        "B02|2025|K901|K|K08|57893397|51982275",
        "B02|2025|K901|K|V02|135326|158771",
        "B02|2025|K901|K|V03|10125061|10224869",
        "B02|2025|K901|K|V04|55723670|58761885",
        "B02|2025|K901|K|V05|96147|124412",
        "B02|2025|K901|K|V06|3926172|3968310",
        "B02|2025|K901|K|V07|1890241|456096",
        "B02|2025|K901|K|V08|35127630|28557626",
        "B02|2025|K901|K|V10|2103159|2035057",
        "B02|2025|K901|K|V11|62267234|49092230",
        "B02|2025|K901|K|V12|58493198|45305677",
        "B02|2025|K901|K|V13|8486810|8773732",
        "B02|2025|K901|K|V15|8042786|8439749",
        "B02|2025|K901|K|V19|283147|243741",
        "B02|2025|K901|K|V20|18491726|20889972",
        "B03|2025|K901|K|R01|92598588|91262652",
        "B03|2025|K901|K|R04|47203841|51056629",
        "B03|2025|K901|K|R05|24211616|18438220",
        "B03|2025|K901|K|R09|21826760|21826760",
        "B03|2025|K901|K|R11|-743629|-158957",
        "B04|2025|K901|K|A01|37514948|32371496",
        "B04|2025|K901|K|A02|113713488|91929477",
        "B04|2025|K901|K|A06|14588099|15728100",
        "B04|2025|K901|K|A12|7804753|7162100",
        "B04|2025|K901|K|A13|11142|11091",
        "B04|2025|K901|K|A16|4635|24257",
        "B04|2025|K901|K|A17|2967710|2076325",
        "B04|2025|K901|K|A18|6570004|132296",
        "B04|2025|K901|K|A21|5911122|2051660",
        "B04|2025|K901|K|A22|0|777186",
        "B04|2025|K901|K|A23|1830134|560102",
        "B04|2025|K901|K|E02|72047782|64535997",
        "B04|2025|K901|K|E04|78101|112653",
        "B04|2025|K901|K|E06|10533035|5407638",
        "B04|2025|K901|K|E11|68042419|56463444",
        "B04|2025|K901|K|E12|7868368|6989517",
        "B04|2025|K901|K|E13|7604374|6968044",
        "B04|2025|K901|K|E18|0|5901",
        "B04|2025|K901|K|E19|5173908|4655624",
        "B04|2025|K901|K|E27|761468|139243",
        "B04|2025|K901|K|E28|270105|75695",
        "B05|2025|K901|K|U01|44721883|44721883",
        "B05|2025|K901|K|U02|423624|423624",
        "B05|2025|K901|K|U04|1001355|0",
        "B05|2025|K901|K|U05|1040210|0",
        "B05|2025|K901|K|U06|779697|0",
        "B05|2025|K901|K|U07|24081014|24081014",
        "B05|2025|K901|K|U09|410928|410928",
        "B05|2025|K901|K|U11|1219627|0",
        "B05|2025|K901|K|U14|78101|0",
        "B05|2025|K901|K|U19|10533035|0",
        "B05|2025|K901|K|U20|8314534|8314534",
        "B05|2025|K901|K|U21|4477057|4477057",
        "B06|2025|K901|K|LD|Z02|2145995",
        "B06|2025|K901|K|LD|Z05|270105",
        "B06|2025|K901|K|LF|Z01|68042419",
        "B06|2025|K901|K|LF|Z02|4784391",
        "B06|2025|K901|K|RT|Z04|5173908",
        "B06|2025|K901|K|SO|Z02|937980"
    ];
    
    static var bericht1_add2024 = [
        "B04|2025|K901|K|E01|86548028|77739465",
        "B04|2025|K901|K|E03|14422145|13090816",
    ];

    static var bericht1_add2026EB0Q = [
        "B04|2025|K901|K|E01|86548028|77739465",
        "B04|2025|K901|K|E03|14422145|13090816",
        "B04|2025|K901|K|E41|741512|723500",
        "B05|2025|K901|K|U22|741512|723500",
    ];
    
    static var bericht1_add2026 = [
        "B04|2025|K901|K|E01|87289540|78462965",
        "B04|2025|K901|K|E03|14422145|13090816",
        "B04|2025|K901|K|E41|741512|723500",
        "B05|2025|K901|K|U22|741512|723500",
    ];

    static var bericht1_add2026EB1C = [
        "B04|2025|K901|K|E01|87289540|78462965",
        "B04|2025|K901|K|E03|14422145|13090816",
        "B04|2025|K901|K|E41|741312|723500",
        "B05|2025|K901|K|U22|741512|723500",
    ];

    func createContextWith (model: UInt32, year: Int32? = nil, flags: Int32 = 0, lines : [String]) throws -> LKFContext {
        let c = try lkfcTests.createContextWith(model:model, flags: flags, lines:lines)
        if let year {
            c.setContextParameters(year: year)
        }
        return c
    }
    
    func combine (_ first:[String], _ second:[String]) -> [String] {
        return lkfcTests.combine(first, second)
    }
    
    func replace (_ source: String, with target: String, in stringArray: [String]) -> [String] {
        return stringArray.map {
            $0.replacingOccurrences(of: source, with: target)
        }
    }

    func test1 () throws {
        
        let data2024 = combine(lkfcBerichtTests.bericht1, lkfcBerichtTests.bericht1_add2024)
        let data2026 = combine(lkfcBerichtTests.bericht1, lkfcBerichtTests.bericht1_add2026)

        var context = try createContextWith(model: 2024, year: 2025, lines: data2024)
        try context.execute()

        var errLines = context.getErrorsAndWarnings()
        XCTAssert(errLines.count == 3, "expect EK71, EK76, and headline only")
        
        context = try createContextWith(model: 2025, year: 2025, lines: data2024)
        try context.execute()
        errLines = context.getErrorsAndWarnings()
        XCTAssert(errLines.count == 3, "expect EK71, EK76, and headline only")
                
        context = try createContextWith(model: 2026, year: 2025, lines: data2024)
        try context.execute()
        errLines = context.getErrorsAndWarnings()
        XCTAssert(errLines.count == 3, "expect EK71, EK76, and headline only")
        
        context = try createContextWith(model: 2024, year: 2025, lines: data2026)
        try context.execute()
                
        var errors = context.errorsWith (code:"EB0C")
        XCTAssert(errors != nil, "EB0C expected in model 2024")
        errors = context.errorsWith (code:"EB0D")
        XCTAssert(errors != nil, "EB0D expected in model 2024")
        errors = context.errorsWith (code:"EB0Q")
        XCTAssert(errors != nil, "EB0Q expected in model 2024")
        errors = context.errorsWith (code:"EB13")
        XCTAssert(errors != nil, "EB13 expected in model 2024")
        errLines = context.getErrorsAndWarnings()
        XCTAssert(errLines.count == 7, "expect EK71, EK76, headline, and 4 EB-errors")

                  
        context = try createContextWith(model: 2025, year: 2025, lines: data2026)
        try context.execute()
        errLines = context.getErrorsAndWarnings()
        XCTAssert(errLines.count == 3, "expect EK71, EK76, and headline only (no EB0Q, EB1C)")

        context = try createContextWith(model: 2026, year: 2025, lines: data2026)
        try context.execute()
        errLines = context.getErrorsAndWarnings()
        XCTAssert(errLines.count == 3, "expect EK71, EK76, and headline only (no EB0Q, EB1C)")

        
        let data2026EB1C = combine(lkfcBerichtTests.bericht1, lkfcBerichtTests.bericht1_add2026EB1C)

        context = try createContextWith(model: 2025, year: 2025, lines: data2026EB1C)
        try context.execute()
        
        errors = context.errorsWith (code:"EB0Q")
        XCTAssert(errors == nil, "EB0Q not expected in model 2025")
        errors = context.errorsWith (code:"EB1C")
        XCTAssert(errors != nil, "EB1C expected in model 2025")
        errors = context.errorsWith (code:"EB13")
        XCTAssert(errors != nil, "EB13 expected in model 2025")
        errLines = context.getErrorsAndWarnings()
        XCTAssert(errLines.count == 5, "expect EK71, EK76, and 2 EB-Errors")

        
        context = try createContextWith(model: 2026, year: 2025, lines: data2026EB1C)
        try context.execute()

        errors = context.errorsWith (code:"EB0Q")
        XCTAssert(errors == nil, "EB0Q not expected in model 2026")
        errors = context.errorsWith (code:"EB1C")
        XCTAssert(errors != nil, "EB1C expected in model 2026")
        errors = context.errorsWith (code:"EB13")
        XCTAssert(errors != nil, "EB13 expected in model 2025")
        errLines = context.getErrorsAndWarnings()
        XCTAssert(errLines.count == 5, "expect EK71, EK76, and 2 EB-Errors")

        
        let data2026EB0Q = combine(lkfcBerichtTests.bericht1, lkfcBerichtTests.bericht1_add2026EB0Q)

        context = try createContextWith(model: 2025, year: 2025, lines: data2026EB0Q)
        try context.execute()
        
        errors = context.errorsWith (code:"EB0Q")
        XCTAssert(errors != nil, "EB0Q expected in model 2025")
        errors = context.errorsWith (code:"EB1C")
        XCTAssert(errors == nil, "EB1C not expected in model 2025")
        errors = context.errorsWith (code:"EB13")
        XCTAssert(errors != nil, "EB13 expected in model 2025")

        context = try createContextWith(model: 2026, year: 2025, lines: data2026EB0Q)
        try context.execute()

        errors = context.errorsWith (code:"EB0Q")
        XCTAssert(errors != nil, "EB0Q expected in model 2026")
        errors = context.errorsWith (code:"EB1C")
        XCTAssert(errors == nil, "EB1C not expected in model 2026")
        errors = context.errorsWith (code:"EB13")
        XCTAssert(errors != nil, "EB13 expected in model 2025")

    }
    
}
