using Microsoft.AspNetCore.Mvc;
using Sentry;
using System;

namespace Managing.Api.Controllers
{
    [ApiController]
    [Route("api/[controller]")]
    public class SentryTestController : ControllerBase
    {
        private readonly ILogger<SentryTestController> _logger;

        public SentryTestController(ILogger<SentryTestController> logger)
        {
            _logger = logger;
        }

        [HttpGet("test-exception")]
        public IActionResult TestException()
        {
            try
            {
                throw new Exception($"Test exception from SentryTestController - {DateTime.Now}");
            }
            catch (Exception ex)
            {
                // Add breadcrumbs for context
                SentrySdk.AddBreadcrumb("About to capture test exception", "test");
                
                // Add context to the error
                SentrySdk.ConfigureScope(scope =>
                {
                    scope.SetTag("test_type", "manual_exception");
                    scope.SetExtra("timestamp", DateTime.Now);
                });
                
                // Log to both Serilog and Sentry
                _logger.LogError(ex, "Test exception captured in SentryTestController");
                
                // Explicitly capture exception
                SentrySdk.CaptureException(ex);
                
                return Ok(new 
                { 
                    message = "Exception manually captured and sent to Sentry",
                    exceptionMessage = ex.Message,
                    timestamp = DateTime.Now
                });
            }
        }

        [HttpGet("throw-exception")]
        public IActionResult ThrowException()
        {
            _logger.LogInformation("About to throw an uncaught exception");
            
            // This should be automatically captured by Sentry middleware
            throw new InvalidOperationException($"Uncaught exception from ThrowException endpoint - {DateTime.Now}");
        }

        [HttpGet("test-message")]
        public IActionResult TestMessage()
        {
            // Send a simple message to Sentry
            SentrySdk.CaptureMessage("Test message from Managing API", SentryLevel.Info);
            
            return Ok(new 
            { 
                message = "Test message sent to Sentry",
                timestamp = DateTime.Now
            });
        }
    }
} 