using Asp.Versioning;
using Microsoft.AspNetCore.Mvc;
using AdsAnalyticsService.API.Application.DTOs;

namespace AdsAnalyticsService.API.Controllers;

/// <summary>
/// EN: API Controller for breakdown analytics.
/// VI: API Controller phân tích breakdown.
/// </summary>
[ApiController]
[ApiVersion("1.0")]
[Route("api/v{version:apiVersion}/ads-analytics/campaigns")]
[Produces("application/json")]
public class BreakdownController : ControllerBase
{
    private readonly ILogger<BreakdownController> _logger;

    public BreakdownController(ILogger<BreakdownController> logger)
    {
        _logger = logger ?? throw new ArgumentNullException(nameof(logger));
    }

    /// <summary>
    /// EN: Get campaign breakdown by dimension (age, gender, device, placement).
    /// VI: Lấy phân tích chiến dịch theo chiều (tuổi, giới tính, thiết bị, vị trí).
    /// </summary>
    [HttpGet("{id}/breakdown")]
    [ProducesResponseType(typeof(CampaignBreakdownDto), StatusCodes.Status200OK)]
    [ProducesResponseType(StatusCodes.Status400BadRequest)]
    public IActionResult GetBreakdown(
        Guid id,
        [FromQuery] string by,
        [FromQuery] DateTime? startDate = null,
        [FromQuery] DateTime? endDate = null)
    {
        if (string.IsNullOrWhiteSpace(by))
            return BadRequest(new { message = "Breakdown dimension 'by' is required" });

        var validDimensions = new[] { "age", "gender", "device", "placement" };
        if (!validDimensions.Contains(by.ToLower()))
            return BadRequest(new { message = $"Invalid dimension. Valid options: {string.Join(", ", validDimensions)}" });

        // EN: Return mock data (full implementation requires event tracking data)
        // VI: Trả về mock data (triển khai đầy đủ cần dữ liệu tracking)
        var mockBreakdown = GenerateMockBreakdown(id, by);

        _logger.LogInformation("Generated mock breakdown data for campaign {CampaignId} by {Dimension}", 
            id, by);

        return Ok(mockBreakdown);
    }

    private CampaignBreakdownDto GenerateMockBreakdown(Guid campaignId, string dimension)
    {
        var breakdown = dimension.ToLower() switch
        {
            "age" => new List<BreakdownDimension>
            {
                new("18-24", 15000, 450, 450.00m, 3.00m, 1.00m),
                new("25-34", 25000, 1000, 1000.00m, 4.00m, 1.00m),
                new("35-44", 20000, 700, 700.00m, 3.50m, 1.00m),
                new("45+", 10000, 300, 300.00m, 3.00m, 1.00m)
            },
            "gender" => new List<BreakdownDimension>
            {
                new("Male", 35000, 1400, 1400.00m, 4.00m, 1.00m),
                new("Female", 30000, 1000, 1000.00m, 3.33m, 1.00m),
                new("Other", 5000, 50, 50.00m, 1.00m, 1.00m)
            },
            "device" => new List<BreakdownDimension>
            {
                new("Mobile", 45000, 1800, 1800.00m, 4.00m, 1.00m),
                new("Desktop", 20000, 550, 550.00m, 2.75m, 1.00m),
                new("Tablet", 5000, 100, 100.00m, 2.00m, 1.00m)
            },
            "placement" => new List<BreakdownDimension>
            {
                new("Feed", 40000, 1600, 1600.00m, 4.00m, 1.00m),
                new("Story", 20000, 600, 600.00m, 3.00m, 1.00m),
                new("Search", 10000, 250, 250.00m, 2.50m, 1.00m)
            },
            _ => new List<BreakdownDimension>()
        };

        return new CampaignBreakdownDto(campaignId, dimension, breakdown);
    }
}
