using FluentValidation;
using Microsoft.AspNetCore.Mvc;
using Meezi.API.Models.Crm;
using Meezi.API.Services;
using Meezi.Core.Interfaces;
using Meezi.Shared;

namespace Meezi.API.Controllers;

[Route("api/cafes/{cafeId}/coupons")]
public class CouponsController : CafeApiControllerBase
{
    private readonly ICouponService _couponService;
    private readonly IValidator<CreateCouponRequest> _createValidator;

    public CouponsController(
        ICouponService couponService,
        IValidator<CreateCouponRequest> createValidator)
    {
        _couponService = couponService;
        _createValidator = createValidator;
    }

    [HttpGet]
    public async Task<IActionResult> GetAll(string cafeId, ITenantContext tenant, CancellationToken cancellationToken)
    {
        if (EnsureCafeAccess(cafeId, tenant) is { } denied) return denied;
        var data = await _couponService.GetAllAsync(cafeId, cancellationToken);
        return Ok(new ApiResponse<IReadOnlyList<CouponDto>>(true, data));
    }

    [HttpGet("{id}")]
    public async Task<IActionResult> Get(string cafeId, string id, ITenantContext tenant, CancellationToken cancellationToken)
    {
        if (EnsureCafeAccess(cafeId, tenant) is { } denied) return denied;
        var data = await _couponService.GetAsync(cafeId, id, cancellationToken);
        if (data is null) return NotFoundError();
        return Ok(new ApiResponse<CouponDto>(true, data));
    }

    [HttpPost("validate")]
    public async Task<IActionResult> Validate(
        string cafeId,
        [FromBody] ValidateCouponRequest request,
        ITenantContext tenant,
        CancellationToken cancellationToken)
    {
        if (EnsureCafeAccess(cafeId, tenant) is { } denied) return denied;

        var (data, error) = await _couponService.ValidateAsync(cafeId, request, cancellationToken);
        if (error is not null)
            return BadRequest(new ApiResponse<object>(false, null, error));

        return Ok(new ApiResponse<ValidateCouponResult>(true, data));
    }

    [HttpPost]
    public async Task<IActionResult> Create(
        string cafeId,
        [FromBody] CreateCouponRequest request,
        ITenantContext tenant,
        CancellationToken cancellationToken)
    {
        if (EnsureCafeAccess(cafeId, tenant) is { } denied) return denied;
        var validation = await _createValidator.ValidateAsync(request, cancellationToken);
        if (!validation.IsValid) return BadRequest(ValidationError(validation));

        var data = await _couponService.CreateAsync(cafeId, request, cancellationToken);
        if (data is null)
            return BadRequest(new ApiResponse<object>(false, null,
                new ApiError("DUPLICATE_CODE", "Coupon code already exists.")));

        return Ok(new ApiResponse<CouponDto>(true, data));
    }

    [HttpPatch("{id}")]
    public async Task<IActionResult> Update(
        string cafeId,
        string id,
        [FromBody] UpdateCouponRequest request,
        ITenantContext tenant,
        CancellationToken cancellationToken)
    {
        if (EnsureCafeAccess(cafeId, tenant) is { } denied) return denied;
        var data = await _couponService.UpdateAsync(cafeId, id, request, cancellationToken);
        if (data is null) return NotFoundError();
        return Ok(new ApiResponse<CouponDto>(true, data));
    }

    [HttpDelete("{id}")]
    public async Task<IActionResult> Delete(
        string cafeId,
        string id,
        ITenantContext tenant,
        CancellationToken cancellationToken)
    {
        if (EnsureCafeAccess(cafeId, tenant) is { } denied) return denied;
        var deleted = await _couponService.DeleteAsync(cafeId, id, cancellationToken);
        if (!deleted) return NotFoundError();
        return Ok(new ApiResponse<object>(true, new { id }));
    }
}
