using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using RR3CommunityServer.Data;
using RR3CommunityServer.Models;

namespace RR3CommunityServer.Controllers;

[ApiController]
[Route("api/settings")]
public class ServerSettingsController : ControllerBase
{
    private readonly RR3DbContext _context;
    private readonly ILogger<ServerSettingsController> _logger;

    public ServerSettingsController(RR3DbContext context, ILogger<ServerSettingsController> logger)
    {
        _context = context;
        _logger = logger;
    }

    /// <summary>
    /// Get user settings (called by APK sync button)
    /// GET /api/settings/getUserSettings?deviceId=xxx
    /// </summary>
    [HttpGet("getUserSettings")]
    public async Task<ActionResult<UserSettingsResponse>> GetUserSettings([FromQuery] string? deviceId)
    {
        try
        {
            if (string.IsNullOrEmpty(deviceId))
            {
                _logger.LogWarning("GetUserSettings: No deviceId provided");
                return BadRequest(new { error = "deviceId is required" });
            }

            _logger.LogInformation($"🔄 GetUserSettings: deviceId={deviceId}");

            var settings = await _context.UserSettings
                .Where(s => s.DeviceId == deviceId)
                .FirstOrDefaultAsync();

            if (settings == null)
            {
                _logger.LogInformation($"⚠️ No settings found for deviceId={deviceId}, returning defaults");
                return Ok(new UserSettingsResponse
                {
                    mode = "offline",
                    serverUrl = "",
                    message = "No settings found, using defaults"
                });
            }

            _logger.LogInformation($"✅ Found settings: mode={settings.Mode}, url={settings.ServerUrl}");
            return Ok(new UserSettingsResponse
            {
                mode = settings.Mode,
                serverUrl = settings.ServerUrl,
                message = "Settings retrieved successfully"
            });
        }
        catch (Exception ex)
        {
            _logger.LogError(ex, "❌ Error in GetUserSettings");
            return StatusCode(500, new { error = "Internal server error" });
        }
    }

    /// <summary>
    /// Update user settings (called by web panel)
    /// POST /api/settings/updateUserSettings
    /// Body: { "deviceId": "xxx", "mode": "online", "serverUrl": "https://example.com:8443" }
    /// </summary>
    [HttpPost("updateUserSettings")]
    public async Task<ActionResult<UpdateSettingsResponse>> UpdateUserSettings([FromBody] UpdateUserSettingsRequest request)
    {
        try
        {
            if (string.IsNullOrEmpty(request.deviceId))
            {
                return BadRequest(new { error = "deviceId is required" });
            }

            if (string.IsNullOrEmpty(request.mode))
            {
                return BadRequest(new { error = "mode is required" });
            }

            _logger.LogInformation($"🔄 UpdateUserSettings: deviceId={request.deviceId}, mode={request.mode}, url={request.serverUrl}");

            var settings = await _context.UserSettings
                .Where(s => s.DeviceId == request.deviceId)
                .FirstOrDefaultAsync();

            if (settings == null)
            {
                // Create new settings
                settings = new UserSettings
                {
                    DeviceId = request.deviceId,
                    Mode = request.mode,
                    ServerUrl = request.serverUrl ?? "",
                    LastUpdated = DateTime.UtcNow
                };
                _context.UserSettings.Add(settings);
                _logger.LogInformation($"➕ Created new settings for deviceId={request.deviceId}");
            }
            else
            {
                // Update existing settings
                settings.Mode = request.mode;
                settings.ServerUrl = request.serverUrl ?? "";
                settings.LastUpdated = DateTime.UtcNow;
                _logger.LogInformation($"✏️ Updated existing settings for deviceId={request.deviceId}");
            }

            await _context.SaveChangesAsync();

            return Ok(new UpdateSettingsResponse
            {
                success = true,
                message = "Settings updated successfully"
            });
        }
        catch (Exception ex)
        {
            _logger.LogError(ex, "❌ Error in UpdateUserSettings");
            return StatusCode(500, new { error = "Internal server error" });
        }
    }

    /// <summary>
    /// Get all user settings (for admin panel)
    /// GET /api/settings/getAllUserSettings
    /// </summary>
    [HttpGet("getAllUserSettings")]
    public async Task<ActionResult<List<UserSettings>>> GetAllUserSettings()
    {
        try
        {
            var allSettings = await _context.UserSettings
                .OrderByDescending(s => s.LastUpdated)
                .ToListAsync();

            _logger.LogInformation($"📋 Retrieved {allSettings.Count} user settings");
            return Ok(allSettings);
        }
        catch (Exception ex)
        {
            _logger.LogError(ex, "❌ Error in GetAllUserSettings");
            return StatusCode(500, new { error = "Internal server error" });
        }
    }
}

// Response models
public class UserSettingsResponse
{
    public string mode { get; set; } = "offline";
    public string serverUrl { get; set; } = "";
    public string? message { get; set; }
}

public class UpdateUserSettingsRequest
{
    public string deviceId { get; set; } = string.Empty;
    public string mode { get; set; } = "offline";
    public string? serverUrl { get; set; }
}

public class UpdateSettingsResponse
{
    public bool success { get; set; }
    public string? message { get; set; }
}
