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

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

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

		public ViewResult Index()
		{
			var models = _context.Opportunities.ProjectTo<OpportunityViewModel>().ToArray();
			return View(models);
		}

		public JsonResult Add(AddOpportunityForm form)
		{
			var customer = _context.Customers.Include(x => x.Opportunities).Single(x => x.Id == form.CustomerId);

			var opportunity = Mapper.Map<Opportunity>(form);

			customer.Opportunities.Add(opportunity);

			_context.SaveChanges();

			var model = Mapper.Map<CustomerOpportunityViewModel>(opportunity);

			return BetterJson(model);
		}
	}
}