using API.Models.Internal.Altersgruppen;
using API.Repository.AgeGroupRepo;
using Microsoft.AspNetCore.Mvc;

namespace API.Controllers
{
    [ApiController]
    [Route("api/ageGroups/")]
    public class AgeGroupController : ControllerBase
    {
        private IAgeGroupService _ageGroupService;

        public AgeGroupController(IAgeGroupService ageGroupService)
        {
            _ageGroupService = ageGroupService;
        }

        [HttpGet()]
        public async Task<IActionResult> GetAll()
        {
            var allAgeGroups = await _ageGroupService.GetAllAsync();

            return Ok(allAgeGroups);
        }

        [HttpGet("{id}")]
        public async Task<IActionResult> GetOne([FromRoute] string id)
        {
            var group = await _ageGroupService.GetAsync(id);

            if (group == null)
            {
                return NotFound();
            }

            return Ok(group);
        }

        [HttpPost()]
        public async Task<IActionResult> Create([FromBody] AltersGruppeIngoing groupDto)
        {
            var group = await _ageGroupService.CreateAsync(groupDto.ToInternalFromIngoing());

            return CreatedAtAction(nameof(GetOne), new { Id = group.Id }, group);
        }

        [HttpPut("{id}")]
        public async Task<IActionResult> Update([FromRoute] string id, [FromBody] AltersGruppeIngoing groupDto)
        {
            var group = await _ageGroupService.UpdateAsync(id, groupDto.ToInternalFromIngoing());

            if(group == null)
            {
                return NotFound();
            }

            return Ok(group);
        }

        [HttpDelete("{id}")]
        public async Task<IActionResult> Delete([FromRoute] string Id)
        {
            var group = await _ageGroupService.DeleteAsync(Id);

            if (group == null)
            {
                return NotFound();
            }

            return NoContent();
        }
    }
}
