using NetAdmin.Application.Repositories;
using NetAdmin.Application.Services;
using NetAdmin.Domain.DbMaps.Sys;
using NetAdmin.Domain.Dto.Dependency;
using NetAdmin.Domain.Dto.Sys.Role;
using NetAdmin.SysComponent.Application.Services.Sys.Dependency;

namespace NetAdmin.SysComponent.Application.Services.Sys;

/// <inheritdoc cref="IRoleService" />
public sealed class RoleService(DefaultRepository<Sys_Role> rpo) //
    : RepositoryService<Sys_Role, IRoleService>(rpo), IRoleService
{
    /// <inheritdoc />
    public async Task<int> BulkDeleteAsync(BulkReq<DelReq> req)
    {
        var sum = 0;
        foreach (var item in req.Items) {
            sum += await DeleteAsync(item).ConfigureAwait(false);
        }

        return sum;
    }

    /// <inheritdoc />
    public async Task<QueryRoleRsp> CreateAsync(CreateRoleReq req)
    {
        var entity = req.Adapt<Sys_Role>();
        var ret    = await Rpo.InsertAsync(entity).ConfigureAwait(false);

        await Rpo.SaveManyAsync(entity, nameof(entity.Depts)).ConfigureAwait(false);
        await Rpo.SaveManyAsync(entity, nameof(entity.Menus)).ConfigureAwait(false);
        await Rpo.SaveManyAsync(entity, nameof(entity.Apis)).ConfigureAwait(false);

        entity = entity with { Id = ret.Id };
        return entity.Adapt<QueryRoleRsp>();
    }

    /// <inheritdoc />
    /// <exception cref="NetAdminInvalidOperationException">Users_exist_under_this_role_and_deletion_is_not_allowed</exception>
    public async Task<int> DeleteAsync(DelReq req)
    {
        return await Rpo.Orm.Select<Sys_UserRole>().ForUpdate().AnyAsync(a => a.RoleId == req.Id).ConfigureAwait(false)
            ? throw new NetAdminInvalidOperationException(Ln.该角色下存在用户)
            : await Rpo.DeleteAsync(a => a.Id == req.Id).ConfigureAwait(false);
    }

    /// <inheritdoc />
    public Task<bool> ExistAsync(QueryReq<QueryRoleReq> req)
    {
        return QueryInternal(req).AnyAsync();
    }

    /// <inheritdoc />
    public async Task<QueryRoleRsp> GetAsync(QueryRoleReq req)
    {
        var ret = await QueryInternal(new QueryReq<QueryRoleReq> { Filter = req }).ToOneAsync().ConfigureAwait(false);
        return ret.Adapt<QueryRoleRsp>();
    }

    /// <inheritdoc />
    public async Task<PagedQueryRsp<QueryRoleRsp>> PagedQueryAsync(PagedQueryReq<QueryRoleReq> req)
    {
        var list = await QueryInternal(req)
                         .Page(req.Page, req.PageSize)
                         .Count(out var total)
                         .ToListAsync()
                         .ConfigureAwait(false);

        return new PagedQueryRsp<QueryRoleRsp>(req.Page, req.PageSize, total, list.Adapt<IEnumerable<QueryRoleRsp>>());
    }

    /// <inheritdoc />
    public async Task<IEnumerable<QueryRoleRsp>> QueryAsync(QueryReq<QueryRoleReq> req)
    {
        var ret = await QueryInternal(req).ToListAsync().ConfigureAwait(false);
        return ret.Adapt<IEnumerable<QueryRoleRsp>>();
    }

    /// <inheritdoc />
    public async Task<QueryRoleRsp> UpdateAsync(UpdateRoleReq req)
    {
        var entity = req.Adapt<Sys_Role>();
        _ = await Rpo.UpdateAsync(entity).ConfigureAwait(false);
        await Rpo.SaveManyAsync(entity, nameof(entity.Depts)).ConfigureAwait(false);
        await Rpo.SaveManyAsync(entity, nameof(entity.Menus)).ConfigureAwait(false);
        await Rpo.SaveManyAsync(entity, nameof(entity.Apis)).ConfigureAwait(false);

        return (await QueryAsync(new QueryReq<QueryRoleReq> { Filter = new QueryRoleReq { Id = req.Id } })
            .ConfigureAwait(false)).First();
    }

    /// <inheritdoc />
    protected override Task<Sys_Role> UpdateForSqliteAsync(Sys_Role req)
    {
        throw new NotImplementedException();
    }

    private ISelect<Sys_Role> QueryInternal(QueryReq<QueryRoleReq> req)
    {
        var ret = Rpo.Select.IncludeMany(a => a.Depts.Select(b => new Sys_Dept { Id = b.Id }))
                     .IncludeMany(a => a.Menus.Select(b => new Sys_Menu { Id        = b.Id }))
                     .IncludeMany(a => a.Apis.Select(b => new Sys_Api { Id          = b.Id }))
                     .WhereDynamicFilter(req.DynamicFilter)
                     .WhereDynamic(req.Filter)
                     .WhereIf( //
                         req.Keywords?.Length > 0
                       , a => a.Id == req.Keywords.Int64Try(0) || a.Name.Contains(req.Keywords) ||
                              a.Summary.Contains(req.Keywords))
                     .OrderByPropertyNameIf(req.Prop?.Length > 0, req.Prop, req.Order == Orders.Ascending);

        if (!req.Prop?.Equals(nameof(req.Filter.Sort), StringComparison.OrdinalIgnoreCase) ?? true) {
            ret = ret.OrderByDescending(a => a.Sort);
        }

        if (!req.Prop?.Equals(nameof(req.Filter.CreatedTime), StringComparison.OrdinalIgnoreCase) ?? true) {
            ret = ret.OrderByDescending(a => a.CreatedTime);
        }

        return ret;
    }
}