using System.Collections.Generic;
using AutoMapper;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc;
using skydiveLogs_api.DataContract;
using skydiveLogs_api.Domain;
using skydiveLogs_api.DomainBusiness.Interfaces;

namespace skydiveLogs_api.Controllers
{
    public class TunnelFlightController : Base
    {
        #region Public Constructors

        public TunnelFlightController(ITunnelFlightService tunnelFlightService,
                                      IMapper mapper)
        {
            _tunnelFlightService = tunnelFlightService;
            _mapper = mapper;
        }

        #endregion Public Constructors

        #region Public Methods

        /// <summary>
        /// Retrieves a list of all tunnel flights.
        /// </summary>
        /// <returns>A collection of TunnelFlightResp objects containing all tunnel flights.</returns>
        [HttpGet]
        [EnableCors]
        public IEnumerable<TunnelFlightResp> Get()
        {
            var result = _tunnelFlightService.GetAllTunnelFlights();
            return _mapper.Map<IEnumerable<TunnelFlightResp>>(result);
        }

        /// <summary>
        /// Retrieves a tunnel flight by its ID.
        /// </summary>
        /// <param name="id">The tunnel flight ID to retrieve.</param>
        /// <returns>A TunnelFlightResp object containing the tunnel flight details.</returns>
        [HttpGet("{id}")]
        [EnableCors]
        public TunnelFlightResp Get(int id)
        {
            var result = _tunnelFlightService.GetTunnelFlightById(id);
            return _mapper.Map<TunnelFlightResp>(result);
        }

        /// <summary>
        /// Retrieves tunnel flights filtered by date range.
        /// </summary>
        /// <param name="beginDate">The start date for filtering.</param>
        /// <param name="endDate">The end date for filtering.</param>
        /// <returns>A collection of TunnelFlightResp objects filtered by the specified date range.</returns>
        [HttpGet("{beginDate}/{endDate}")]
        [EnableCors]
        public IEnumerable<TunnelFlightResp> Get(string beginDate, string endDate)
        {
            var result = _tunnelFlightService.GetTunnelFlightByDates(beginDate, endDate);
            return _mapper.Map<IEnumerable<TunnelFlightResp>>(result);
        }

        /// <summary>
        /// Retrieves tunnel flights grouped by month within a date range.
        /// </summary>
        /// <param name="beginDate">The start date for grouping.</param>
        /// <param name="endDate">The end date for grouping.</param>
        /// <returns>A collection of StatisticForChartResp objects grouped by month.</returns>
        [HttpGet("month/{beginDate}/{endDate}")]
        [EnableCors]
        public IEnumerable<StatisticForChartResp> GetGroupByMonth(string beginDate, string endDate)
        {
            var result = _tunnelFlightService.GetTunnelFlightGroupByMonth(beginDate, endDate);
            return _mapper.Map<IEnumerable<StatisticForChartResp>>(result);
        }

        /// <summary>
        /// Adds a new tunnel flight to the system.
        /// </summary>
        /// <param name="value">TunnelFlightReq object containing the new tunnel flight data.</param>
        [HttpPost]
        [EnableCors]
        public void Post([FromBody] TunnelFlightReq value)
        {
            _tunnelFlightService.AddNewFlight(value.TunnelId,
                                              value.JumpTypeId,
                                              _mapper.Map<TunnelFlight>(value));
        }

        /// <summary>
        /// Updates an existing tunnel flight.
        /// </summary>
        /// <param name="id">The tunnel flight ID to update.</param>
        /// <param name="value">TunnelFlightReq object containing the updated tunnel flight data.</param>
        [HttpPut("{id}")]
        [EnableCors]
        public void Put(int id, [FromBody] TunnelFlightReq value)
        {
            _tunnelFlightService.UpdateTunnelFlight(id, _mapper.Map<TunnelFlight>(value));
        }

        /// <summary>
        /// Deletes a tunnel flight by its ID.
        /// </summary>
        /// <param name="id">The tunnel flight ID to delete.</param>
        [HttpDelete("{id}")]
        [EnableCors]
        public void Delete(int id)
        {
            _tunnelFlightService.DeleteTunnelFlightById(id);
        }

        #endregion Public Methods

        #region Private Fields

        private readonly ITunnelFlightService _tunnelFlightService;
        private readonly IMapper _mapper;

        #endregion Private Fields
    }
}
