using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Authorization;
using DramaLing.Api.Models.DTOs;
using DramaLing.Api.Services;
using System.Diagnostics;

namespace DramaLing.Api.Controllers;

[ApiController]
[Route("api/ai")]
public class AIController : ControllerBase
{
    private readonly IAnalysisService _analysisService;
    private readonly ILogger<AIController> _logger;

    public AIController(
        IAnalysisService analysisService,
        ILogger<AIController> logger)
    {
        _analysisService = analysisService;
        _logger = logger;
    }

    /// <summary>
    /// 智能分析英文句子
    /// </summary>
    /// <param name="request">分析請求</param>
    /// <returns>分析結果</returns>
    [HttpPost("analyze-sentence")]
    [AllowAnonymous]
    public async Task<ActionResult<SentenceAnalysisResponse>> AnalyzeSentence(
        [FromBody] SentenceAnalysisRequest request)
    {
        var requestId = Guid.NewGuid().ToString();
        var stopwatch = Stopwatch.StartNew();

        try
        {
            // For testing without auth - use dummy user ID
            var userId = "test-user-id";

            _logger.LogInformation("Processing sentence analysis request {RequestId} for user {UserId}",
                requestId, userId);

            // Input validation
            if (!ModelState.IsValid)
            {
                return BadRequest(CreateErrorResponse("INVALID_INPUT", "輸入格式錯誤",
                    ModelState.Values.SelectMany(v => v.Errors).Select(e => e.ErrorMessage).ToList(),
                    requestId));
            }

            // 使用帶快取的分析服務
            var options = request.Options ?? new AnalysisOptions();
            var analysisData = await _analysisService.AnalyzeSentenceAsync(
                request.InputText, options);

            stopwatch.Stop();
            analysisData.Metadata.ProcessingDate = DateTime.UtcNow;

            _logger.LogInformation("Sentence analysis completed for request {RequestId} in {ElapsedMs}ms",
                requestId, stopwatch.ElapsedMilliseconds);

            return Ok(new SentenceAnalysisResponse
            {
                Success = true,
                ProcessingTime = stopwatch.Elapsed.TotalSeconds,
                Data = analysisData
            });
        }
        catch (ArgumentException ex)
        {
            _logger.LogWarning(ex, "Invalid input for request {RequestId}", requestId);
            return BadRequest(CreateErrorResponse("INVALID_INPUT", ex.Message, null, requestId));
        }
        catch (InvalidOperationException ex)
        {
            _logger.LogError(ex, "AI service error for request {RequestId}", requestId);
            return StatusCode(500, CreateErrorResponse("AI_SERVICE_ERROR", "AI服務暫時不可用", null, requestId));
        }
        catch (Exception ex)
        {
            _logger.LogError(ex, "Unexpected error processing request {RequestId}", requestId);
            return StatusCode(500, CreateErrorResponse("INTERNAL_ERROR", "伺服器內部錯誤", null, requestId));
        }
    }

    /// <summary>
    /// 健康檢查端點
    /// </summary>
    [HttpGet("health")]
    [AllowAnonymous]
    public ActionResult GetHealth()
    {
        return Ok(new
        {
            Status = "Healthy",
            Service = "AI Analysis Service",
            Timestamp = DateTime.UtcNow,
            Version = "1.0"
        });
    }

    /// <summary>
    /// 取得分析統計資訊
    /// </summary>
    [HttpGet("stats")]
    [AllowAnonymous]
    public async Task<ActionResult> GetAnalysisStats()
    {
        try
        {
            var stats = await _analysisService.GetAnalysisStatsAsync();
            return Ok(new
            {
                Success = true,
                Data = new
                {
                    TotalAnalyses = stats.TotalAnalyses,
                    CachedAnalyses = stats.CachedAnalyses,
                    CacheHitRate = stats.CacheHitRate,
                    AverageResponseTimeMs = stats.AverageResponseTimeMs,
                    LastAnalysisAt = stats.LastAnalysisAt,
                    ProviderUsage = stats.ProviderUsageStats
                }
            });
        }
        catch (Exception ex)
        {
            _logger.LogError(ex, "Error getting analysis stats");
            return StatusCode(500, new { Success = false, Error = "無法取得統計資訊" });
        }
    }

    private ApiErrorResponse CreateErrorResponse(string code, string message, object? details, string requestId)
    {
        var suggestions = GetSuggestionsForError(code);

        return new ApiErrorResponse
        {
            Success = false,
            Error = new ApiError
            {
                Code = code,
                Message = message,
                Details = details,
                Suggestions = suggestions
            },
            RequestId = requestId,
            Timestamp = DateTime.UtcNow
        };
    }

    private List<string> GetSuggestionsForError(string errorCode)
    {
        return errorCode switch
        {
            "INVALID_INPUT" => new List<string> { "請檢查輸入格式", "確保文本長度在限制內" },
            "RATE_LIMIT_EXCEEDED" => new List<string> { "升級到Premium帳戶以獲得無限使用", "明天重新嘗試" },
            "AI_SERVICE_ERROR" => new List<string> { "請稍後重試", "如果問題持續，請聯繫客服" },
            _ => new List<string> { "請稍後重試" }
        };
    }
}