using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZooContracts.BindingModels;
using ZooContracts.SearchModel;
using ZooContracts.StoragesContracts;
using ZooContracts.ViewModels;
using ZooDatabaseImplements;
using ZooDatabaseImplements.Models;

namespace ZooDatabaseImplements.Implements
{
    public class RouteStorage : IRouteStorage
    {
        public List<RouteViewModel> GetFullList()
        {
            using var context = new ZooDatabase();
            return context.Routes
                .Include(x => x.Client)
                .Select(x => x.GetViewModel)
                .ToList();
        }
        public List<RouteViewModel> GetFilteredList(RouteSearchModel model)
        {
            using var context = new ZooDatabase();
            return context.Routes
                .Where(x => x.Id == model.Id)
                .Include(x => x.Client)
                .Select(x => x.GetViewModel)
                .ToList();
        }
        public RouteViewModel? GetElement(RouteSearchModel model)
        {
            if (model == null)
            {
                return null;
            }
            using var context = new ZooDatabase();
            if (model.Id.HasValue)
            {
                return context.Routes.Include(x => x.Client)
                    .FirstOrDefault(x => x.Id == model.Id)?.GetViewModel;
            }
            return null;
        }
        public RouteViewModel? Insert(RouteBindingModel model)
        {
            using var context = new ZooDatabase();
            var newRoute = Route.Create(context, model);
            if (newRoute == null)
            {
                return null;
            }
            context.Routes.Add(newRoute);
            context.SaveChanges();
            return newRoute.GetViewModel;
        }
        public RouteViewModel? Update(RouteBindingModel model)
        {
            using var context = new ZooDatabase();
            var Route = context.Routes.FirstOrDefault(x => x.Id == model.Id);
            if (Route == null)
            {
                return null;
            }
            Route.Update(model);
            context.SaveChanges();
            return Route.GetViewModel;
        }
        public RouteViewModel? Delete(RouteBindingModel model)
        {
            using var context = new ZooDatabase();
            var Route = context.Routes.FirstOrDefault(rec => rec.Id == model.Id);
            if (Route != null)
            {
                context.Routes.Remove(Route);
                context.SaveChanges();
                return Route.GetViewModel;
            }
            return null;
        }
        /// <summary>
        /// Получение списка заповедников по выбранному маршруту
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public List<ReportRouteReservesViewModel> GetReserveRoutes(RouteSearchModel model)
        {
            if (model.SelectedRoutesIds == null)
            {
                return new();
            }
            using var context = new ZooDatabase();
            return context.Routes
                .Where(x => model.SelectedRoutesIds.Contains(x.Id))
                .Select(x => new ReportRouteReservesViewModel()
                {
                    RouteId = x.Id,
                    Reserves = context.RouteReserves
                        .Include(x => x.Reserve)
                        .ThenInclude(x => x.Employee)
                        .Where(x => x.RouteId == x.Id)
                        .Select(x => new ReportReserveRoutesViewModel()
                        {

                            ReserveId = x.Route.Id,
                            EmployeeName = x.Reserve.Employee.EmployeeName,
                            Sum = x.Reserve.ReservePrice

                        })
                        .ToList()
                })
                .ToList();
        }
    }
}
