import sys
from django.core.exceptions import ObjectDoesNotExist
from django.core.management.base import BaseCommand

from squad.core.models import Project, Build, Environment
from squad.core.tasks import UpdateProjectStatus


class Command(BaseCommand):

    help = """Move test runs identified by environment slug
    from one project to another. This action preserves
    datetime of the objects and statuses."""

    def add_arguments(self, parser):
        parser.add_argument(
            '--old-project-slug',
            dest="old_project_slug",
            help="Slug of the project from which to migrate test runs"
        )
        parser.add_argument(
            '--new-project-slug',
            dest="new_project_slug",
            help="Slug of the project to which to migrate test runs"
        )
        parser.add_argument(
            '--env-slug',
            dest="env_slug",
            help="Slug of the environment to migrate to new project"
        )

    def handle(self, *args, **options):
        self.options = options

        if not self.options['old_project_slug']:
            print("ERROR: old_project_slug missing")
            sys.exit(1)

        if not self.options['new_project_slug']:
            print("ERROR: new_project_slug missing")
            sys.exit(1)

        if not self.options['env_slug']:
            print("ERROR: env_slug missing")
            sys.exit(1)

        old_project = None
        new_project = None
        env = None
        try:
            old_project = Project.objects.get(slug=self.options['old_project_slug'])
        except ObjectDoesNotExist:
            print("Project: %s not found. Exiting" % self.options['old_project_slug'])
            sys.exit(0)
        try:
            new_project = Project.objects.get(slug=self.options['new_project_slug'])
        except ObjectDoesNotExist:
            print("Project: %s not found. Exiting" % self.options['new_project_slug'])
            sys.exit(0)
        try:
            env = Environment.objects.get(project=old_project, slug=self.options['env_slug'])
        except ObjectDoesNotExist:
            print("Environment: %s not found. Exiting" % self.options['env_slug'])
            sys.exit(0)
        print("Migrating testruns from project %s to %s" % (old_project.slug, new_project.slug))
        print("All test runs with environment name: %s will be migrated" % env.slug)

        for build in old_project.builds.all():
            if build.test_runs.filter(environment=env):
                print("moving build: %s" % build)
                new_build, _ = Build.objects.get_or_create(
                    version=build.version,
                    project=new_project,
                    created_at=build.created_at)
                for testrun in build.test_runs.filter(environment=env):
                    testrun.build = new_build
                    testrun.save()
                    testrun.environment.project = new_project
                    testrun.environment.save()
                    for testjob in testrun.test_jobs.all():
                        testjob.target = new_project
                        testjob.save()
                    UpdateProjectStatus()(testrun)
                new_build.status.created_at = build.status.created_at
                new_build.status.last_updated = build.status.last_updated
                new_build.status.save()
            else:
                print("No matching test runs found in build: %s" % build)

add mgmt command to migrate testruns between projects