using LSFE.MobApp.Contracts;
using LSFE.MobApp.Entities.Location;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LSFE.MobApp.Services
{
    public static class RepositoryExtensions
    {
        public static async Task<T> FindByPropertyAsync<T>(this IRepository<T> repository, string propertyName, object value) where T : class, new()
        {
            // Create a dynamic query based on property name and value
            var query = $"SELECT * FROM {typeof(T).Name}s WHERE {propertyName} = ?";
            var results = await repository.QueryAsync(query, value);
            return results.FirstOrDefault();
        }

        public static async Task<Institution> FindByInstitutionIdAsync(this IRepository<Institution> repository, int institutionId)
        {
            return await repository.FirstOrDefaultAsync(x => x.InstitutionId == institutionId);
        }

        public static async Task<List<Institution>> GetByTerritoryIdAsync(this IRepository<Institution> repository, int territoryId)
        {
            return await repository.WhereAsync(x => x.TerritoryId == territoryId && x.IsActive);
        }
    }
}