"""
Use this command to view the INSTALLED_APPS segments, displaying how the app
overrides are structured.
"""

import json

from django.core.management.base import BaseCommand
from django.core.serializers.json import DjangoJSONEncoder

from ocyan.main.settings.base import _installed_apps


class Command(BaseCommand):
    help = __doc__

    def add_arguments(self, parser):
        parser.add_argument(
            "segment",
            choices=("all", "front", "django", "ocyan", "apps", "oscar", "wagtail"),
            default="all",
            help="The segment to display",
            nargs="?",
        )

    def handle(self, *args, **options):
        if options["segment"] != "all":
            segment = getattr(_installed_apps, options["segment"])
            self.stdout.write(
                json.dumps(segment.representation, indent=2, cls=DjangoJSONEncoder)
            )
        else:
            self.stdout.write(str(_installed_apps))
