using Microsoft.EntityFrameworkCore;
using Domain.Entities;
using Models.Interfaces;
using Models.Models;
using System.Globalization;

namespace Models.Repositories
{
    public class PhSLookUpRepository(PhronCareOperationsHubContext context) : IPhSLookUpRepository
{
    #region Declaraciones
    private readonly PhronCareOperationsHubContext _context = context;
    #endregion

    public async Task<IEnumerable<ELookUpItem>> CustomersListAsync(string filter, int limit = 10)
        => await _context.PhSCustomers
            .Where(c => c.Name.Contains(filter))
            .OrderBy(c => c.Name)
            .Select(c => new ELookUpItem { Id = c.Id, Nombre = c.Name })
            .Take(limit)
            .ToListAsync();

        public async Task<IEnumerable<ELookUpItem>> InstitutionsListAsync(string filter, int limit = 10)
            => await _context.PhSInstitutions
                .Where(c => c.Name.Contains(filter))
                .OrderBy(c => c.Name)
                .Select(c => new ELookUpItem { Id = c.Id, Nombre = c.Name + " (" + c.Streetaddress + ")" })
                .Take(limit)
                .ToListAsync();
        public async Task<IEnumerable<ELookUpItem>> PatientsListAsync(string filter, int limit = 10)
        {
            var ti = CultureInfo.CurrentCulture.TextInfo;

            return await _context.PhSPatients
                .Where(c => c.Firstname.Contains(filter) || c.Lastname.Contains(filter))
                .OrderBy(c => c.Firstname)
                .Select(c => new ELookUpItem
                {
                    Id = c.Id,
                    Nombre = ti.ToTitleCase(c.Firstname.ToLower()) + " " + ti.ToTitleCase(c.Lastname.ToLower())
                })
                .Take(limit)
                .ToListAsync();
        }
        public async Task<IEnumerable<ELookUpItem>> ProfessionalsListAsync(string filter, int limit = 10)
        {
            TextInfo ti = CultureInfo.CurrentCulture.TextInfo;

            return await _context.PhSProfessionals
                .Where(c => c.Fullname.Contains(filter))
                .OrderBy(c => c.Fullname)
                .Select(c => new ELookUpItem
                {
                    Id = c.Id,
                    Nombre = ti.ToTitleCase(c.Fullname.ToLower())
                })
                .Take(limit)
                .ToListAsync();
        }
        public async Task<IEnumerable<ELookUpItem>> PeopleListAsync(string filter, int limit = 10)
            => await _context.PhSPeople
                .Where(c => c.Name.Contains(filter) && c.Active)
                .OrderBy(c => c.Name)
                .Select(c => new ELookUpItem { Id = c.Id, Nombre = c.Name })
                .Take(limit)
                .ToListAsync();
        public async Task<IEnumerable<ELookUpItem>> BussinessUnitsListAsync(string filter, int limit = 10)
            => await _context.PhSBusinessUnits
                .Where(c => c.Code.Contains(filter) || c.Description.Contains(filter))
                .OrderBy(c => c.Code)
                .Select(c => new ELookUpItem { Id = c.Id, Nombre = c.Code + " | " + c.Description })
                .Take(limit)
                .ToListAsync();
        public async Task<IEnumerable<EProductLookupItem>> ProductsListAsync(string filter, int limit = 10)
            => await _context.PhSProducts
                .Where(c => c.Name.Contains(filter) || c.Description.Contains(filter))
                .OrderBy(c => c.Name)
                .Select(c => new EProductLookupItem { Id = c.Id, Code=c.Businessunits.Code, Description = c.Description , UnitPrice= c.Baseprice })
                .Take(limit)
                .ToListAsync();
        public async Task<IEnumerable<ELookUpItem>> PaymentTermsListAsync(string filter = "", int limit = 10)
        {
            var query = _context.PhSPaymentTerms
                .Where(p => p.Isactive);

            if (!string.IsNullOrWhiteSpace(filter))
                query = query.Where(p => p.Description.Contains(filter));

            return await query
                .OrderBy(p => p.Days)
                .Select(p => new ELookUpItem { Id = p.Id, Nombre = p.Description })
                .Take(limit)
                .ToListAsync();
        }
        public async Task<IEnumerable<ELookUpItem>> ApprovedQuotesListAsync(string filter, int limit = 10)
        {
            return await (
                from q in _context.PhSQuoteHeaders
                join c in _context.PhSCustomers on q.CustomerId equals c.Id
                where q.Status == "Aprobado" &&
                      (q.Quotenumber.Contains(filter) || c.Name.Contains(filter))
                orderby q.Issuedate descending
                select new ELookUpItem
                {
                    Id = q.Id,
                    Nombre = q.Quotenumber + " - " + c.Name
                }
            )
            .Take(limit)
            .ToListAsync();
        }

    }
}
