﻿using Belatrix.EnglishCourse.Api.Queries.StudentGroups;
using Belatrix.EnglishCourse.Common;
using Belatrix.EnglishCourse.Common.QueryHandler;
using Belatrix.EnglishCourse.Common.Utils;
using Belatrix.EnglishCourse.Domain.Contracts;
using Belatrix.EnglishCourse.Domain.Models;
using Belatrix.EnglishCourse.Repository.Context;
using Dapper;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Threading.Tasks;

namespace Belatrix.EnglishCourse.Api.QueryHandler.StudentGroups
{
    public class GetTeacherGroupsHandler : IQueryHandler<GetTeacherGroupsQuery>
    {
        private readonly IDbConnection _conn;
        private readonly IConfiguration _config;
        private readonly IOfficeRepository _officeRepo;
        protected RepositoryDbContext _context;

        public GetTeacherGroupsHandler(IConfiguration config, IOfficeRepository officeRepo, RepositoryDbContext context)
        {
            _config = config;
            _officeRepo = officeRepo;
            _context = context;
            _conn = _context.Database.GetDbConnection();
        }

        public async Task HandleAsync(GetTeacherGroupsQuery query)
        {
            string groupCapacity = Constants.DefaultGroupCapacityValue;

            var office = await _officeRepo.GetSingle(o => o.IdOffice == Convert.ToInt32(query.OfficeId)) ?? new Office();

            groupCapacity = office.GroupCapacity.ToString();

            query.Groups = await _conn.QueryAsync<GroupSummary>("dbo.GetTeacherGroups", new { OfficeId = query.OfficeId , GroupCapacity = groupCapacity }, null, null, CommandType.StoredProcedure);
        }
    }
}
