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)
        {
            var items = _budgetService.GetBudgetItems(month, year);
            return Ok(items);
        }

        // New endpoint to get categories
        [HttpGet("categories")]
        public IActionResult GetCategories()
        {
            var categories = _budgetService.GetCategories();
            return Ok(categories);
        }

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

            // Assuming you have a method in your BudgetService to update an item
            var result = _budgetService.UpdateBudgetItem(item);

            if (result)
            {
                return Ok("Item updated successfully.");
            }

            return StatusCode(500, "Error updating item.");
        }

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

            // Assuming you have a method in your BudgetService to add an item
            var result = _budgetService.AddBudgetItem(item);

            if (result)
            {
                return CreatedAtAction(nameof(GetBudgetItems), new { id = item.ID }, item);
            }

            return StatusCode(500, "Error adding item.");
        }

    }

}
