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}/shifts")]
public sealed class StoreShiftsController(IMediator mediator) : BaseApiController
{
    /// <summary>
    /// 查询排班（默认未来 7 天）。
    /// </summary>
    [HttpGet]
    [PermissionAuthorize("store-shift:read")]
    [ProducesResponseType(typeof(ApiResponse<IReadOnlyList<StoreEmployeeShiftDto>>), StatusCodes.Status200OK)]
    public async Task<ApiResponse<IReadOnlyList<StoreEmployeeShiftDto>>> List(
        long storeId,
        [FromQuery] DateTime? from,
        [FromQuery] DateTime? to,
        [FromQuery] long? staffId,
        CancellationToken cancellationToken)
    {
        var result = await mediator.Send(new ListStoreEmployeeShiftsQuery
        {
            StoreId = storeId,
            From = from,
            To = to,
            StaffId = staffId
        }, cancellationToken);

        return ApiResponse<IReadOnlyList<StoreEmployeeShiftDto>>.Ok(result);
    }

    /// <summary>
    /// 创建排班。
    /// </summary>
    [HttpPost]
    [PermissionAuthorize("store-shift:create")]
    [ProducesResponseType(typeof(ApiResponse<StoreEmployeeShiftDto>), StatusCodes.Status200OK)]
    public async Task<ApiResponse<StoreEmployeeShiftDto>> Create(long storeId, [FromBody] CreateStoreEmployeeShiftCommand command, CancellationToken cancellationToken)
    {
        if (command.StoreId == 0)
        {
            command = command with { StoreId = storeId };
        }

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

    /// <summary>
    /// 更新排班。
    /// </summary>
    [HttpPut("{shiftId:long}")]
    [PermissionAuthorize("store-shift:update")]
    [ProducesResponseType(typeof(ApiResponse<StoreEmployeeShiftDto>), StatusCodes.Status200OK)]
    [ProducesResponseType(typeof(ApiResponse<object>), StatusCodes.Status404NotFound)]
    public async Task<ApiResponse<StoreEmployeeShiftDto>> Update(long storeId, long shiftId, [FromBody] UpdateStoreEmployeeShiftCommand command, CancellationToken cancellationToken)
    {
        if (command.StoreId == 0 || command.ShiftId == 0)
        {
            command = command with { StoreId = storeId, ShiftId = shiftId };
        }

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

    /// <summary>
    /// 删除排班。
    /// </summary>
    [HttpDelete("{shiftId:long}")]
    [PermissionAuthorize("store-shift:delete")]
    [ProducesResponseType(typeof(ApiResponse<object>), StatusCodes.Status200OK)]
    [ProducesResponseType(typeof(ApiResponse<object>), StatusCodes.Status404NotFound)]
    public async Task<ApiResponse<object>> Delete(long storeId, long shiftId, CancellationToken cancellationToken)
    {
        var success = await mediator.Send(new DeleteStoreEmployeeShiftCommand { StoreId = storeId, ShiftId = shiftId }, cancellationToken);
        return success
            ? ApiResponse<object>.Ok(null)
            : ApiResponse<object>.Error(ErrorCodes.NotFound, "排班不存在");
    }
}
