using System.Collections.Generic;
using System.Linq;
using EventsAppQuest.Application.Services;
using EventsAppQuest.Domain.Models;

namespace EventsAppQuest.Infrastructure.Services
{
    /// <summary>
    /// Implementação mock do serviço de presets de layout. Presets por template/categoria.
    /// </summary>
    public class MockLayoutPresetService : ILayoutPresetService
    {
        private readonly List<EventLayoutPreset> _presets = new List<EventLayoutPreset>();
        private readonly List<EventLayoutPresetItem> _presetItems = new List<EventLayoutPresetItem>();

        public MockLayoutPresetService()
        {
            // Casamento clássico (tpl-2)
            _presets.Add(new EventLayoutPreset
            {
                Id = "preset-wedding",
                Name = "Casamento clássico",
                EventCategoryId = "cat-wedding",
                EventTemplateId = "tpl-2",
                Description = "Cerimônia e recepção: arco, palco, mesa de doces e iluminação.",
                IsDefault = true,
                DisplayOrder = 1
            });
            _presetItems.Add(new EventLayoutPresetItem { PresetId = "preset-wedding", ShowroomItemId = "shr-5", PreferredSlotId = "stage", IsRequired = true, DisplayOrder = 1 });
            _presetItems.Add(new EventLayoutPresetItem { PresetId = "preset-wedding", ShowroomItemId = "shr-7", PreferredSlotId = "stage", IsRequired = true, DisplayOrder = 2 });
            _presetItems.Add(new EventLayoutPresetItem { PresetId = "preset-wedding", ShowroomItemId = "shr-6", PreferredSlotId = "dining", IsRequired = true, DisplayOrder = 3 });
            _presetItems.Add(new EventLayoutPresetItem { PresetId = "preset-wedding", ShowroomItemId = "shr-8", PreferredSlotId = "center", IsRequired = false, DisplayOrder = 4 });

            // Aniversário premium (tpl-1)
            _presets.Add(new EventLayoutPreset
            {
                Id = "preset-birthday",
                Name = "Aniversário premium",
                EventCategoryId = "cat-birthday",
                EventTemplateId = "tpl-1",
                Description = "Mesa do bolo, decoração e buffet em destaque.",
                IsDefault = true,
                DisplayOrder = 2
            });
            _presetItems.Add(new EventLayoutPresetItem { PresetId = "preset-birthday", ShowroomItemId = "shr-1", PreferredSlotId = "dining", IsRequired = true, DisplayOrder = 1 });
            _presetItems.Add(new EventLayoutPresetItem { PresetId = "preset-birthday", ShowroomItemId = "shr-2", PreferredSlotId = "center", IsRequired = true, DisplayOrder = 2 });
            _presetItems.Add(new EventLayoutPresetItem { PresetId = "preset-birthday", ShowroomItemId = "shr-3", PreferredSlotId = "dining", IsRequired = false, DisplayOrder = 3 });
            _presetItems.Add(new EventLayoutPresetItem { PresetId = "preset-birthday", ShowroomItemId = "shr-4", PreferredSlotId = "center", IsRequired = false, DisplayOrder = 4 });

            // Business conferência (tpl-3)
            _presets.Add(new EventLayoutPreset
            {
                Id = "preset-business",
                Name = "Business conferência",
                EventCategoryId = "cat-business",
                EventTemplateId = "tpl-3",
                Description = "Palco, mesas de reunião, coffee break e audiovisual.",
                IsDefault = true,
                DisplayOrder = 3
            });
            _presetItems.Add(new EventLayoutPresetItem { PresetId = "preset-business", ShowroomItemId = "shr-9", PreferredSlotId = "stage", IsRequired = true, DisplayOrder = 1 });
            _presetItems.Add(new EventLayoutPresetItem { PresetId = "preset-business", ShowroomItemId = "shr-10", PreferredSlotId = "center", IsRequired = true, DisplayOrder = 2 });
            _presetItems.Add(new EventLayoutPresetItem { PresetId = "preset-business", ShowroomItemId = "shr-11", PreferredSlotId = "lounge", IsRequired = false, DisplayOrder = 3 });
            _presetItems.Add(new EventLayoutPresetItem { PresetId = "preset-business", ShowroomItemId = "shr-12", PreferredSlotId = "wall", IsRequired = false, DisplayOrder = 4 });
        }

        public EventLayoutPreset GetById(string presetId) => _presets.Find(p => p.Id == presetId);

        public IReadOnlyList<EventLayoutPreset> GetByTemplateId(string templateId)
        {
            if (string.IsNullOrEmpty(templateId)) return new List<EventLayoutPreset>();
            return _presets.Where(p => p.EventTemplateId == templateId).OrderBy(p => p.DisplayOrder).ToList();
        }

        public IReadOnlyList<EventLayoutPreset> GetByCategoryId(string categoryId)
        {
            if (string.IsNullOrEmpty(categoryId)) return new List<EventLayoutPreset>();
            return _presets.Where(p => p.EventCategoryId == categoryId).OrderBy(p => p.DisplayOrder).ToList();
        }

        public IReadOnlyList<EventLayoutPreset> GetAvailableForProject(EventProject project)
        {
            if (project == null) return new List<EventLayoutPreset>();
            var list = new List<EventLayoutPreset>();
            if (!string.IsNullOrEmpty(project.EventTemplateId))
                list.AddRange(_presets.Where(p => p.EventTemplateId == project.EventTemplateId));
            if (!string.IsNullOrEmpty(project.EventCategoryId))
                list.AddRange(_presets.Where(p => p.EventCategoryId == project.EventCategoryId));
            return list.Distinct().OrderBy(p => p.DisplayOrder).ToList();
        }

        public IReadOnlyList<EventLayoutPresetItem> GetItemsByPresetId(string presetId)
        {
            if (string.IsNullOrEmpty(presetId)) return new List<EventLayoutPresetItem>();
            return _presetItems.Where(i => i.PresetId == presetId).OrderBy(i => i.DisplayOrder).ToList();
        }
    }
}
