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

namespace RR3CommunityServer.Controllers;

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

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

    [HttpPost("logEvent")]
    public async Task<ActionResult<SynergyResponse<object>>> LogEvent([FromBody] TrackingEvent trackingEvent)
    {
        try
        {
            // Store event in database
            var analyticsEvent = new AnalyticsEvent
            {
                EventType = trackingEvent.eventType ?? "unknown",
                UserId = null, // TrackingEvent doesn't have userId
                SessionId = null, // TrackingEvent doesn't have sessionId
                EventData = System.Text.Json.JsonSerializer.Serialize(trackingEvent.properties ?? new Dictionary<string, object>()),
                Timestamp = DateTimeOffset.FromUnixTimeMilliseconds(trackingEvent.timestamp).UtcDateTime
            };

            _context.AnalyticsEvents.Add(analyticsEvent);
            await _context.SaveChangesAsync();

            _logger.LogInformation("Tracking Event Stored: {EventType}", 
                trackingEvent.eventType);

            var response = new SynergyResponse<object>
            {
                resultCode = 0,
                message = "Event logged",
                data = new { received = true, eventId = analyticsEvent.Id }
            };

            return Ok(response);
        }
        catch (Exception ex)
        {
            _logger.LogError(ex, "Error storing tracking event");
            
            // Still return success to not break game
            return Ok(new SynergyResponse<object>
            {
                resultCode = 0,
                message = "Event logged",
                data = new { received = true }
            });
        }
    }

    [HttpPost("logEvents")]
    public async Task<ActionResult<SynergyResponse<object>>> LogEvents([FromBody] List<TrackingEvent> events)
    {
        try
        {
            var analyticsEvents = events.Select(e => new AnalyticsEvent
            {
                EventType = e.eventType ?? "unknown",
                UserId = null,
                SessionId = null,
                EventData = System.Text.Json.JsonSerializer.Serialize(e.properties ?? new Dictionary<string, object>()),
                Timestamp = DateTimeOffset.FromUnixTimeMilliseconds(e.timestamp).UtcDateTime
            }).ToList();

            _context.AnalyticsEvents.AddRange(analyticsEvents);
            await _context.SaveChangesAsync();

            _logger.LogInformation("Tracking Batch Stored: {Count} events", events.Count);

            var response = new SynergyResponse<object>
            {
                resultCode = 0,
                message = $"{events.Count} events logged",
                data = new { received = events.Count }
            };

            return Ok(response);
        }
        catch (Exception ex)
        {
            _logger.LogError(ex, "Error storing tracking events batch");
            
            // Still return success to not break game
            return Ok(new SynergyResponse<object>
            {
                resultCode = 0,
                message = $"{events.Count} events logged",
                data = new { received = events.Count }
            });
        }
    }
}
