from __future__ import print_function

import json
import os

from django.conf import settings
from django.contrib.auth.hashers import make_password
from django.contrib.auth.models import User
from wagtail.wagtailcore.models import Page, Site

from v1.models import HomePage, BrowseFilterablePage


def run():
    print('Running script \'scripts.initial_data\' ...')
    admin_user = None
    site_root = None
    events = None

    admin_user = User.objects.filter(username='admin')
    if not admin_user:
        admin_user = User(username='admin',
                            password=make_password(os.environ.get('WAGTAIL_ADMIN_PW')),
                            is_superuser=True, is_active=True, is_staff=True)
        admin_user.save()
    else:
        admin_user = admin_user[0]

    # Creates a new site root `CFGov`
    site_root = HomePage.objects.filter(title='CFGOV')
    if not site_root:
        root = Page.objects.first()
        site_root = HomePage(title='CFGOV', slug='home-page', depth=2, owner=admin_user)
        site_root.live = True
        root.add_child(instance=site_root)
        latest = site_root.save_revision(user=admin_user, submitted_for_moderation=False)
        latest.save()
    else:
        site_root = site_root[0]

    # Setting new site root
    if not Site.objects.filter(hostname='content.localhost').exists():
        site = Site.objects.first()
        site.port = 8000
        site.root_page_id = site_root.id
        site.save()
        content_site = Site(hostname='content.localhost', port=8000, root_page_id=site_root.id)
        content_site.save()

        # Clean Up
        old_site_root = Page.objects.filter(id=2)[0]
        if old_site_root:
            old_site_root.delete()

    # Events Browse Page required for event `import-data` command
    if not BrowseFilterablePage.objects.filter(title='Events').exists():
        events = BrowseFilterablePage(title='Events', slug='events', owner=admin_user)
        site_root.add_child(instance=events)
        revision = events.save_revision(
            user=admin_user,
            submitted_for_moderation=False,
        )
        revision.publish()

    # Archived Events Browse Filterable Page
    if not BrowseFilterablePage.objects.filter(title='Archive').exists():
        archived_events = BrowseFilterablePage(title='Archive', slug='archive', owner=admin_user)
        if not events:
            events = BrowseFilterablePage.objects.get(title='Events')

        events.add_child(instance=archived_events)
        revision = archived_events.save_revision(
            user=admin_user,
            submitted_for_moderation=False,
        )
        revision.publish()

[((20, 17, 20, 54), 'django.contrib.auth.models.User.objects.filter', 'User.objects.filter', (), '', False, 'from django.contrib.auth.models import User\n'), ((30, 16, 30, 54), 'v1.models.HomePage.objects.filter', 'HomePage.objects.filter', (), '', False, 'from v1.models import HomePage, BrowseFilterablePage\n'), ((32, 15, 32, 35), 'wagtail.wagtailcore.models.Page.objects.first', 'Page.objects.first', ({}, {}), '()', False, 'from wagtail.wagtailcore.models import Page, Site\n'), ((33, 20, 33, 88), 'v1.models.HomePage', 'HomePage', (), '', False, 'from v1.models import HomePage, BrowseFilterablePage\n'), ((43, 15, 43, 35), 'wagtail.wagtailcore.models.Site.objects.first', 'Site.objects.first', ({}, {}), '()', False, 'from wagtail.wagtailcore.models import Page, Site\n'), ((47, 23, 47, 95), 'wagtail.wagtailcore.models.Site', 'Site', (), '', False, 'from wagtail.wagtailcore.models import Page, Site\n'), ((57, 17, 57, 86), 'v1.models.BrowseFilterablePage', 'BrowseFilterablePage', (), '', False, 'from v1.models import HomePage, BrowseFilterablePage\n'), ((67, 26, 67, 97), 'v1.models.BrowseFilterablePage', 'BrowseFilterablePage', (), '', False, 'from v1.models import HomePage, BrowseFilterablePage\n'), ((51, 24, 51, 49), 'wagtail.wagtailcore.models.Page.objects.filter', 'Page.objects.filter', (), '', False, 'from wagtail.wagtailcore.models import Page, Site\n'), ((69, 21, 69, 69), 'v1.models.BrowseFilterablePage.objects.get', 'BrowseFilterablePage.objects.get', (), '', False, 'from v1.models import HomePage, BrowseFilterablePage\n'), ((42, 11, 42, 60), 'wagtail.wagtailcore.models.Site.objects.filter', 'Site.objects.filter', (), '', False, 'from wagtail.wagtailcore.models import Page, Site\n'), ((56, 11, 56, 62), 'v1.models.BrowseFilterablePage.objects.filter', 'BrowseFilterablePage.objects.filter', (), '', False, 'from v1.models import HomePage, BrowseFilterablePage\n'), ((66, 11, 66, 63), 'v1.models.BrowseFilterablePage.objects.filter', 'BrowseFilterablePage.objects.filter', (), '', False, 'from v1.models import HomePage, BrowseFilterablePage\n'), ((23, 51, 23, 85), 'os.environ.get', 'os.environ.get', ({(23, 66, 23, 84): '"""WAGTAIL_ADMIN_PW"""'}, {}), "('WAGTAIL_ADMIN_PW')", False, 'import os\n')]