using Microsoft.AspNetCore.Mvc;
using Aberwyn.Data;
using Aberwyn.Models;
using System.Collections.Generic;

namespace Aberwyn.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class BudgetApiController : ControllerBase
    {
        private readonly BudgetService _budgetService;

        public BudgetApiController(BudgetService budgetService)
        {
            _budgetService = budgetService;
        }

        [HttpGet("items")]
        public IActionResult GetBudgetItems([FromQuery] int month, [FromQuery] int year)
        {
            try
            {
                var items = _budgetService.GetBudgetItems(month, year);
                return Ok(items);
            }
            catch (Exception ex)
            {
                // Log the exception (consider using a logging framework)
                return StatusCode(500, new { message = "An error occurred while fetching budget items.", details = ex.Message });
            }
        }

        [HttpGet("categories")]
        public IActionResult GetCategories()
        {
            try
            {
                var categories = _budgetService.GetCategories();
                return Ok(categories);
            }
            catch (Exception ex)
            {
                // Log the exception
                return StatusCode(500, new { message = "An error occurred while fetching categories.", details = ex.Message });
            }
        }

        [HttpPut("items")]
        public IActionResult UpdateBudgetItem([FromBody] BudgetItem item)
        {
            if (item == null || item.ID <= 0)
            {
                return BadRequest("Invalid budget item data.");
            }

            try
            {
                var result = _budgetService.UpdateBudgetItem(item);
                if (result)
                {
                    return Ok("Item updated successfully.");
                }
                return StatusCode(500, "Error updating item.");
            }
            catch (Exception ex)
            {
                // Log the exception
                return StatusCode(500, new { message = "An error occurred while updating the item.", details = ex.Message });
            }
        }

        [HttpPost("items")]
        public IActionResult AddBudgetItem([FromBody] BudgetItem item)
        {
            if (item == null || string.IsNullOrEmpty(item.Name) || item.Amount <= 0)
            {
                return BadRequest("Invalid budget item data.");
            }

            try
            {
                var result = _budgetService.AddBudgetItem(item);
                if (result)
                {
                    return CreatedAtAction(nameof(GetBudgetItems), new { id = item.ID }, item);
                }
                return StatusCode(500, "Error adding item.");
            }
            catch (Exception ex)
            {
                // Log the exception
                return StatusCode(500, new { message = "An error occurred while adding the item.", details = ex.Message });
            }
        }
    }
}
