using NUnit.Framework;
using NcProgramManager.Cnc;
using NcProgramManager.Cnc.Models;

namespace NcProgramManager.Tests.Unit
{
    [TestFixture]
    public class NcProgramValidatorTests
    {
        // ── Universal rules ───────────────────────────────────────────────────

        [Test]
        public void Validate_BlockTooLong_ReturnsError()
        {
            var v = new NcProgramValidator(CncManufacturer.Fanuc);
            string longLine = "O1234\n" + new string('X', 81);
            var r = v.Validate("O1234", longLine);
            Assert.That(r.Success, Is.False);
            Assert.That(HasErrorContaining(r, "80"), Is.True);
        }

        [Test]
        public void Validate_BlockExactly80_NoLengthError()
        {
            var v = new NcProgramValidator(CncManufacturer.Fanuc);
            string line80 = new string('G', 80);
            var r = v.Validate("O1234", "O1234\n" + line80);
            // Should not have a block-length error (may still have other errors)
            Assert.That(HasErrorContaining(r, "Block length"), Is.False);
        }

        [Test]
        public void Validate_LowercaseLetter_ReturnsError()
        {
            var v = new NcProgramValidator(CncManufacturer.Siemens);
            var r = v.Validate("PROG.MPF", "PROG.MPF\ng00 X10");
            Assert.That(r.Success, Is.False);
            Assert.That(HasErrorContaining(r, "Lowercase"), Is.True);
        }

        [Test]
        public void Validate_NoLowercase_NoLowercaseError()
        {
            var v = new NcProgramValidator(CncManufacturer.Siemens);
            var r = v.Validate("PROG.MPF", "G00 X10 Y20");
            Assert.That(HasErrorContaining(r, "Lowercase"), Is.False);
        }

        [Test]
        public void Validate_TabCharacter_ReturnsError()
        {
            var v = new NcProgramValidator(CncManufacturer.Siemens);
            var r = v.Validate("PROG.MPF", "G00\tX10");
            Assert.That(r.Success, Is.False);
            Assert.That(HasErrorContaining(r, "Tab"), Is.True);
        }

        [Test]
        public void Validate_NestedParentheses_ReturnsError()
        {
            var v = new NcProgramValidator(CncManufacturer.Fanuc);
            var r = v.Validate("O1234", "O1234\nG00 ((NESTED))");
            Assert.That(r.Success, Is.False);
            Assert.That(HasErrorContaining(r, "Nested"), Is.True);
        }

        [Test]
        public void Validate_SingleParenComment_NoNestedError()
        {
            var v = new NcProgramValidator(CncManufacturer.Fanuc);
            var r = v.Validate("O1234", "O1234\nG00 (COMMENT) X10");
            Assert.That(HasErrorContaining(r, "Nested"), Is.False);
        }

        [Test]
        public void Validate_CommentExceeds32Chars_ReturnsError()
        {
            var v = new NcProgramValidator(CncManufacturer.Fanuc);
            string longComment = "(" + new string('A', 33) + ")";
            var r = v.Validate("O1234", "O1234\n" + longComment);
            Assert.That(r.Success, Is.False);
            Assert.That(HasErrorContaining(r, "Comment length"), Is.True);
        }

        [Test]
        public void Validate_CommentExactly32Chars_NoCommentLengthError()
        {
            var v = new NcProgramValidator(CncManufacturer.Fanuc);
            string comment = "(" + new string('A', 32) + ")";
            var r = v.Validate("O1234", "O1234\n" + comment);
            Assert.That(HasErrorContaining(r, "Comment length"), Is.False);
        }

        // ── Fanuc rules ───────────────────────────────────────────────────────

        [Test]
        public void Validate_Fanuc_ValidNameAndContent_Succeeds()
        {
            var v = new NcProgramValidator(CncManufacturer.Fanuc);
            var r = v.Validate("O1234", "O1234\nG00 X10 Y20\nM30");
            Assert.That(r.Success, Is.True);
        }

        [Test]
        public void Validate_Fanuc_NameNotOFourDigits_ReturnsError()
        {
            var v = new NcProgramValidator(CncManufacturer.Fanuc);
            var r = v.Validate("PROG", "O1234\nG00");
            Assert.That(r.Success, Is.False);
            Assert.That(HasErrorContaining(r, "O####"), Is.True);
        }

        [Test]
        public void Validate_Fanuc_NameO12345_ReturnsError()
        {
            var v = new NcProgramValidator(CncManufacturer.Fanuc);
            // 5 digits — invalid for Fanuc
            var r = v.Validate("O12345", "O1234\nG00");
            Assert.That(r.Success, Is.False);
            Assert.That(HasErrorContaining(r, "O####"), Is.True);
        }

        [Test]
        public void Validate_Fanuc_FirstLineNotONumber_ReturnsError()
        {
            var v = new NcProgramValidator(CncManufacturer.Fanuc);
            var r = v.Validate("O1234", "G00 X10\nM30");
            Assert.That(r.Success, Is.False);
            Assert.That(HasErrorContaining(r, "First line"), Is.True);
        }

        [TestCase(CncManufacturer.Fanuc,      "O1234",    "O1234\nG00 X10;")]
        [TestCase(CncManufacturer.Mitsubishi, "O1234",    "O1234\nG00 X10;")]
        [TestCase(CncManufacturer.Siemens,    "PROG.MPF", "G00 X10;")]
        [TestCase(CncManufacturer.Heidenhain, "PROG.H",   "L X+10;")]
        public void Validate_SemicolonForbidden_ReturnsError(CncManufacturer m, string name, string content)
        {
            var r = new NcProgramValidator(m).Validate(name, content);
            Assert.That(r.Success, Is.False);
            Assert.That(HasErrorContaining(r, ";"), Is.True);
        }

        [Test]
        public void Validate_Fanuc_BracketForbidden_ReturnsError()
        {
            var v = new NcProgramValidator(CncManufacturer.Fanuc);
            var r = v.Validate("O1234", "O1234\nG00 [X10]");
            Assert.That(r.Success, Is.False);
            Assert.That(HasErrorContaining(r, "["), Is.True);
        }

        [Test]
        public void Validate_Fanuc_MacroHash_ReturnsWarningNotError()
        {
            var v = new NcProgramValidator(CncManufacturer.Fanuc);
            var r = v.Validate("O1234", "O1234\n#100=10");
            // Warnings alone do not cause failure
            Assert.That(r.Success, Is.True);
            Assert.That(HasWarningContaining(r, "#"), Is.True);
        }

        [Test]
        public void Validate_Fanuc_NameFromPathStripped()
        {
            var v = new NcProgramValidator(CncManufacturer.Fanuc);
            // Full path — name part is O1234
            var r = v.Validate("//CNC_MEM/USER/PATH1/O1234", "O1234\nG00 X10");
            Assert.That(HasErrorContaining(r, "O####"), Is.False);
        }

        // ── Mitsubishi rules ──────────────────────────────────────────────────

        [Test]
        public void Validate_Mitsubishi_ValidNameAndContent_Succeeds()
        {
            var v = new NcProgramValidator(CncManufacturer.Mitsubishi);
            var r = v.Validate("O1234", "O1234\nG00 X10\nM30");
            Assert.That(r.Success, Is.True);
        }

        [Test]
        public void Validate_Mitsubishi_BadName_ReturnsError()
        {
            var v = new NcProgramValidator(CncManufacturer.Mitsubishi);
            var r = v.Validate("PROG", "O1234\nG00");
            Assert.That(r.Success, Is.False);
            Assert.That(HasErrorContaining(r, "O####"), Is.True);
        }

        // ── Siemens rules ─────────────────────────────────────────────────────

        [Test]
        public void Validate_Siemens_ValidNameAndContent_Succeeds()
        {
            var v = new NcProgramValidator(CncManufacturer.Siemens);
            var r = v.Validate("PROG.MPF", "G00 X10 Y20\nM30");
            Assert.That(r.Success, Is.True);
        }

        [Test]
        public void Validate_Siemens_NameNoDotMpf_ReturnsError()
        {
            var v = new NcProgramValidator(CncManufacturer.Siemens);
            var r = v.Validate("PROG", "G00 X10");
            Assert.That(r.Success, Is.False);
            Assert.That(HasErrorContaining(r, ".MPF"), Is.True);
        }

        [Test]
        public void Validate_Siemens_LowercaseMpf_ReturnsNameError()
        {
            var v = new NcProgramValidator(CncManufacturer.Siemens);
            var r = v.Validate("prog.mpf", "G00 X10");
            Assert.That(r.Success, Is.False);
            Assert.That(HasErrorContaining(r, ".MPF"), Is.True);
        }

        [Test]
        public void Validate_Siemens_NoFirstLineRequired()
        {
            var v = new NcProgramValidator(CncManufacturer.Siemens);
            var r = v.Validate("PROG.MPF", "G00 X10\nM30");
            Assert.That(HasErrorContaining(r, "First line"), Is.False);
        }

        // ── Heidenhain rules ──────────────────────────────────────────────────

        [Test]
        public void Validate_Heidenhain_ValidNameAndContent_Succeeds()
        {
            var v = new NcProgramValidator(CncManufacturer.Heidenhain);
            var r = v.Validate("PROG.H", "L X+10 Y+20 R0 FMAX\nL Z-5 F500");
            Assert.That(r.Success, Is.True);
        }

        [Test]
        public void Validate_Heidenhain_NameNoDotH_ReturnsError()
        {
            var v = new NcProgramValidator(CncManufacturer.Heidenhain);
            var r = v.Validate("PROG", "L X+10");
            Assert.That(r.Success, Is.False);
            Assert.That(HasErrorContaining(r, ".H"), Is.True);
        }

        [Test]
        public void Validate_Heidenhain_PercentForbidden_ReturnsError()
        {
            var v = new NcProgramValidator(CncManufacturer.Heidenhain);
            var r = v.Validate("PROG.H", "L X+10 % COMMENT");
            Assert.That(r.Success, Is.False);
            Assert.That(HasErrorContaining(r, "%"), Is.True);
        }

        [Test]
        public void Validate_Heidenhain_NoFirstLineRequired()
        {
            var v = new NcProgramValidator(CncManufacturer.Heidenhain);
            var r = v.Validate("PROG.H", "L X+10\nM30");
            Assert.That(HasErrorContaining(r, "First line"), Is.False);
        }

        // ── Helpers ───────────────────────────────────────────────────────────

        private static bool HasErrorContaining(ValidationResult r, string fragment)
        {
            foreach (var e in r.Errors)
                if (e.Severity == ValidationSeverity.Error && e.Message.Contains(fragment))
                    return true;
            return false;
        }

        private static bool HasWarningContaining(ValidationResult r, string fragment)
        {
            foreach (var e in r.Errors)
                if (e.Severity == ValidationSeverity.Warning && e.Message.Contains(fragment))
                    return true;
            return false;
        }
    }
}
