from django.core.management import BaseCommand
from django.core.management import call_command
from django.conf import settings
import json
import os


class Command(BaseCommand):
    def handle(self, *args, **options):
        schemas = [
            {
                'filename': 'schema.json',
                'schema': 'api.schema.schema'
            },
            {
                'filename': 'schema_public.json',
                'schema': 'api.schema_public.schema'
            }
        ]

        for schema in schemas:
            self.create_schema(schema['filename'], schema['schema'])

    def create_schema(self, filename, schema):
        cypress_path = os.path.join(settings.BASE_DIR, '..', 'client', 'cypress', 'fixtures', filename)
        call_command('graphql_schema', schema=schema, out=filename, indent=4)
        with open(filename) as f:
            initial_json = json.loads(f.read())

        data = initial_json['data']
        with open(cypress_path, 'w') as o:
            o.write(json.dumps(data))
