using Microsoft.AspNetCore.Mvc;
using Meezi.API.Services;
using Meezi.Core.Enums;
using Meezi.Core.Interfaces;
using Meezi.Infrastructure.Services.Platform;
using Meezi.Shared;

namespace Meezi.API.Controllers;

[Route("api/cafes/{cafeId}/media")]
public class MediaController : CafeApiControllerBase
{
    private readonly IMediaStorageService _media;

    public MediaController(IMediaStorageService media) => _media = media;

    [HttpPost("menu-image")]
    [RequestSizeLimit(5 * 1024 * 1024)]
    public Task<IActionResult> UploadMenuImage(
        string cafeId, IFormFile file, ITenantContext tenant, CancellationToken cancellationToken)
        => Upload(cafeId, file, tenant, _media.SaveMenuImageAsync, "INVALID_FILE", "Use JPEG/PNG/WebP up to 5MB.", cancellationToken);

    [HttpPost("menu-video")]
    [RequestSizeLimit(25 * 1024 * 1024)]
    public Task<IActionResult> UploadMenuVideo(
        string cafeId, IFormFile file, ITenantContext tenant, CancellationToken cancellationToken)
        => Upload(cafeId, file, tenant, _media.SaveMenuVideoAsync, "INVALID_FILE", "Use MP4/WebM/MOV up to 25MB.", cancellationToken);

    [HttpPost("menu-model3d")]
    [RequestSizeLimit(8 * 1024 * 1024)]
    public async Task<IActionResult> UploadMenuModel3d(
        string cafeId,
        IFormFile file,
        ITenantContext tenant,
        IPlatformCatalogService catalog,
        CancellationToken cancellationToken)
    {
        if (EnsureCafeAccess(cafeId, tenant) is { } denied) return denied;
        var planTier = tenant.PlanTier ?? PlanTier.Free;
        if (!await catalog.IsFeatureEnabledForCafeAsync(cafeId, planTier, "menu_3d", cancellationToken))
        {
            return StatusCode(
                StatusCodes.Status403Forbidden,
                new ApiResponse<object>(
                    false,
                    null,
                    new ApiError("PLAN_FEATURE_DISABLED", "3D menu is not included in your plan. Upgrade to enable it.")));
        }

        return await Upload(
            cafeId,
            file,
            tenant,
            _media.SaveMenuModel3dAsync,
            "INVALID_FILE",
            "Use GLB (.glb) up to 8MB.",
            cancellationToken);
    }

    [HttpPost("table-image")]
    [RequestSizeLimit(5 * 1024 * 1024)]
    public Task<IActionResult> UploadTableImage(
        string cafeId, IFormFile file, ITenantContext tenant, CancellationToken cancellationToken)
        => Upload(cafeId, file, tenant, _media.SaveTableImageAsync, "INVALID_FILE", "Use JPEG/PNG/WebP up to 5MB.", cancellationToken);

    [HttpPost("table-video")]
    [RequestSizeLimit(25 * 1024 * 1024)]
    public Task<IActionResult> UploadTableVideo(
        string cafeId, IFormFile file, ITenantContext tenant, CancellationToken cancellationToken)
        => Upload(cafeId, file, tenant, _media.SaveTableVideoAsync, "INVALID_FILE", "Use MP4/WebM/MOV up to 25MB.", cancellationToken);

    [HttpPost("cafe-logo")]
    [RequestSizeLimit(5 * 1024 * 1024)]
    public Task<IActionResult> UploadCafeLogo(
        string cafeId, IFormFile file, ITenantContext tenant, CancellationToken cancellationToken)
        => Upload(cafeId, file, tenant, _media.SaveCafeLogoAsync, "INVALID_FILE", "Use JPEG/PNG/WebP up to 5MB.", cancellationToken);

    [HttpPost("cafe-cover")]
    [RequestSizeLimit(5 * 1024 * 1024)]
    public Task<IActionResult> UploadCafeCover(
        string cafeId, IFormFile file, ITenantContext tenant, CancellationToken cancellationToken)
        => Upload(cafeId, file, tenant, _media.SaveCafeCoverAsync, "INVALID_FILE", "Use JPEG/PNG/WebP up to 5MB.", cancellationToken);

    private async Task<IActionResult> Upload(
        string cafeId,
        IFormFile file,
        ITenantContext tenant,
        Func<string, IFormFile, CancellationToken, Task<string?>> save,
        string errorCode,
        string errorMessage,
        CancellationToken cancellationToken)
    {
        if (EnsureCafeAccess(cafeId, tenant) is { } denied) return denied;
        if (file is null || file.Length == 0)
            return BadRequest(new ApiResponse<object>(false, null, new ApiError("INVALID_FILE", "No file uploaded.")));

        var url = await save(cafeId, file, cancellationToken);
        if (url is null)
            return BadRequest(new ApiResponse<object>(false, null, new ApiError(errorCode, errorMessage)));

        return Ok(new ApiResponse<UploadResultDto>(true, new UploadResultDto(url)));
    }
}

public record UploadResultDto(string Url);
