using Dapper;
using Lada.Framework.Data.Domains.Cms;
using Lada.Framework.Data.Repositories;
using Lada.Framework.Infrastructures.Helpers;
using ladaexpress.vn.Infrastructures.DTO;
using ladaexpress.vn.Infrastructures.DTO.Models.Categories;
using ladaexpress.vn.Infrastructures.DTO.Models.Hr;
using ladaexpress.vn.Infrastructures.DTO.Models.Post.Index.Childs;
using ladaexpress.vn.Infrastructures.DTO.Models.Product.Index.Childs;
using ladaexpress.vn.Infrastructures.DTO.Models.Tags;
using Microsoft.EntityFrameworkCore;
using System.Data;

namespace ladaexpress.vn.Infrastructures.Services
{
    public interface ITagService
    {
        Task<TagDetailModel> PrepareTagDetail(int id);
    }
    public class TagService : ITagService
    {
        private readonly IRepository<Tag> _tagRepository;
        private readonly IDbConnection _dbConnection;

        public TagService(IDbConnection dbConnection, IRepository<Tag> tagRepository)
        {
            _dbConnection = dbConnection;
            _tagRepository = tagRepository;
        }

        public async Task<TagDetailModel> PrepareTagDetail(int id)
        {
            var model = new TagDetailModel();
            var currentTime = DateTimeHelper.CurrentTime();

            var tag = await _tagRepository.Query().FirstOrDefaultAsync(c => c.Id == id);
            if (tag != null && tag.Id > 0)
            {
                model = tag.ToDetailModel();
                model.Posts = await PreparePosts(id, "TagDetail_Posts");
                model.JobPosts = await PrepareJobPosts(id, "TagDetail_JobPosts");
                model.Products = await PrepareProducts(id, "TagDetail_Products");
            }
            return model;
        }

        public async Task<List<PostItem>> PreparePosts(int id, string storeName)
        {
            var posts = new List<PostItem>();
            try
            {
                posts = (await _dbConnection.QueryAsync<PostItem>(storeName, new { TagId = id }, commandType: CommandType.StoredProcedure)).ToList();
            }
            catch (Exception)
            {
            }
            return posts;
        }

        public async Task<List<JobPostItem>> PrepareJobPosts(int id, string storeName)
        {
            var posts = new List<JobPostItem>();
            try
            {
                posts = (await _dbConnection.QueryAsync<JobPostItem>(storeName, new { TagId = id }, commandType: CommandType.StoredProcedure)).ToList();
            }
            catch (Exception)
            {
            }
            return posts;
        }

        public async Task<List<ProductItem>> PrepareProducts(int id, string storeName)
        {
            var posts = new List<ProductItem>();
            try
            {
                posts = (await _dbConnection.QueryAsync<ProductItem>(storeName, new { TagId = id }, commandType: CommandType.StoredProcedure)).ToList();
            }
            catch (Exception)
            {
            }
            return posts;
        }
    }
}
