# By: Riasat Ullah
# This file contains functions that will help clean up the cache.

from cache_queries import cache_organizations, cache_policies, cache_services, cache_users, cache_workflows
from taskcallrest import settings


def clean_cached_organization_month_costs(client, retain_org_ids):
    '''
    Cleans up organization month costs cache.
    :param client: cache client
    :param retain_org_ids: (list of int) of organization IDs
    '''
    if settings.CACHE_ON:
        if len(retain_org_ids) == 0:
            cache_organizations.remove_all_organization_month_costs(client)
        else:
            cached_org_ids = cache_organizations.get_organization_ids_from_cached_month_costs(client)
            ids_to_remove = list(set(cached_org_ids).difference(retain_org_ids))
            cache_organizations.remove_organization_month_costs(client, ids_to_remove)


def clean_cached_policies(client, retain_policy_ids):
    '''
    Cleans up the policies and policy references cache.
    :param client: cache client
    :param retain_policy_ids: (list of int) of policy IDs to retain
    '''
    if settings.CACHE_ON:
        if len(retain_policy_ids) == 0:
            cache_policies.remove_all_policies(client)
        else:
            cached_policy_ids = cache_policies.get_cached_policy_ids(client)
            ids_to_remove = list(set(cached_policy_ids).difference(retain_policy_ids))
            cache_policies.remove_policies(client, ids_to_remove)


def clean_cached_services(client, retain_service_ids):
    '''
    Cleans up the services and service references cache.
    :param client: cache client
    :param retain_service_ids: (list of int) of service IDs to retain
    '''
    if settings.CACHE_ON:
        if len(retain_service_ids) == 0:
            cache_users.remove_all_user_policy_info(client)
        else:
            cached_service_ids = cache_services.get_cached_service_ids(client)
            ids_to_remove = list(set(cached_service_ids).difference(retain_service_ids))
            cache_services.remove_services(client, ids_to_remove)


def clean_cached_user_info(client, retain_user_policy_ids):
    '''
    Cleans up the user policy info cache.
    :param client: cache client
    :param retain_user_policy_ids: (list of int) of user policy IDs to retain
    '''
    if settings.CACHE_ON:
        if len(retain_user_policy_ids) == 0:
            cache_users.remove_all_user_policy_info(client)
        else:
            cached_user_pol_ids = cache_users.get_cached_user_policy_id_from_info(client)
            ids_to_remove = list(set(cached_user_pol_ids).difference(retain_user_policy_ids))
            cache_users.remove_user_policy_info(client, ids_to_remove)


def clean_cached_workflows(client, retain_org_ids):
    '''
    Cleans up organization workflows cache.
    :param client: cache client
    :param retain_org_ids: (list of int) of organization IDs
    '''
    if settings.CACHE_ON:
        if len(retain_org_ids) == 0:
            cache_workflows.remove_all_workflows(client)
        else:
            cached_org_ids = cache_workflows.get_organization_ids_from_cached_workflows(client)
            ids_to_remove = list(set(cached_org_ids).difference(retain_org_ids))
            cache_workflows.remove_organization_workflows(client, ids_to_remove)
