using PARR.DAL.Context;
using PARR.Domain.Entities.Job;
using PARR.Domain.Entities.Unit;
using PARR.Domain.Enums;

namespace PARR.DAL.IntegrationTests
{
    public static class TestDataGenerator
    {
        // --- Unit IDs ---
        public static readonly Guid UnitAId = Guid.Parse("11111111-1111-1111-1111-111111111111");
        public static readonly Guid UnitBId = Guid.Parse("00000000-0000-1111-1111-111111111111");
        public static readonly Guid UnitCId = Guid.Parse("22222222-2222-2222-2222-222222222222");
        public static readonly Guid UnitDId = Guid.Parse("33333333-3333-3333-3333-333333333333");
        public static readonly Guid UnitWId = Guid.Parse("44444444-4444-4444-4444-444444444444");
        public static readonly Guid UnitXId = Guid.Parse("55555555-5555-5555-5555-555555555555");
        public static readonly Guid UnitYId = Guid.Parse("66666666-6666-6666-6666-666666666666");
        public static readonly Guid UnitZId = Guid.Parse("77777777-7777-7777-7777-777777777777");

        // --- Field IDs (для обхода составного ключа UnitFilterId+FieldId) ---
        public static readonly Guid FieldId1 = Guid.Parse("08b42860-09a7-4a1f-aa4f-0290e86df883");//ТИП_ЭК
        public static readonly Guid FieldId2 = Guid.Parse("5b2d3f5b-a9f3-4a6b-9142-70ef4357ac37"); //неуникальный
        public static readonly Guid FieldId3 = Guid.Parse("5f3c7b8b-583d-411b-b9bd-1e045ddc5a8e");//ПОДКАТЕГОРИЯ_ЭК
        public static readonly Guid FieldId4 = Guid.Parse("3e9ac5aa-531b-4ffc-8f93-694d00aa92a0");//активный

        // --- Field Value IDs ---
        public static readonly Guid FieldValueId1 = Guid.Parse("fe18edcd-ab6a-4f3e-b7bc-e54aad9ecc0d"); // система хранения данных (схд) field3
        public static readonly Guid FieldValueId2 = Guid.Parse("f385b22c-42c3-4db9-bd41-b829603cc2d9"); // 1 field2+field4
        public static readonly Guid FieldValueId3 = Guid.Parse("2badc139-69cb-4d52-830a-1b3c6a9faf9d"); // 0 field2+field4
        public static readonly Guid FieldValueId4 = Guid.Parse("fde68ef7-a07e-4c10-872c-cd98c3d1c30b"); // коммутатор
        public static readonly Guid FieldValueId5 = Guid.Parse("9f169862-3f78-49f2-b967-13b5b2f56231"); // серверное оборудование
        public static readonly Guid FieldValueId6 = Guid.Parse("075b6fad-d24f-40f4-aa0e-83a4cfc46bbf"); // стойка цвк
        public static readonly Guid FieldValueId7 = Guid.Parse("9679afd8-8004-4ce2-bd6f-9005b037ef96"); // схд

        internal static void Seed(DataContext context)
        {
            if (context.Units.Any()) return;

            var now = DateTimeOffset.UtcNow;

            // 1. Значения полей (остаются те же)
            var fieldValues = new[]
            {
                new UnitFieldValue { Id = FieldValueId1, Value = "система хранения данных (схд)", DateCreated = now },
                new UnitFieldValue { Id = FieldValueId2, Value = "1", DateCreated = now },
                new UnitFieldValue { Id = FieldValueId3, Value = "0", DateCreated = now },
                new UnitFieldValue { Id = FieldValueId4, Value = "коммутатор", DateCreated = now },
            };
            context.UnitFieldValues.AddRange(fieldValues);

            // 2. Unit'ы
            var units = new[]
            {
                new Unit { Id = UnitAId, Name = "СХД-КМТ-DVGD-MDS9148-1-ДВС", DateCreated = now },
                new Unit { Id = UnitBId, Name = "СХД-КМТ-DVGD-MDS9148-2-ДВС", DateCreated = now },
                new Unit { Id = UnitCId, Name = "СРВ-СТОЙКА-СТОЙКА-ХИВЦ-4.0-ДВС", DateCreated = now },
                new Unit { Id = UnitDId, Name = "СХД-VSP-E590-01-ДВС", DateCreated = now },
                new Unit { Id = UnitWId, Name = "СХД-КМТ-SAN1_FAB_1-ДВС", DateCreated = now },
                new Unit { Id = UnitXId, Name = "СХД-КМТ-CISCO-MDS9148-1-ДВС", DateCreated = now },
                new Unit { Id = UnitYId, Name = "СХД-КМТ-CISCO-MDS9148-3-ДВС", DateCreated = now },
            };
            context.Units.AddRange(units);

            // 3. UnitInUnit (связи)
            var unitInUnits = new[]
            {
                new UnitInUnit { ParentUnitId = UnitAId, ChildUnitId = UnitCId, DateCreated = now },
                new UnitInUnit { ParentUnitId = UnitBId, ChildUnitId = UnitCId, DateCreated = now },
                new UnitInUnit { ParentUnitId = UnitDId, ChildUnitId = UnitAId, DateCreated = now },
                new UnitInUnit { ParentUnitId = UnitDId, ChildUnitId = UnitBId, DateCreated = now },
                new UnitInUnit { ParentUnitId = UnitWId, ChildUnitId = UnitXId, DateCreated = now },
                new UnitInUnit { ParentUnitId = UnitWId, ChildUnitId = UnitYId, DateCreated = now }
            };
            context.UnitInUnits.AddRange(unitInUnits);

            // 4. UnitInValue 
            var unitInValues = new List<UnitInValue>
            {
                // Основные (FieldId)
                new UnitInValue { UnitId = UnitAId, FieldId = FieldId1,  ValueId = FieldValueId4, DateCreated = now },
                new UnitInValue { UnitId = UnitAId, FieldId = FieldId3,  ValueId = FieldValueId1, DateCreated = now },
                new UnitInValue { UnitId = UnitAId, FieldId = FieldId4,  ValueId = FieldValueId2, DateCreated = now },
                new UnitInValue { UnitId = UnitAId, FieldId = FieldId2,  ValueId = FieldValueId3, DateCreated = now },
                new UnitInValue { UnitId = UnitBId, FieldId = FieldId2,  ValueId = FieldValueId3, DateCreated = now },
                new UnitInValue { UnitId = UnitBId, FieldId = FieldId1,  ValueId = FieldValueId4, DateCreated = now },
                new UnitInValue { UnitId = UnitBId, FieldId = FieldId3,  ValueId = FieldValueId1, DateCreated = now },
                new UnitInValue { UnitId = UnitBId, FieldId = FieldId4,  ValueId = FieldValueId2, DateCreated = now },
                new UnitInValue { UnitId = UnitCId, FieldId = FieldId3,  ValueId = FieldValueId5, DateCreated = now },
                new UnitInValue { UnitId = UnitCId, FieldId = FieldId1,  ValueId = FieldValueId6, DateCreated = now },
                new UnitInValue { UnitId = UnitCId, FieldId = FieldId4,  ValueId = FieldValueId2, DateCreated = now },
                new UnitInValue { UnitId = UnitDId, FieldId = FieldId3,  ValueId = FieldValueId1, DateCreated = now },
                new UnitInValue { UnitId = UnitDId, FieldId = FieldId1,  ValueId = FieldValueId7, DateCreated = now },
                new UnitInValue { UnitId = UnitDId, FieldId = FieldId4,  ValueId = FieldValueId2, DateCreated = now },
                new UnitInValue { UnitId = UnitWId, FieldId = FieldId3,  ValueId = FieldValueId1, DateCreated = now },
                new UnitInValue { UnitId = UnitWId, FieldId = FieldId1,  ValueId = FieldValueId4, DateCreated = now },
                new UnitInValue { UnitId = UnitWId, FieldId = FieldId4,  ValueId = FieldValueId2, DateCreated = now },
                new UnitInValue { UnitId = UnitWId, FieldId = FieldId2,  ValueId = FieldValueId2, DateCreated = now },
                new UnitInValue { UnitId = UnitXId, FieldId = FieldId3,  ValueId = FieldValueId1, DateCreated = now },
                new UnitInValue { UnitId = UnitXId, FieldId = FieldId1,  ValueId = FieldValueId4, DateCreated = now },
                new UnitInValue { UnitId = UnitXId, FieldId = FieldId4,  ValueId = FieldValueId2, DateCreated = now },
                new UnitInValue { UnitId = UnitYId, FieldId = FieldId3,  ValueId = FieldValueId1, DateCreated = now },
                new UnitInValue { UnitId = UnitYId, FieldId = FieldId1,  ValueId = FieldValueId4, DateCreated = now },
                new UnitInValue { UnitId = UnitYId, FieldId = FieldId4,  ValueId = FieldValueId2, DateCreated = now },

            };
            context.UnitInValues.AddRange(unitInValues);

            // 5. JobGroupType и Group
            var groupType = context.JobGroupTypes
                .FirstOrDefault(t => t.Code == JobGroupTypesEnum.Simple)
                ?? throw new InvalidOperationException("JobGroupType 'Simple' не найден в контексте.");

            var group = new JobGroup
            {
                Id = Guid.NewGuid(),
                GroupTypeId = groupType.Id,
                GroupType = groupType,
                GroupName = "Группа",
                ShortDescription = "%ТНК-КРАТКО% %ЭК%",
                FullDescription = "Подробно:\n%СВЯЗИ%",
                Solution = "Решение",
                TemplateDuration = "540 00:00:00",
                ReferenceDate = now,
                IsAutoDistributionEnabled = false,
                IsAgent = false,
                DateCreated = now
            };
            context.JobGroups.Add(group);

            // 6. Jobs
            var jobs = new List<Job>();

            // --- TestJob ---
            var job1 = new Job
            {
                Id = Guid.NewGuid(),
                Name = "TestJob_1",
                WorkName = "Работа",
                TemplateNameMask = "%ЭК%",
                WorkGroupMask = "%ГРУППА_РАБОТ%",
                GroupId = group.Id,
                TnkId = Guid.NewGuid(),
                Group = group,
                DateCreated = now,
                ResponseAreaMask = "%ЗО_РГ%"
            };
            var unitFilter1Id = Guid.NewGuid();
            job1.UnitFilters = new List<JobUnitFilter>
            {
                new JobUnitFilter
                {
                    Id = unitFilter1Id,
                    UnitFilter = "СХД-%",
                    JobId = job1.Id,
                    Job = job1,
                    DateCreated = now,
                    FieldFilters = new List<JobFieldFilter>
                    {
                        new JobFieldFilter
                        {
                            UnitFilterId = unitFilter1Id,
                            FieldId = FieldId1,
                            ValueMask="коммутатор"
                        },
                        new JobFieldFilter
                        {
                            UnitFilterId = unitFilter1Id,
                            FieldId = FieldId4,
                            ValueMask="1"
                        },
                        new JobFieldFilter
                        {
                            UnitFilterId = unitFilter1Id,
                            FieldId = FieldId2,
                            ValueMask="0"
                        },
                    },
                    RelationshipFilters = new List<JobRelationshipFilter>
                    {
                        new JobRelationshipFilter
                        {
                            IsParent = true,
                            IsInverse = true,
                            IsFullMatch = true,
                            FieldId = FieldId1,
                            ValueMask = "коммутатор",
                            UnitFilterId = unitFilter1Id
                        }
                    }
                }
            };
            jobs.Add(job1);

            // --- TestJob_2 (с IsParent = false для дочерних связей) ---
            var job2 = new Job
            {
                Id = Guid.NewGuid(),
                Name = "TestJob_2",
                WorkName = "Работа2",
                TemplateNameMask = "%ЭК%",
                WorkGroupMask = "%ГРУППА_РАБОТ%",
                GroupId = group.Id,
                TnkId = Guid.NewGuid(),
                Group = group,
                DateCreated = now,
                ResponseAreaMask= "%ЗО_РГ%"
            };
            var unitFilter2Id = Guid.NewGuid();
            job2.UnitFilters = new List<JobUnitFilter>
            {
                new JobUnitFilter
                {
                    Id = unitFilter2Id,
                    UnitFilter = "СХД-%",
                    JobId = job2.Id,
                    Job = job2,
                    DateCreated = now,
                    FieldFilters = new List<JobFieldFilter>
                    {
                        new JobFieldFilter
                        {
                            UnitFilterId = unitFilter2Id,
                            FieldId = FieldId1,
                            ValueMask="коммутатор"
                        },
                        new JobFieldFilter
                        {
                            UnitFilterId = unitFilter2Id,
                            FieldId = FieldId4,
                            ValueMask="1"
                        },
                    },
                    RelationshipFilters = new List<JobRelationshipFilter>
                    {
                        new JobRelationshipFilter
                        {
                            IsParent = false,         // <-- Новый фильтр: ищем дочерние юниты
                            IsInverse = false,        // <-- Простой фильтр
                            IsFullMatch = false,      // <-- Простой фильтр
                            FieldId = FieldId1,
                            ValueMask = "коммутатор",
                            UnitFilterId = unitFilter2Id
                        }
                    }
                }
            };
            jobs.Add(job2);

            context.Jobs.AddRange(jobs);
            context.SaveChanges();
        }
    }
}
