using Microsoft.AspNetCore.Mvc;
using Strata.Code.Business.Services.Interfaces;

namespace Strata.Code.Web.Controllers
{
    [ApiController]
    [Route("api/[controller]")]
    public class BudgetConfigDefaultSettingController : ControllerBase
    {
        private readonly IBudgetConfigDefaultSettingService _service;

        public BudgetConfigDefaultSettingController(IBudgetConfigDefaultSettingService service)
        {
            _service = service;
        }

        [HttpGet]
        [ProducesResponseType(typeof(IEnumerable<BudgetConfigDefaultSettingDto>), StatusCodes.Status200OK)]
        public async Task<IActionResult> GetAll()
        {
            var settings = await _service.GetAllAsync();
            return Ok(settings);
        }

        [HttpGet("{id}")]
        [ProducesResponseType(typeof(BudgetConfigDefaultSettingDto), StatusCodes.Status200OK)]
        [ProducesResponseType(StatusCodes.Status404NotFound)]
        public async Task<IActionResult> GetById(int id)
        {
            var setting = await _service.GetByIdAsync(id);
            if (setting == null)
                return NotFound();

            return Ok(setting);
        }

        [HttpPost]
        [ProducesResponseType(typeof(BudgetConfigDefaultSettingDto), StatusCodes.Status201Created)]
        [ProducesResponseType(StatusCodes.Status400BadRequest)]
        public async Task<IActionResult> Create([FromBody] BudgetConfigDefaultSettingDto setting)
        {
            if (!ModelState.IsValid)
                return BadRequest(ModelState);

            var createdSetting = await _service.CreateAsync(setting);
            return CreatedAtAction(nameof(GetById), new { id = createdSetting.SettingId }, createdSetting);
        }

        [HttpPut("{id}")]
        [ProducesResponseType(typeof(BudgetConfigDefaultSettingDto), StatusCodes.Status200OK)]
        [ProducesResponseType(StatusCodes.Status404NotFound)]
        [ProducesResponseType(StatusCodes.Status400BadRequest)]
        public async Task<IActionResult> Update(int id, [FromBody] BudgetConfigDefaultSettingDto setting)
        {
            if (id != setting.SettingId)
                return BadRequest();

            var updatedSetting = await _service.UpdateAsync(setting);
            if (updatedSetting == null)
                return NotFound();

            return Ok(updatedSetting);
        }

        [HttpDelete("{id}")]
        [ProducesResponseType(StatusCodes.Status204NoContent)]
        [ProducesResponseType(StatusCodes.Status404NotFound)]
        public async Task<IActionResult> Delete(int id)
        {
            var result = await _service.DeleteAsync(id);
            if (!result)
                return NotFound();

            return NoContent();
        }
    }
}
