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}/pickup")]
public sealed class StorePickupController(IMediator mediator) : BaseApiController
{
    /// <summary>
    /// 获取自提配置。
    /// </summary>
    [HttpGet("settings")]
    [PermissionAuthorize("pickup-setting:read")]
    [ProducesResponseType(typeof(ApiResponse<StorePickupSettingDto>), StatusCodes.Status200OK)]
    public async Task<ApiResponse<StorePickupSettingDto>> GetSetting(long storeId, CancellationToken cancellationToken)
    {
        var result = await mediator.Send(new GetStorePickupSettingQuery { StoreId = storeId }, cancellationToken);
        return result is null
            ? ApiResponse<StorePickupSettingDto>.Error(ErrorCodes.NotFound, "未配置自提设置")
            : ApiResponse<StorePickupSettingDto>.Ok(result);
    }

    /// <summary>
    /// 更新自提配置。
    /// </summary>
    [HttpPut("settings")]
    [PermissionAuthorize("pickup-setting:update")]
    [ProducesResponseType(typeof(ApiResponse<StorePickupSettingDto>), StatusCodes.Status200OK)]
    public async Task<ApiResponse<StorePickupSettingDto>> UpsertSetting(long storeId, [FromBody] UpsertStorePickupSettingCommand command, CancellationToken cancellationToken)
    {
        if (command.StoreId == 0)
        {
            command = command with { StoreId = storeId };
        }

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

    /// <summary>
    /// 查询档期列表。
    /// </summary>
    [HttpGet("slots")]
    [PermissionAuthorize("pickup-slot:read")]
    [ProducesResponseType(typeof(ApiResponse<IReadOnlyList<StorePickupSlotDto>>), StatusCodes.Status200OK)]
    public async Task<ApiResponse<IReadOnlyList<StorePickupSlotDto>>> ListSlots(long storeId, CancellationToken cancellationToken)
    {
        var result = await mediator.Send(new ListStorePickupSlotsQuery { StoreId = storeId }, cancellationToken);
        return ApiResponse<IReadOnlyList<StorePickupSlotDto>>.Ok(result);
    }

    /// <summary>
    /// 创建档期。
    /// </summary>
    [HttpPost("slots")]
    [PermissionAuthorize("pickup-slot:create")]
    [ProducesResponseType(typeof(ApiResponse<StorePickupSlotDto>), StatusCodes.Status200OK)]
    public async Task<ApiResponse<StorePickupSlotDto>> CreateSlot(long storeId, [FromBody] CreateStorePickupSlotCommand command, CancellationToken cancellationToken)
    {
        if (command.StoreId == 0)
        {
            command = command with { StoreId = storeId };
        }

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

    /// <summary>
    /// 更新档期。
    /// </summary>
    [HttpPut("slots/{slotId:long}")]
    [PermissionAuthorize("pickup-slot:update")]
    [ProducesResponseType(typeof(ApiResponse<StorePickupSlotDto>), StatusCodes.Status200OK)]
    [ProducesResponseType(typeof(ApiResponse<object>), StatusCodes.Status404NotFound)]
    public async Task<ApiResponse<StorePickupSlotDto>> UpdateSlot(long storeId, long slotId, [FromBody] UpdateStorePickupSlotCommand command, CancellationToken cancellationToken)
    {
        if (command.StoreId == 0 || command.SlotId == 0)
        {
            command = command with { StoreId = storeId, SlotId = slotId };
        }

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

    /// <summary>
    /// 删除档期。
    /// </summary>
    [HttpDelete("slots/{slotId:long}")]
    [PermissionAuthorize("pickup-slot:delete")]
    [ProducesResponseType(typeof(ApiResponse<object>), StatusCodes.Status200OK)]
    public async Task<ApiResponse<object>> DeleteSlot(long storeId, long slotId, CancellationToken cancellationToken)
    {
        var success = await mediator.Send(new DeleteStorePickupSlotCommand { StoreId = storeId, SlotId = slotId }, cancellationToken);
        return success ? ApiResponse<object>.Ok(null) : ApiResponse<object>.Error(ErrorCodes.NotFound, "档期不存在");
    }
}
