using DamageAssesment.Api.Locations.Interfaces;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace DamageAssesment.Api.Locations.Controllers
{
    [ApiController]
    public class LocationsController : ControllerBase
    {
        private ILocationsProvider LocationProvider;
        public LocationsController(ILocationsProvider LocationsProvider)
        {
            this.LocationProvider = LocationsProvider;
        }
        /// <summary>
        /// Get all locations.
        /// </summary>

        [HttpGet("locations")]
        public async Task<ActionResult> GetLocationsAsync()
        {

            var result = await LocationProvider.GetLocationsAsync();
            if (result.IsSuccess)
            {
                return Ok(result.Locations);
            }
            return NoContent();

        }
        /// <summary>
        /// Get all locations based on locationdId.
        /// </summary>

        [HttpGet("locations/{id}")]
        public async Task<ActionResult> GetLocationByIdAsync(int id)
        {

            var result = await LocationProvider.GetLocationByIdAsync(id);
            if (result.IsSuccess)
            {
                return Ok(result.Location);
            }
            return NotFound();

        }
        /// <summary>
        /// Update a Location.
        /// </summary>

        [HttpPut("locations/{id}")]
        public async Task<IActionResult> UpdateLocation(int id, Models.Location Location)
        {
            if (Location != null)
            {
                var result = await this.LocationProvider.UpdateLocationAsync(id, Location);
                if (result.IsSuccess)
                {
                    return Ok(result.Location);
                }
                return NotFound();
            }
            return NotFound();
        }
        /// <summary>
        /// Save a new location.
        /// </summary>

        [HttpPost("locations")]
        public async Task<IActionResult> CreateLocation(Models.Location Location)
        {
            if (Location != null)
            {
                var result = await this.LocationProvider.PostLocationAsync(Location);
                if (result.IsSuccess)
                {
                    return Ok(result.Location);
                }
                return BadRequest(result.ErrorMessage);
            }
            return BadRequest();
        }
        /// <summary>
        /// Delete an existing location.
        /// </summary>

        [HttpDelete("locations/{id}")]
        public async Task<IActionResult> DeleteLocation(int id)
        {
            var result = await this.LocationProvider.DeleteLocationAsync(id);
            if (result.IsSuccess)
            {
                return Ok(result.Location);
            }
            return NotFound();
        }
    }
}