using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using TakeoutSaaS.Application.App.Products.Commands;
using TakeoutSaaS.Application.App.Products.Dto;
using TakeoutSaaS.Application.App.Products.Queries;
using TakeoutSaaS.Domain.Products.Enums;
using TakeoutSaaS.Module.Authorization.Attributes;
using TakeoutSaaS.Shared.Abstractions.Constants;
using TakeoutSaaS.Shared.Abstractions.Results;
using TakeoutSaaS.Shared.Web.Api;

namespace TakeoutSaaS.AdminApi.Controllers;

/// <summary>
/// 商品管理。
/// </summary>
[ApiVersion("1.0")]
[Authorize]
[Route("api/admin/v{version:apiVersion}/products")]
public sealed class ProductsController(IMediator mediator) : BaseApiController
{
    /// <summary>
    /// 创建商品。
    /// </summary>
    /// <returns>创建的商品信息。</returns>
    [HttpPost]
    [PermissionAuthorize("product:create")]
    [ProducesResponseType(typeof(ApiResponse<ProductDto>), StatusCodes.Status200OK)]
    public async Task<ApiResponse<ProductDto>> Create([FromBody] CreateProductCommand command, CancellationToken cancellationToken)
    {
        // 1. 创建商品
        var result = await mediator.Send(command, cancellationToken);

        // 2. 返回创建结果
        return ApiResponse<ProductDto>.Ok(result);
    }

    /// <summary>
    /// 查询商品列表。
    /// </summary>
    /// <returns>商品分页列表。</returns>
    [HttpGet]
    [PermissionAuthorize("product:read")]
    [ProducesResponseType(typeof(ApiResponse<PagedResult<ProductDto>>), StatusCodes.Status200OK)]
    public async Task<ApiResponse<PagedResult<ProductDto>>> List(
        [FromQuery] long? storeId,
        [FromQuery] long? categoryId,
        [FromQuery] ProductStatus? status,
        [FromQuery] int page = 1,
        [FromQuery] int pageSize = 20,
        [FromQuery] string? sortBy = null,
        [FromQuery] bool sortDesc = true,
        CancellationToken cancellationToken = default)
    {
        // 1. 组装查询参数并执行查询
        var result = await mediator.Send(new SearchProductsQuery
        {
            StoreId = storeId,
            CategoryId = categoryId,
            Status = status,
            Page = page,
            PageSize = pageSize,
            SortBy = sortBy,
            SortDescending = sortDesc
        }, cancellationToken);

        // 2. 返回分页结果
        return ApiResponse<PagedResult<ProductDto>>.Ok(result);
    }

    /// <summary>
    /// 获取商品详情。
    /// </summary>
    /// <returns>商品详情。</returns>
    [HttpGet("{productId:long}")]
    [PermissionAuthorize("product:read")]
    [ProducesResponseType(typeof(ApiResponse<ProductDto>), StatusCodes.Status200OK)]
    [ProducesResponseType(typeof(ApiResponse<object>), StatusCodes.Status404NotFound)]
    public async Task<ApiResponse<ProductDto>> Detail(long productId, CancellationToken cancellationToken)
    {
        // 1. 查询商品详情
        var result = await mediator.Send(new GetProductByIdQuery { ProductId = productId }, cancellationToken);

        // 2. 返回详情或 404
        return result == null
            ? ApiResponse<ProductDto>.Error(ErrorCodes.NotFound, "商品不存在")
            : ApiResponse<ProductDto>.Ok(result);
    }

    /// <summary>
    /// 更新商品。
    /// </summary>
    /// <returns>更新后的商品信息。</returns>
    [HttpPut("{productId:long}")]
    [PermissionAuthorize("product:update")]
    [ProducesResponseType(typeof(ApiResponse<ProductDto>), StatusCodes.Status200OK)]
    [ProducesResponseType(typeof(ApiResponse<object>), StatusCodes.Status404NotFound)]
    public async Task<ApiResponse<ProductDto>> Update(long productId, [FromBody] UpdateProductCommand command, CancellationToken cancellationToken)
    {
        // 1. 确保命令包含商品标识
        if (command.ProductId == 0)
        {
            command = command with { ProductId = productId };
        }

        // 2. 执行更新
        var result = await mediator.Send(command, cancellationToken);

        // 3. 返回更新结果或 404
        return result == null
            ? ApiResponse<ProductDto>.Error(ErrorCodes.NotFound, "商品不存在")
            : ApiResponse<ProductDto>.Ok(result);
    }

    /// <summary>
    /// 删除商品。
    /// </summary>
    /// <returns>删除结果。</returns>
    [HttpDelete("{productId:long}")]
    [PermissionAuthorize("product:delete")]
    [ProducesResponseType(typeof(ApiResponse<object>), StatusCodes.Status200OK)]
    [ProducesResponseType(typeof(ApiResponse<object>), StatusCodes.Status404NotFound)]
    public async Task<ApiResponse<object>> Delete(long productId, CancellationToken cancellationToken)
    {
        // 1. 执行删除
        var success = await mediator.Send(new DeleteProductCommand { ProductId = productId }, cancellationToken);

        // 2. 返回结果或 404
        return success
            ? ApiResponse<object>.Ok(null)
            : ApiResponse<object>.Error(ErrorCodes.NotFound, "商品不存在");
    }

    /// <summary>
    /// 获取商品全量详情。
    /// </summary>
    [HttpGet("{productId:long}/detail")]
    [PermissionAuthorize("product:read")]
    [ProducesResponseType(typeof(ApiResponse<ProductDetailDto>), StatusCodes.Status200OK)]
    [ProducesResponseType(typeof(ApiResponse<object>), StatusCodes.Status404NotFound)]
    public async Task<ApiResponse<ProductDetailDto>> FullDetail(long productId, CancellationToken cancellationToken)
    {
        var result = await mediator.Send(new GetProductDetailQuery { ProductId = productId }, cancellationToken);
        return result == null
            ? ApiResponse<ProductDetailDto>.Error(ErrorCodes.NotFound, "商品不存在")
            : ApiResponse<ProductDetailDto>.Ok(result);
    }

    /// <summary>
    /// 上架商品。
    /// </summary>
    [HttpPost("{productId:long}/publish")]
    [PermissionAuthorize("product:publish")]
    [ProducesResponseType(typeof(ApiResponse<ProductDto>), StatusCodes.Status200OK)]
    [ProducesResponseType(typeof(ApiResponse<object>), StatusCodes.Status404NotFound)]
    public async Task<ApiResponse<ProductDto>> Publish(long productId, [FromBody] PublishProductCommand command, CancellationToken cancellationToken)
    {
        if (command.ProductId == 0)
        {
            command = command with { ProductId = productId };
        }

        var result = await mediator.Send(command, cancellationToken);
        return result == null
            ? ApiResponse<ProductDto>.Error(ErrorCodes.NotFound, "商品不存在")
            : ApiResponse<ProductDto>.Ok(result);
    }

    /// <summary>
    /// 下架商品。
    /// </summary>
    [HttpPost("{productId:long}/unpublish")]
    [PermissionAuthorize("product:publish")]
    [ProducesResponseType(typeof(ApiResponse<ProductDto>), StatusCodes.Status200OK)]
    [ProducesResponseType(typeof(ApiResponse<object>), StatusCodes.Status404NotFound)]
    public async Task<ApiResponse<ProductDto>> Unpublish(long productId, [FromBody] UnpublishProductCommand command, CancellationToken cancellationToken)
    {
        if (command.ProductId == 0)
        {
            command = command with { ProductId = productId };
        }

        var result = await mediator.Send(command, cancellationToken);
        return result == null
            ? ApiResponse<ProductDto>.Error(ErrorCodes.NotFound, "商品不存在")
            : ApiResponse<ProductDto>.Ok(result);
    }

    /// <summary>
    /// 替换商品 SKU。
    /// </summary>
    [HttpPut("{productId:long}/skus")]
    [PermissionAuthorize("product-sku:update")]
    [ProducesResponseType(typeof(ApiResponse<IReadOnlyList<ProductSkuDto>>), StatusCodes.Status200OK)]
    public async Task<ApiResponse<IReadOnlyList<ProductSkuDto>>> ReplaceSkus(long productId, [FromBody] ReplaceProductSkusCommand command, CancellationToken cancellationToken)
    {
        if (command.ProductId == 0)
        {
            command = command with { ProductId = productId };
        }

        var result = await mediator.Send(command, cancellationToken);
        return ApiResponse<IReadOnlyList<ProductSkuDto>>.Ok(result);
    }

    /// <summary>
    /// 替换商品规格。
    /// </summary>
    [HttpPut("{productId:long}/attributes")]
    [PermissionAuthorize("product-attr:update")]
    [ProducesResponseType(typeof(ApiResponse<IReadOnlyList<ProductAttributeGroupDto>>), StatusCodes.Status200OK)]
    public async Task<ApiResponse<IReadOnlyList<ProductAttributeGroupDto>>> ReplaceAttributes(long productId, [FromBody] ReplaceProductAttributesCommand command, CancellationToken cancellationToken)
    {
        if (command.ProductId == 0)
        {
            command = command with { ProductId = productId };
        }

        var result = await mediator.Send(command, cancellationToken);
        return ApiResponse<IReadOnlyList<ProductAttributeGroupDto>>.Ok(result);
    }

    /// <summary>
    /// 替换商品加料。
    /// </summary>
    [HttpPut("{productId:long}/addons")]
    [PermissionAuthorize("product-addon:update")]
    [ProducesResponseType(typeof(ApiResponse<IReadOnlyList<ProductAddonGroupDto>>), StatusCodes.Status200OK)]
    public async Task<ApiResponse<IReadOnlyList<ProductAddonGroupDto>>> ReplaceAddons(long productId, [FromBody] ReplaceProductAddonsCommand command, CancellationToken cancellationToken)
    {
        if (command.ProductId == 0)
        {
            command = command with { ProductId = productId };
        }

        var result = await mediator.Send(command, cancellationToken);
        return ApiResponse<IReadOnlyList<ProductAddonGroupDto>>.Ok(result);
    }

    /// <summary>
    /// 替换商品媒资。
    /// </summary>
    [HttpPut("{productId:long}/media")]
    [PermissionAuthorize("product-media:update")]
    [ProducesResponseType(typeof(ApiResponse<IReadOnlyList<ProductMediaAssetDto>>), StatusCodes.Status200OK)]
    public async Task<ApiResponse<IReadOnlyList<ProductMediaAssetDto>>> ReplaceMedia(long productId, [FromBody] ReplaceProductMediaCommand command, CancellationToken cancellationToken)
    {
        if (command.ProductId == 0)
        {
            command = command with { ProductId = productId };
        }

        var result = await mediator.Send(command, cancellationToken);
        return ApiResponse<IReadOnlyList<ProductMediaAssetDto>>.Ok(result);
    }

    /// <summary>
    /// 替换商品价格策略。
    /// </summary>
    [HttpPut("{productId:long}/pricing-rules")]
    [PermissionAuthorize("product-pricing:update")]
    [ProducesResponseType(typeof(ApiResponse<IReadOnlyList<ProductPricingRuleDto>>), StatusCodes.Status200OK)]
    public async Task<ApiResponse<IReadOnlyList<ProductPricingRuleDto>>> ReplacePricingRules(long productId, [FromBody] ReplaceProductPricingRulesCommand command, CancellationToken cancellationToken)
    {
        if (command.ProductId == 0)
        {
            command = command with { ProductId = productId };
        }

        var result = await mediator.Send(command, cancellationToken);
        return ApiResponse<IReadOnlyList<ProductPricingRuleDto>>.Ok(result);
    }
}
