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

namespace Managing.Api.Controllers;

/// <summary>
/// Controller for managing application settings and configurations.
/// Provides endpoints for setting up default configurations and resetting settings.
/// Requires authorization for access and produces JSON responses.
/// </summary>
[ApiController]
[Authorize]
[Route("[controller]")]
[Produces("application/json")]
public class SettingsController : BaseController
{
    private readonly ISettingsService _settingsService;

    /// <summary>
    /// Initializes a new instance of the <see cref="SettingsController"/> class.
    /// </summary>
    /// <param name="settingsService">The service for managing application settings.</param>
    /// <param name="userService">The service for user-related operations.</param>
    public SettingsController(ISettingsService settingsService, IUserService userService)
        : base(userService)
    {
        _settingsService = settingsService;
    }

    /// <summary>
    /// Sets up initial application settings.
    /// </summary>
    /// <returns>A result indicating if the setup was successful.</returns>
    [HttpPost]
    public async Task<ActionResult<bool>> SetupSettings()
    {
        try
        {
            var result = await _settingsService.SetupSettings();
            return Ok(result);
        }
        catch (Exception ex)
        {
            return StatusCode(500, $"Error setting up settings: {ex.Message}");
        }
    }

    /// <summary>
    /// Resets all application settings to their default values.
    /// </summary>
    /// <returns>A result indicating if the reset was successful.</returns>
    [HttpDelete]
    public async Task<ActionResult<bool>> ResetSettings()
    {
        try
        {
            var result = await _settingsService.ResetSettings();
            return Ok(result);
        }
        catch (Exception ex)
        {
            return StatusCode(500, $"Error resetting settings: {ex.Message}");
        }
    }

    /// <summary>
    /// Creates default configuration for backtesting including Money Management, Strategy, and Scenario
    /// for the authenticated user.
    /// </summary>
    /// <returns>A result indicating if the default configuration was created successfully.</returns>
    [HttpPost]
    [Route("create-default-config")]
    public async Task<ActionResult<bool>> CreateDefaultConfiguration()
    {
        try
        {
            var user = await GetUser();
            if (user == null)
            {
                return Unauthorized("User not found or authentication failed");
            }

            var result = await _settingsService.CreateDefaultConfiguration(user);
            return Ok(result);
        }
        catch (Exception ex)
        {
            return StatusCode(500, $"Error creating default configuration: {ex.Message}");
        }
    }
}
