using Managing.Application.Abstractions;
using Managing.Application.Abstractions.Services;
using Managing.Domain.MoneyManagements;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace Managing.Api.Controllers;

/// <summary>
/// Controller for managing money management strategies.
/// Provides endpoints for creating, retrieving, updating, and deleting money management strategies.
/// Requires authorization for access and produces JSON responses.
/// </summary>
[ApiController]
[Authorize]
[Route("[controller]")]
[Produces("application/json")]
public class MoneyManagementController : BaseController
{
    private readonly IMoneyManagementService _moneyManagementService;

    /// <summary>
    /// Initializes a new instance of the <see cref="MoneyManagementController"/> class.
    /// </summary>
    /// <param name="moneyManagementService">The service for managing money management strategies.</param>
    /// <param name="userService">The service for user-related operations.</param>
    public MoneyManagementController(
        IMoneyManagementService moneyManagementService,
        IUserService userService)
        : base(userService)
    {
        _moneyManagementService = moneyManagementService;
    }

    /// <summary>
    /// Creates a new money management strategy or updates an existing one for the authenticated user.
    /// </summary>
    /// <param name="moneyManagement">The money management strategy to create or update.</param>
    /// <returns>The created or updated money management strategy.</returns>
    [HttpPost]
    public async Task<ActionResult<MoneyManagement>> PostMoneyManagement(MoneyManagement moneyManagement)
    {
        try
        {
            var user = await GetUser();
            var result = await _moneyManagementService.CreateOrUpdateMoneyManagement(user, moneyManagement);
            return Ok(result);
        }
        catch (Exception ex)
        {
            return StatusCode(500, $"Error creating/updating money management: {ex.Message}");
        }
    }

    /// <summary>
    /// Retrieves all money management strategies for the authenticated user.
    /// </summary>
    /// <returns>A list of money management strategies.</returns>
    [HttpGet]
    [Route("moneymanagements")]
    public async Task<ActionResult<IEnumerable<MoneyManagement>>> GetMoneyManagements()
    {
        try
        {
            var user = await GetUser();
            var moneyManagements = await _moneyManagementService.GetMoneyMangements(user);
            return Ok(moneyManagements);
        }
        catch (Exception ex)
        {
            return StatusCode(500, $"Error retrieving money managements: {ex.Message}");
        }
    }

    /// <summary>
    /// Retrieves a specific money management strategy by name for the authenticated user.
    /// </summary>
    /// <param name="name">The name of the money management strategy to retrieve.</param>
    /// <returns>The requested money management strategy if found.</returns>
    [HttpGet]
    public async Task<ActionResult<MoneyManagement>> GetMoneyManagement(string name)
    {
        try
        {
            var user = await GetUser();
            var result = await _moneyManagementService.GetMoneyMangement(user, name);
            
            if (result == null)
            {
                return NotFound($"Money management strategy '{name}' not found");
            }
            
            return Ok(result);
        }
        catch (Exception ex)
        {
            return StatusCode(500, $"Error retrieving money management: {ex.Message}");
        }
    }

    /// <summary>
    /// Deletes a specific money management strategy by name for the authenticated user.
    /// </summary>
    /// <param name="name">The name of the money management strategy to delete.</param>
    /// <returns>An ActionResult indicating the outcome of the operation.</returns>
    [HttpDelete]
    public async Task<ActionResult> DeleteMoneyManagement(string name)
    {
        try
        {
            var user = await GetUser();
            var result = await _moneyManagementService.DeleteMoneyManagement(user, name);
            
            if (!result)
            {
                return NotFound($"Money management strategy '{name}' not found or could not be deleted");
            }
            
            return Ok(new { success = true, message = $"Money management strategy '{name}' deleted successfully" });
        }
        catch (Exception ex)
        {
            return StatusCode(500, $"Error deleting money management: {ex.Message}");
        }
    }
}