
class MigrationListTargetData(object):
    """
    Target data which specified possible target objects which could be used in migration list.

    List of objects:
    - Target resellers - list of resellers to which you could reassign a subscription using migration list
    - Target customers - list of customers to which you could reassign a subscription using migration list
    - Target plans - list of plans to which you could reassign a subscription using migration list
    """

    def __init__(self, hosting_repository):
        """
        :type hosting_repository: parallels.core.hosting_repository.model.HostingRepositoryModel
        """
        self._hosting_repository = hosting_repository

    def get_resellers(self):
        """Get list of resellers as a dictionary.

        Example structure: {
            'john': {
                'contact_name': 'John Smith'
            },
            'tom': {
                'contact_name': 'Tom Smith'
            }
        }

        :rtype: dict
        """
        return {
            reseller.username: {
                'contact_name': reseller.first_name
            }
            for reseller in self._hosting_repository.reseller.get_list()
        }

    def get_customers(self):
        """Get list of customers as a dictionary.

        Example structure: {
            'john': {
                'contact_name': 'John Smith',
                'owner': 'john'  # owned by reseller with login 'john'
            },
            'tom': {
                'contact_name': 'Tom Smith',
                'owner': None  # owned by administrator
            }
        }

        :rtype: dict
        """
        admin_id = self._hosting_repository.panel.get_administrator_id(None)

        resellers_by_id = {
            reseller.reseller_id: reseller.username
            for reseller in self._hosting_repository.reseller.get_list()
        }

        result = {}

        for customer in self._hosting_repository.customer.get_list():
            if customer.owner_id == admin_id:
                owner = None
            else:
                if customer.owner_id in resellers_by_id:
                    owner = resellers_by_id[customer.owner_id]
                else:
                    # looks like inconsistency - customer is not owned by admin and any of resellers - just skip it
                    continue

            result[customer.username] = {
                'contact_name': customer.first_name,
                'owner': owner
            }

        return result

    def get_plans(self):
        """Get plans as a list of dictionaries

        Example structure: [
            {
                'name': 'Gold Hosting',
                'owner': None  # Plan owned by admin
            },
            {
                'name': 'Silver hosting',
                'owner': 'john'  # Plan owned by reseller with login 'john'
            }
        ]


        :rtype: dict
        """
        admin_id = self._hosting_repository.panel.get_administrator_id(None)

        resellers_by_id = {
            reseller.reseller_id: reseller.username
            for reseller in self._hosting_repository.reseller.get_list()
        }

        result = []
        for plan in self._hosting_repository.service_plan.get_list():
            if plan.owner_id == admin_id:
                owner = None
            else:
                if plan.owner_id in resellers_by_id:
                    owner = resellers_by_id[plan.owner_id]
                else:
                    # looks like inconsistency - plan is not owned by admin and any of resellers - just skip it
                    continue

            result.append({
                'name': plan.name,
                'owner': owner
            })

        return result
