using Microsoft.AspNetCore.Mvc;
using Meezi.Admin.API.Models;
using Meezi.Admin.API.Services;
using Meezi.Core.Enums;
using Meezi.Shared;

namespace Meezi.Admin.API.Controllers;

[Route("api/admin/plans")]
public class AdminPlansController : AdminApiControllerBase
{
    private readonly IAdminPlatformService _platform;

    public AdminPlansController(IAdminPlatformService platform)
    {
        _platform = platform;
    }

    [HttpGet]
    public async Task<IActionResult> List(CancellationToken cancellationToken)
    {
        var plans = await _platform.GetPlansAsync(cancellationToken);
        return Ok(new ApiResponse<object>(true, plans));
    }

    [HttpPut("{tier}")]
    public async Task<IActionResult> Update(PlanTier tier, [FromBody] UpdatePlanRequest request, CancellationToken cancellationToken)
    {
        var ok = await _platform.UpdatePlanAsync(tier, request, cancellationToken);
        if (!ok)
            return NotFound(new ApiResponse<object>(false, null, new ApiError("NOT_FOUND", "Plan not found.")));

        var plans = await _platform.GetPlansAsync(cancellationToken);
        var updated = plans.FirstOrDefault(p => p.Tier == tier);
        return Ok(new ApiResponse<object>(true, updated));
    }
}
