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/settings")]
public class AdminSettingsController : AdminApiControllerBase
{
    private readonly IAdminPlatformService _platform;

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

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

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

        return Ok(new ApiResponse<object>(true, new { key, request.Value }));
    }
}
