using Infrastructure;
using OpenAuth.App.BaseApp.Base;
using OpenAuth.App.Interface;
using OpenAuth.App.Request;
using OpenAuth.Repository;
using OpenAuth.Repository.Domain;
using SqlSugar;

namespace OpenAuth.App.ServiceApp.AI;

public class AiAchievementServiceApp : SqlSugarBaseApp<LasaAiAchievement, SugarDbContext>
{
    public AiAchievementServiceApp(ISugarUnitOfWork<SugarDbContext> unitWork,
        ISimpleClient<LasaAiAchievement> repository, IAuth auth) : base(unitWork, repository, auth)
    {
    }

    public async Task<Response<PageInfo<List<LasaAiAchievement>>>> GetAiAchievementList(PageReq req)
    {
        RefAsync<int> total = 0;
        var page = await Repository.AsQueryable()
            .WhereIF(!string.IsNullOrEmpty(req.key), x => x.Title.Contains(req.key))
            .WhereIF(req.startTime != null, x => x.CreateTime >= req.startTime && x.CreateTime <= req.endTime)
            .ToPageListAsync(req.page, req.limit, total);
        return new Response<PageInfo<List<LasaAiAchievement>>>()
        {
            Result = new PageInfo<List<LasaAiAchievement>>()
            {
                Items = page,
                Total = total.Value
            }
        };
    }

    public async Task<Response<LasaAiAchievement>> GetAiAchievement(string id)
    {
        var result = await Repository.GetByIdAsync(id);
        if (result != null)
        {
            var images = await Repository
                .ChangeRepository<SugarRepositiry<LasaAiAchievementDetail>>()
                .AsQueryable()
                .Where(x => x.AiAchievementId == id)
                .ToListAsync();
            result.AiAchievementDetailList = images;
        }

        return new Response<LasaAiAchievement>()
        {
            Result = result
        };
    }
}