using System;
using System.Linq;
using System.Web.Mvc;
using AutoMapper.QueryableExtensions;
using InventoryTraker.Web.Data;
using InventoryTraker.Web.Models;
using InventoryTraker.Web.Utilities;

namespace InventoryTraker.Web.Controllers
{
	public class ReportController : ControllerBase
	{
		private readonly AppDbContext _context;

		public ReportController(AppDbContext context)
		{
			_context = context;
		}

		public ActionResult Index()
		{
			return View();
		}

		public JsonResult NewCustomers()
		{
			var startOfMonth = DateTime.Today.ToStartOfMonth();
			var endOfMonth = DateTime.Today.ToEndOfMonth();

			var customers = _context.Customers.Where(x => x.CreateDate >= startOfMonth && x.CreateDate <= endOfMonth)
				.ProjectTo<NewCustomerReportViewModel>().ToArray();

			return BetterJson(customers);
		}

		public JsonResult LostCustomers()
		{
			var startOfMonth = DateTime.Today.ToStartOfMonth();
			var endOfMonth = DateTime.Today.ToEndOfMonth();

			var customers = _context.Customers.Where(x => x.TerminationDate >= startOfMonth && x.TerminationDate <= endOfMonth)
				.ProjectTo<LostCustomerReportViewModel>().ToArray();

			return Json(customers);
		}
	}
}