using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using SIGCM.Domain.Entities;
using SIGCM.Domain.Interfaces;
using SIGCM.Infrastructure.Repositories;

namespace SIGCM.API.Controllers;

[ApiController]
[Route("api/[controller]")]
[Authorize(Roles = "Admin")]
public class CouponsController : ControllerBase
{
    private readonly ICouponRepository _repository;
    private readonly AuditRepository _auditRepo;

    public CouponsController(ICouponRepository repository, AuditRepository auditRepo)
    {
        _repository = repository;
        _auditRepo = auditRepo;
    }

    [HttpGet]
    public async Task<IActionResult> GetAll()
    {
        var coupons = await _repository.GetAllAsync();
        return Ok(coupons);
    }

    [HttpPost]
    public async Task<IActionResult> Create(CreateCouponDto dto)
    {
        // Simple manual mapping
        var coupon = new Coupon
        {
            Code = dto.Code.ToUpper(),
            DiscountType = dto.DiscountType,
            DiscountValue = dto.DiscountValue,
            ExpiryDate = dto.ExpiryDate,
            MaxUsages = dto.MaxUsages,
            MaxUsagesPerUser = dto.MaxUsagesPerUser,
            IsActive = true
        };

        var id = await _repository.CreateAsync(coupon);

        // Audit Log
        var userIdClaim = User.FindFirst("Id")?.Value;
        if (int.TryParse(userIdClaim, out int userId))
        {
            await _auditRepo.AddLogAsync(new AuditLog
            {
                UserId = userId,
                Action = "CREATE_COUPON",
                EntityId = id,
                EntityType = "Coupon",
                Details = $"Cupón {coupon.Code} creado ({dto.DiscountType}: {dto.DiscountValue})",
                CreatedAt = DateTime.UtcNow
            });
        }

        return Ok(new { id });
    }

    [HttpDelete("{id}")]
    public async Task<IActionResult> Delete(int id)
    {
        await _repository.DeleteAsync(id);

        // Audit Log
        var userIdClaim = User.FindFirst("Id")?.Value;
        if (int.TryParse(userIdClaim, out int userId))
        {
            await _auditRepo.AddLogAsync(new AuditLog
            {
                UserId = userId,
                Action = "DELETE_COUPON",
                EntityId = id,
                EntityType = "Coupon",
                Details = $"Cupón ID #{id} eliminado",
                CreatedAt = DateTime.UtcNow
            });
        }

        return NoContent();
    }
}

public class CreateCouponDto
{
    public required string Code { get; set; }
    public required string DiscountType { get; set; } // "Percentage" or "Fixed"
    public decimal DiscountValue { get; set; }
    public DateTime? ExpiryDate { get; set; }
    public int? MaxUsages { get; set; }
    public int? MaxUsagesPerUser { get; set; }
}
