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.Domain.Merchants.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}/stores/{storeId:long}/staffs")]
public sealed class StoreStaffsController(IMediator mediator) : BaseApiController
{
    /// <summary>
    /// 查询门店员工列表。
    /// </summary>
    [HttpGet]
    [PermissionAuthorize("store-staff:read")]
    [ProducesResponseType(typeof(ApiResponse<IReadOnlyList<StoreStaffDto>>), StatusCodes.Status200OK)]
    public async Task<ApiResponse<IReadOnlyList<StoreStaffDto>>> List(
        long storeId,
        [FromQuery] StaffRoleType? role,
        [FromQuery] StaffStatus? status,
        CancellationToken cancellationToken)
    {
        var result = await mediator.Send(new ListStoreStaffQuery
        {
            StoreId = storeId,
            RoleType = role,
            Status = status
        }, cancellationToken);

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

    /// <summary>
    /// 创建门店员工。
    /// </summary>
    [HttpPost]
    [PermissionAuthorize("store-staff:create")]
    [ProducesResponseType(typeof(ApiResponse<StoreStaffDto>), StatusCodes.Status200OK)]
    public async Task<ApiResponse<StoreStaffDto>> Create(long storeId, [FromBody] CreateStoreStaffCommand command, CancellationToken cancellationToken)
    {
        if (command.StoreId == 0)
        {
            command = command with { StoreId = storeId };
        }

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

    /// <summary>
    /// 更新门店员工。
    /// </summary>
    [HttpPut("{staffId:long}")]
    [PermissionAuthorize("store-staff:update")]
    [ProducesResponseType(typeof(ApiResponse<StoreStaffDto>), StatusCodes.Status200OK)]
    [ProducesResponseType(typeof(ApiResponse<object>), StatusCodes.Status404NotFound)]
    public async Task<ApiResponse<StoreStaffDto>> Update(long storeId, long staffId, [FromBody] UpdateStoreStaffCommand command, CancellationToken cancellationToken)
    {
        if (command.StoreId == 0 || command.StaffId == 0)
        {
            command = command with { StoreId = storeId, StaffId = staffId };
        }

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

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