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 ImageController : Base
    {
        #region Public Constructors

        public ImageController(IUserImageService imageService,
                               IMapper mapper)
        {
            _imageService = imageService;
            _mapper = mapper;
        }

        #endregion Public Constructors

        #region Public Methods

        /// <summary>
        /// Deletes an image by its ID.
        /// </summary>
        /// <param name="id">The image ID to delete.</param>
        [HttpDelete("{id}")]
        [EnableCors]
        public void Delete(int id)
        {
            _imageService.DeleteImageById(id);
        }

        /// <summary>
        /// Retrieves a list of all images.
        /// </summary>
        /// <returns>A collection of ImageResp objects containing all images.</returns>
        [HttpGet]
        [EnableCors]
        public IEnumerable<ImageResp> Get()
        {
            var result = _imageService.GetAllImages();
            return _mapper.Map<IEnumerable<ImageResp>>(result);
        }

        /// <summary>
        /// Retrieves an image by its ID.
        /// </summary>
        /// <param name="id">The image ID to retrieve.</param>
        /// <returns>An ImageResp object containing the image details.</returns>
        [HttpGet("{id}")]
        [EnableCors]
        public ImageResp Get(int id)
        {
            var result = _imageService.GetImageById(id);
            return _mapper.Map<ImageResp>(result);
        }

        /// <summary>
        /// Adds a new image to the system.
        /// </summary>
        /// <param name="value">ImageReq object containing the new image data.</param>
        [HttpPost]
        [EnableCors]
        public void Post([FromBody] ImageReq value)
        {
            _imageService.AddNewImage(_mapper.Map<UserImage>(value));
        }

        /// <summary>
        /// Updates an existing image.
        /// </summary>
        /// <param name="id">The image ID to update.</param>
        /// <param name="value">ImageReq object containing the updated image data.</param>
        [HttpPut("{id}")]
        [EnableCors]
        public void Put(int id, [FromBody] ImageReq value)
        {
            _imageService.UpdateImage(id, _mapper.Map<UserImage>(value));
        }

        #endregion Public Methods

        #region Private Fields

        private readonly IUserImageService _imageService;
        private readonly IMapper _mapper;

        #endregion Private Fields
    }
}
