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

namespace Meezi.Admin.API.Controllers;

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

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

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

    [HttpPatch("{featureKey}")]
    public async Task<IActionResult> Update(string featureKey, [FromBody] UpdateFeatureRequest request, CancellationToken cancellationToken)
    {
        var ok = await _platform.UpdateFeatureAsync(featureKey, request, cancellationToken);
        if (!ok)
            return NotFound(new ApiResponse<object>(false, null, new ApiError("NOT_FOUND", "Feature not found.")));

        return Ok(new ApiResponse<object>(true, new { featureKey }));
    }
}
