using System.Collections.Generic;
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using TakeoutSaaS.Application.App.Stores.Commands;
using TakeoutSaaS.Application.App.Stores.Dto;
using TakeoutSaaS.Application.App.Stores.Queries;
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}/stores/{storeId:long}/table-areas")]
public sealed class StoreTableAreasController(IMediator mediator) : BaseApiController
{
    /// <summary>
    /// 查询区域列表。
    /// </summary>
    [HttpGet]
    [PermissionAuthorize("store-table-area:read")]
    [ProducesResponseType(typeof(ApiResponse<IReadOnlyList<StoreTableAreaDto>>), StatusCodes.Status200OK)]
    public async Task<ApiResponse<IReadOnlyList<StoreTableAreaDto>>> List(long storeId, CancellationToken cancellationToken)
    {
        var result = await mediator.Send(new ListStoreTableAreasQuery { StoreId = storeId }, cancellationToken);
        return ApiResponse<IReadOnlyList<StoreTableAreaDto>>.Ok(result);
    }

    /// <summary>
    /// 创建区域。
    /// </summary>
    [HttpPost]
    [PermissionAuthorize("store-table-area:create")]
    [ProducesResponseType(typeof(ApiResponse<StoreTableAreaDto>), StatusCodes.Status200OK)]
    public async Task<ApiResponse<StoreTableAreaDto>> Create(long storeId, [FromBody] CreateStoreTableAreaCommand command, CancellationToken cancellationToken)
    {
        if (command.StoreId == 0)
        {
            command = command with { StoreId = storeId };
        }

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

    /// <summary>
    /// 更新区域。
    /// </summary>
    [HttpPut("{areaId:long}")]
    [PermissionAuthorize("store-table-area:update")]
    [ProducesResponseType(typeof(ApiResponse<StoreTableAreaDto>), StatusCodes.Status200OK)]
    [ProducesResponseType(typeof(ApiResponse<object>), StatusCodes.Status404NotFound)]
    public async Task<ApiResponse<StoreTableAreaDto>> Update(long storeId, long areaId, [FromBody] UpdateStoreTableAreaCommand command, CancellationToken cancellationToken)
    {
        if (command.StoreId == 0 || command.AreaId == 0)
        {
            command = command with { StoreId = storeId, AreaId = areaId };
        }

        var result = await mediator.Send(command, cancellationToken);
        return result == null
            ? ApiResponse<StoreTableAreaDto>.Error(ErrorCodes.NotFound, "桌台区域不存在")
            : ApiResponse<StoreTableAreaDto>.Ok(result);
    }

    /// <summary>
    /// 删除区域。
    /// </summary>
    [HttpDelete("{areaId:long}")]
    [PermissionAuthorize("store-table-area:delete")]
    [ProducesResponseType(typeof(ApiResponse<object>), StatusCodes.Status200OK)]
    [ProducesResponseType(typeof(ApiResponse<object>), StatusCodes.Status404NotFound)]
    public async Task<ApiResponse<object>> Delete(long storeId, long areaId, CancellationToken cancellationToken)
    {
        var success = await mediator.Send(new DeleteStoreTableAreaCommand { StoreId = storeId, AreaId = areaId }, cancellationToken);
        return success
            ? ApiResponse<object>.Ok(null)
            : ApiResponse<object>.Error(ErrorCodes.NotFound, "桌台区域不存在或不可删除");
    }
}
