using PropertyFuzeData;

namespace EfformanceBillingData
{
    // **************************************************************************************************
    // This class is inherited from PropertyFuzeData.RecurringCharge only becuase I don't want to mess
    // with the source code in PropertyFuzeData.  This code should all be moved to the base class.
    // **************************************************************************************************
    public class RecurringCharge : PropertyFuzeData.RecurringCharge
    {
        // FromDate and ToDate should be changed in the base class to be nullable
        public DateTime? FromDate { get; set; }

        public DateTime? ToDate { get; set; }
        public ChargeType? ChargeType { get; set; }
        public Tenants? Tenants { get; set; }

        public PFDataResponse<RecurringCharge> GetRecurringCharges(string RestURL, string APIToken, GetRecurringChargesParms Filter)
        {
            // Filter for Recurring Charges
            string sRecurringChargeIDFilter = string.Empty;

            if (Filter.RecurringChargeIDs != null && Filter.RecurringChargeIDs.Count > 0)
            {
                sRecurringChargeIDFilter = $"RecurringChargeID,in,({string.Join(",", Filter.RecurringChargeIDs.Select(x => x.ToString()))});";
            }

            // Filter for ChargeTypes
            string sChargeTypeIDFilter = string.Empty;

            if (Filter.ChargeTypeIDs != null && Filter.ChargeTypeIDs.Count > 0)
            {
                sChargeTypeIDFilter = $"ChargeTypeID,in,({string.Join(",", Filter.ChargeTypeIDs.Select(x => x.ToString()))});";
            }

            // Filter for EntityKeyIDs
            string sEntityKeyIDFilter = string.Empty;

            if (Filter.EntityKeyIDs != null && Filter.EntityKeyIDs.Count > 0)
            {
                sEntityKeyIDFilter = $"EntityKeyID,in,({string.Join(",", Filter.EntityKeyIDs.Select(x => x.ToString()))});";
            }

            // Filter for EntityType
            string sEntityTypeFilter = string.Empty;

            if (Filter.EntityKeyIDs != null && Filter.EntityKeyIDs.Count > 0)
            {
                sEntityTypeFilter = $"EntityType,eq,{Filter.EntityType};";
            }

            // ActiveAsOfDate Filter
            string sActiveAsOfDateFilter = string.Empty;

            if (Filter.ActiveAsOfDate != DateOnly.MinValue)
            {
                sActiveAsOfDateFilter =
                    $"FromDate,len,{Filter.ActiveAsOfDate:yyyy-MM-dd};" +
                    $"ToDate,gen,{Filter.ActiveAsOfDate:yyyy-MM-dd};";
            }

            // Tenant Statuses Filter
            string sTenantStatusesFilter = string.Empty;

            if (Filter.TenantStatuses != null && Filter.TenantStatuses.Count > 0)
            {
                sTenantStatusesFilter = $"Tenant.Status,in,({string.Join(",", Filter.TenantStatuses)});";
            }

            // Build the filter expression
            string sFilterExpression =
                (sRecurringChargeIDFilter +
                 sChargeTypeIDFilter +
                 sEntityKeyIDFilter +
                 sEntityTypeFilter +
                 sActiveAsOfDateFilter +
                 sTenantStatusesFilter).TrimEnd(';');

            // Build the field list
            string sFields =
                ("RecurringChargeID,EntityType,EntityKeyID,Frequency,ChargeTypeID,Amount,Comment,FromDate,ToDate," +
                 (Filter.IncludeChargeType ? "ChargeType(ChargeTypeID,Name,Description,GLAccountID,DefaultAmount)," : string.Empty) +
                 (Filter.IncludeTenant ? "Tenant(TenantID,TenantDisplayID,FirstName,LastName)," : string.Empty)).TrimEnd(',');

            // Build the embed list
            string sEmbeds =
                ((Filter.IncludeChargeType ? "ChargeType," : string.Empty) +
                 (Filter.IncludeTenant ? "Tenant" : string.Empty)).TrimEnd(',');

            string sQuery = "/RecurringCharges/Search";

            var GetOptions = new RentManagerAPIHelper.GetOptionsModel
            {
                Embeds = sEmbeds,
                FilterExpression = sFilterExpression,
                Fields = sFields,
                PageNumber = RentManagerAPIHelper.RMNullValue,
                PageSize = RentManagerAPIHelper.RMNullValue
            };

            var rm = new RentManagerAPIHelper();

            PFDataResponse<RecurringCharge> RecurringChargeList = rm.PostRentManagerData<RecurringCharge>(RestURL, APIToken, sQuery, GetOptions);

            return RecurringChargeList;
        }

        public class GetRecurringChargesParms
        {
            public List<int>? RecurringChargeIDs { get; set; }
            public List<int>? ChargeTypeIDs { get; set; }
            public List<int>? EntityKeyIDs { get; set; }
            public RentManagerAPIHelper.EntityType EntityType { get; set; }
            public DateOnly ActiveAsOfDate { get; set; }
            public List<string>? TenantStatuses { get; set; }
            public bool IncludeChargeType { get; set; }
            public bool IncludeTenant { get; set; }
        }
    }
}
