import Foundation
import SwiftLintCore
import TestHelpers
import Testing

@testable import SwiftLintBuiltInRules

private let fixturesDirectory = "\(TestResources.path())/FileHeaderRuleFixtures"

@Suite(.rulesRegistered)
struct FileHeaderRuleTests {
    private func validate(fileName: String, using configuration: Any) throws -> [StyleViolation] {
        let file = TestResources.path()
            .appending(path: "FileHeaderRuleFixtures", directoryHint: .isDirectory)
            .appending(path: fileName, directoryHint: .notDirectory)
        let rule = try FileHeaderRule(configuration: configuration)
        return rule.validate(file: SwiftLintFile(path: file)!)
    }

    @Test
    func fileHeaderWithDefaultConfiguration() {
        verifyRule(FileHeaderRule.description, skipCommentTests: true)
    }

    @Test
    func fileHeaderWithRequiredString() {
        let nonTriggeringExamples = #examples([
            "// **Header",
            "//\n// **Header",
        ])
        let triggeringExamples = #examples([
            "↓// Copyright\n",
            "let foo = \"**Header\"",
            "let foo = 2 // **Header",
            "let foo = 2\n// **Header",
            "let foo = 2 // **Header",
        ])
        let description = FileHeaderRule.description
            .with(nonTriggeringExamples: nonTriggeringExamples)
            .with(triggeringExamples: triggeringExamples)

        verifyRule(
            description,
            ruleConfiguration: ["required_string": "**Header"],
            stringDoesntViolate: false,
            skipCommentTests: true,
            skipDisableCommandTests: true,
            testMultiByteOffsets: false,
            testShebang: false
        )
    }

    @Test
    func fileHeaderWithRequiredPattern() {
        let nonTriggeringExamples = #examples([
            "// Copyright © 2016 Realm",
            "//\n// Copyright © 2016 Realm)",
        ])
        let triggeringExamples = #examples([
            "↓// Copyright\n",
            "↓// Copyright © foo Realm",
            "↓// Copyright © 2016 MyCompany",
        ])
        let description = FileHeaderRule.description
            .with(nonTriggeringExamples: nonTriggeringExamples)
            .with(triggeringExamples: triggeringExamples)

        verifyRule(description, ruleConfiguration: ["required_pattern": "\\d{4} Realm"],
                   stringDoesntViolate: false, skipCommentTests: true,
                   testMultiByteOffsets: false)
    }

    @Test
    func fileHeaderWithRequiredStringAndURLComment() {
        let nonTriggeringExamples = #examples([
            "/* Check this url: https://github.com/realm/SwiftLint */"
        ])
        let triggeringExamples = #examples([
            "/* Check this url: https://github.com/apple/swift */"
        ])
        let description = FileHeaderRule.description
            .with(nonTriggeringExamples: nonTriggeringExamples)
            .with(triggeringExamples: triggeringExamples)

        let config = ["required_string": "/* Check this url: https://github.com/realm/SwiftLint */"]
        verifyRule(description, ruleConfiguration: config,
                   stringDoesntViolate: false, skipCommentTests: true,
                   testMultiByteOffsets: false)
    }

    @Test
    func fileHeaderWithForbiddenString() {
        let nonTriggeringExamples = #examples([
            "// Copyright\n",
            "let foo = \"**All rights reserved.\"",
            "let foo = 2 // **All rights reserved.",
            "let foo = 2\n// **All rights reserved.",
            "let foo = 2 // **All rights reserved.",
        ])
        let triggeringExamples = #examples([
            "// ↓**All rights reserved.",
            "//\n// ↓**All rights reserved.",
        ])
        let description = FileHeaderRule.description
            .with(nonTriggeringExamples: nonTriggeringExamples)
            .with(triggeringExamples: triggeringExamples)

        verifyRule(description, ruleConfiguration: ["forbidden_string": "**All rights reserved."],
                   skipCommentTests: true)
    }

    @Test
    func fileHeaderWithForbiddenPattern() {
        let nonTriggeringExamples = #examples([
            "// Copyright\n",
            "// FileHeaderRuleTests.m\n",
            "let foo = \"FileHeaderRuleTests.swift\"",
            "let foo = 2 // FileHeaderRuleTests.swift.",
            "let foo = 2\n // FileHeaderRuleTests.swift.",
        ])
        let triggeringExamples = #examples([
            "//↓ FileHeaderRuleTests.swift",
            "//\n//↓ FileHeaderRuleTests.swift",
        ])
        let description = FileHeaderRule.description
            .with(nonTriggeringExamples: nonTriggeringExamples)
            .with(triggeringExamples: triggeringExamples)

        verifyRule(description, ruleConfiguration: ["forbidden_pattern": "\\s\\w+\\.swift"],
                   skipCommentTests: true)
    }

    @Test
    func fileHeaderWithForbiddenPatternAndDocComment() {
        let nonTriggeringExamples = #examples([
            "/// This is great tool with tests.\nclass GreatTool {}",
            "class GreatTool {}",
        ])
        let triggeringExamples = #examples([
            "// FileHeaderRule↓Tests.swift",
            "//\n// FileHeaderRule↓Tests.swift",
        ])
        let description = FileHeaderRule.description
            .with(nonTriggeringExamples: nonTriggeringExamples)
            .with(triggeringExamples: triggeringExamples)

        verifyRule(description, ruleConfiguration: ["forbidden_pattern": "[tT]ests"],
                   skipCommentTests: true, testMultiByteOffsets: false)
    }

    @Test
    func fileHeaderWithRequiredStringUsingFilenamePlaceholder() throws {
        let configuration = ["required_string": "// SWIFTLINT_CURRENT_FILENAME"]

        // Non triggering tests
        try #expect(validate(fileName: "FileNameMatchingSimple.swift", using: configuration).isEmpty)

        // Triggering tests
        try #expect(validate(fileName: "FileNameCaseMismatch.swift", using: configuration).count == 1)
        try #expect(validate(fileName: "FileNameMismatch.swift", using: configuration).count == 1)
        try #expect(validate(fileName: "FileNameMissing.swift", using: configuration).count == 1)
    }

    @Test
    func fileHeaderWithForbiddenStringUsingFilenamePlaceholder() throws {
        let configuration = ["forbidden_string": "// SWIFTLINT_CURRENT_FILENAME"]

        // Non triggering tests
        try #expect(validate(fileName: "FileNameCaseMismatch.swift", using: configuration).isEmpty)
        try #expect(validate(fileName: "FileNameMismatch.swift", using: configuration).isEmpty)
        try #expect(validate(fileName: "FileNameMissing.swift", using: configuration).isEmpty)

        // Triggering tests
        #expect(try validate(fileName: "FileNameMatchingSimple.swift", using: configuration).count == 1)
    }

    @Test
    func fileHeaderWithRequiredPatternUsingFilenamePlaceholder() throws {
        let configuration1 = ["required_pattern": "// SWIFTLINT_CURRENT_FILENAME\n.*\\d{4}"]
        let configuration2 = [
            "required_pattern": "// Copyright © \\d{4}\n// File: \"SWIFTLINT_CURRENT_FILENAME\""
        ]

        // Non triggering tests
        try #expect(validate(fileName: "FileNameMatchingSimple.swift", using: configuration1).isEmpty)
        try #expect(validate(fileName: "FileNameMatchingComplex.swift", using: configuration2).isEmpty)

        // Triggering tests
        try #expect(validate(fileName: "FileNameCaseMismatch.swift", using: configuration1).count == 1)
        try #expect(validate(fileName: "FileNameMismatch.swift", using: configuration1).count == 1)
        try #expect(validate(fileName: "FileNameMissing.swift", using: configuration1).count == 1)
    }

    @Test
    func fileHeaderWithForbiddenPatternUsingFilenamePlaceholder() throws {
        let configuration1 = ["forbidden_pattern": "// SWIFTLINT_CURRENT_FILENAME\n.*\\d{4}"]
        let configuration2 = ["forbidden_pattern": "//.*(\\s|\")SWIFTLINT_CURRENT_FILENAME(\\s|\").*"]

        // Non triggering tests
        try #expect(validate(fileName: "FileNameCaseMismatch.swift", using: configuration1).isEmpty)
        try #expect(validate(fileName: "FileNameMismatch.swift", using: configuration1).isEmpty)
        try #expect(validate(fileName: "FileNameMissing.swift", using: configuration1).isEmpty)

        try #expect(validate(fileName: "FileNameCaseMismatch.swift", using: configuration2).isEmpty)
        try #expect(validate(fileName: "FileNameMismatch.swift", using: configuration2).isEmpty)
        try #expect(validate(fileName: "FileNameMissing.swift", using: configuration2).isEmpty)

        // Triggering tests
        try #expect(validate(fileName: "FileNameMatchingSimple.swift", using: configuration1).count == 1)
        try #expect(validate(fileName: "FileNameMatchingComplex.swift", using: configuration2).count == 1)
    }

    @Test
    func fileHeaderShouldBeEmpty() throws {
        let configuration = ["forbidden_pattern": "."]

        // Non triggering tests
        try #expect(validate(fileName: "FileHeaderEmpty.swift", using: configuration).isEmpty)
        try #expect(validate(fileName: "DocumentedType.swift", using: configuration).isEmpty)

        // Triggering tests
        try #expect(validate(fileName: "FileNameCaseMismatch.swift", using: configuration).count == 1)
        try #expect(validate(fileName: "FileNameMismatch.swift", using: configuration).count == 1)
        try #expect(validate(fileName: "FileNameMissing.swift", using: configuration).count == 1)
    }

    @Test
    func simplePattern() {
        let description = FileHeaderRule.description
            .with(nonTriggeringExamples: #examples([
                """
                    // Test

                    enum Test {}
                    """,
                """
                    // Test
                    """,
                """
                    // Test

                    """,
            ]))
            .with(triggeringExamples: [])

        verifyRule(
            description,
            ruleConfiguration: [
                "required_pattern": #"""
                    \/\/ Test

                    """#, // The empty line at the end is important since YAML adds it as well in `|` blocks.
            ],
            skipCommentTests: true,
            testMultiByteOffsets: false
        )
    }

    @Test
    func pattern() {
        let description = FileHeaderRule.description
            .with(nonTriggeringExamples: #examples([
                """
                    //
                    //  Test.swift
                    //  Dummy App
                    //
                    //  Created by Alice Bob on 3.9.2025.
                    //  Copyright © 2025 Dummy Corporation. All rights reserved.
                    //

                    enum Test {}
                    """,
            ]))
            .with(triggeringExamples: [])

        verifyRule(
            description,
            ruleConfiguration: [
                "required_pattern": #"""
                    \/\/
                    \/\/  Test\.swift
                    \/\/  .*?
                    \/\/
                    \/\/  Created by .*? on \d{1,2}[\.\/]\d{1,2}[\.\/]\d{2,4}\.
                    \/\/  Copyright © \d{4} Dummy Corporation\. All rights reserved\.
                    \/\/

                    """#, // The empty line at the end is important since YAML adds it as well in `|` blocks.
            ],
            skipCommentTests: true,
            testMultiByteOffsets: false
        )
    }
}
