using Exam.Context;
using Exam.Models;

namespace Exam.Repositories
{
    public class Repository : IRepository
    {
        private readonly DataContext _context;
        public Repository(DataContext context)
        {
            _context = context;
        }

        public int AddReservavtion(Reservation reservation)
        {
            try
            {
                _context.Add(reservation);
                _context.SaveChanges();
                return reservation.Id;
            }
            catch (Exception ex) 
            {
                return -1;
            }

        }

        public bool RemoveReservavtions(List<int> ids)
        {
            try
            {
                var entities = _context.Reservations.Where(r => ids.Contains(r.Id)).ToList();
                _context.RemoveRange(entities);
                _context.SaveChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }

        public List<Flight> ShowFlightsByDateAndCity(DateTime date, string city)
        {
            return _context.Flights.Where(f => f.Date.Equals(date) && f.DestinationCity.Equals(city) && f.AvailableSeats > 0).ToList();
        }

        public List<Hotel> ShowHotelsByDateAndCity(DateTime date, string city)
        {
            return _context.Hotels.Where(h => h.Date.Equals(date) && h.City.Equals(city) && h.AvailableRooms > 0).ToList();
        }

        public void DecrementFlightSeats(int id)
        {
            _context.Flights.Where(f => f.FlightId == id).FirstOrDefault().AvailableSeats--;
            _context.SaveChanges();
        }

        public void DecrementHotelRooms(int id)
        {
            _context.Hotels.Where(f => f.HotelId == id).FirstOrDefault().AvailableRooms--;
            _context.SaveChanges();
        }
    }
}
