using AutoMapper;
using ConstructorApp.BusinessLayer.Abstract;
using ConstructorApp.DtoLayer.ReferenceDto;
using ConstructorApp.EntityLayer.Entities;
using Microsoft.AspNetCore.Mvc;

namespace ConstructorAppApi.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class ReferenceController : ControllerBase
    {
        private readonly IReferenceService _referenceService;
        private readonly IMapper _mapper;

        public ReferenceController(IReferenceService referenceService, IMapper mapper)
        {
            _referenceService = referenceService;
            _mapper = mapper;
        }

        [HttpGet]
        public IActionResult ReferenceList()
        {
            var value = _referenceService.TGetListAll();
            var result = _mapper.Map<List<ResultReferenceDto>>(value);
            return Ok(result);
        }

        [HttpPost]
        public IActionResult CreateReference(CreateReferenceDto createReferenceDto)
        {
            var value = _mapper.Map<Reference>(createReferenceDto);
            _referenceService.TAdd(value);
            return Ok("Referans Bilgisi Eklendi");
        }

        [HttpDelete("{id}")]
        public IActionResult DeleteReference(int id)
        {
            var value = _referenceService.TGetByID(id);
            if (value != null)
            {
                _referenceService.TDelete(value);
                return Ok("Referans Bilgisi Silindi");
            }
            return NotFound("Referans Bilgisi Bulunamadı");
        }

        [HttpPut]
        public IActionResult UpdateReference(UpdateReferenceDto updateReferenceDto)
        {
            var value = _mapper.Map<Reference>(updateReferenceDto);
            _referenceService.TUpdate(value);
            return Ok("Referans Alanı Güncellendi");
        }

        [HttpGet("{id}")]
        public IActionResult GetReference(int id)
        {
            var value = _referenceService.TGetByID(id);
            if (value != null)
            {
                var result = _mapper.Map<ResultReferenceDto>(value);
                return Ok(result);
            }
            return NotFound("Referans Bilgisi Bulunamadı");
        }

        [HttpGet("CountAll")]
        public IActionResult CountAll()
        {
            return Ok(_referenceService.TCountAll());
        }
    }
}
