using System;
using System.Web.Http;
using WindowsDataCenter.Helpers;
using Interfaces;

namespace WindowsDataCenter
{
    [RoutePrefix("api/logs")]
    public class LogsController:ApiController
    {
        private IRepository _repo;
        private ILogger _logger;

        public LogsController(IRepository repo, ILogger logger)
        {
            if (repo == null)
            {
                throw new ArgumentNullException(nameof(repo));
            }
            if (logger == null)
            {
                throw new ArgumentNullException(nameof(logger));
            }
            _repo = repo;
            _logger = logger;
        }
        
        [HttpPost]
        [Route("create")]
        [CacheControl(MaxAge = 0)]
        public IHttpActionResult CreateAndEditResultLog([FromBody] TimeLog log)
        {
            log.Source = LogSource.UI;
            var resp = log.Id > 0 ? _repo.UpdateLog(log) : _repo.CreateLog(log);
            return Ok(new {Id=log.Id, OperationResponse=resp});
        }
        
        [HttpDelete]
        [Route("delete")]
        [CacheControl(MaxAge = 0)]
        public IHttpActionResult DeleteLog([FromBody] TimeLog log)
        {
            _logger.Info("Removing Log {0} for user id {1}", log.Id, log.UserId);
            _repo.DeleteLog(log);
            return Ok();
        }
    }
}
