using Microsoft.AspNetCore.Mvc.Infrastructure;
using Microsoft.EntityFrameworkCore;
using System.Data.Common;
using FamilyTreeAPI.Entities;
using FamilyTreeAPI.Interface;

namespace FamilyTreeAPI.Models
{
    public partial class FamilyTreeDBContext
    {
        private enum EnumStaff
        {
            Id =0,
            Firstname,
            Lastname,
            Email,
            Stype,
            Active,
            Role,
            Password
        };
        private enum EnumStaffWork
        {
            id =0,
            staffid,
            description,
            startdate,
            sactive,
            did,
            starttime,
            stoptime,
            task
        }
        private enum EnumServiceTask
        {
            id = 0,
            staffid,
            sdate,
            description,
            code,
            status,
            assignTo,
            clientid,                        
            serviceno
        }

        public async Task<List<StaffDto>> LoadStaffAsync(StaffCriteria criteria)
        {
            List<StaffDto> list = new();
            using (DbConnection connect = this.Database.GetDbConnection())
            {
                DbCommand command = connect.CreateCommand();
                command.CommandType = System.Data.CommandType.Text;
                command.CommandText = "SELECT * From usp_search_user(:iemail,:ifirstname,:ilastname)";
                
                var loginParam = command.CreateParameter();

                loginParam.ParameterName = "iemail";
                loginParam.Value = string.IsNullOrEmpty(criteria.Email) ? "" : criteria.Email;
                
                var fistnameParam = command.CreateParameter();
                
                fistnameParam.ParameterName = "ifirstname";
                fistnameParam.Value = string.IsNullOrEmpty(criteria.FirstName) ? "" : criteria.FirstName;
                
                var surnameParam = command.CreateParameter() ;
                
                surnameParam.ParameterName = "ilastname";
                surnameParam.Value = string.IsNullOrEmpty(criteria.LastName) ? "" : criteria.LastName;
                
                command.Parameters.Add(loginParam);
                command.Parameters.Add(fistnameParam);
                command.Parameters.Add(surnameParam);
                await connect.OpenAsync();
                DbDataReader reader = await command.ExecuteReaderAsync();
                StaffDto staff;
                int idx = 0;
                while (reader.Read())
                {
                    staff = new();
                    idx = (int)EnumStaff.Id;
                    staff.Id = reader.GetFieldValue<int>(idx);
                    idx = (int)EnumStaff.Firstname;
                    staff.Firstname = reader.GetFieldValue<string?>(idx);
                    idx = (int) EnumStaff.Lastname;
                    staff.Lastname = reader.GetFieldValue<string?>(idx);
                    idx = (int)EnumStaff.Email;
                    staff.Email = reader.GetFieldValue<string?>(idx);
                    idx = (int)EnumStaff.Active;
                    staff.Active = reader.GetFieldValue<bool?>(idx);
                    idx = (int)EnumStaff.Role;
                    staff.RoleType = reader.GetFieldValue<int?>(idx);
                    idx = (int)EnumStaff.Password;
                    if (!reader.IsDBNull(idx))
                        staff.Password = reader.GetFieldValue<string?>(idx);

                    list.Add(staff);
                }

            }
            return list;
        }

    }
}
