import logging

from django.core.management.base import BaseCommand
from django.conf import settings

from products.models import CategoryTag


class Command(BaseCommand):

    help = 'Extract top level catefory tags'

    def handle(self, *args, **kwargs):
        # get all instances
        tags = CategoryTag.objects.all()
        top_tags = []

        print("Extracting top-level tags from CategoryTag instances")
        # extract tags with no ancestor
        for tag in tags:
            if not tag.get_ancestors():
                top_tags.append(tag.name)

        print("Saving top-level tags to file")
        # save results to dataset/top_tags.txt
        path = f"{settings.BASE_DIR}/../datasets/top_tags.txt"
        with open(path, 'wt') as f:
            f.writelines([tag + '\n' for tag in top_tags])

        # print out results
        logging.info(f"Extracted {len(top_tags)} to {path}")
