import argparse
import os

import jsonschema
import yaml  # type: ignore
from django.apps import apps
from django.core.management import BaseCommand, call_command


class Command(BaseCommand):

    # Path to example config file
    example_configfile = os.path.relpath(
        os.path.join(
            apps.get_app_config("DataRepo").path,
            "example_data" "small_dataset" "small_obob_study_params.yaml",
        )
    )

    # Path to config file schema
    schema_path = os.path.relpath(
        os.path.join(
            apps.get_app_config("DataRepo").path,
            "schemas",
            "load_study.yaml",
        )
    )

    # Show this when the user types help
    help = (
        "Loads compounds, animals, samples, and accucor data using a YAML "
        "file to specify parameters. "
        "Example usage: manage.py load_study config_file.yaml "
    )

    def add_arguments(self, parser):
        parser.add_argument(
            "study_params",
            type=argparse.FileType("r"),
            help=(
                "File containing parameters used to load study data "
                f"EXAMPLE {self.example_configfile} : "
                f"SCHEMA {self.schema_path}"
            ),
        )

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

        # Read load study parameters
        study_params = yaml.safe_load(options["study_params"])
        study_dir = os.path.dirname(os.path.realpath(options["study_params"].name))

        with open(self.schema_path, "r") as stream:
            schema = yaml.safe_load(stream)

        # Validate the configuration
        jsonschema.validate(study_params, schema)
        self.stdout.write(
            self.style.SUCCESS(
                f"Validated study parameters {options['study_params'].name}"
            )
        )
        if "compounds" in study_params:
            compounds_file = os.path.join(study_dir, study_params["compounds"])
            self.stdout.write(
                self.style.MIGRATE_HEADING(f"Loading compounds from {compounds_file}")
            )
            call_command("load_compounds", compounds=compounds_file)

        if "animals_samples_treatments" in study_params:
            # Read in animals and samples file
            animals_samples_table_file = os.path.join(
                study_dir, study_params["animals_samples_treatments"]["table"]
            )
            self.stdout.write(
                self.style.MIGRATE_HEADING(
                    f"Loading animals and samples from {animals_samples_table_file}"
                )
            )
            if "headers" in study_params["animals_samples_treatments"]:
                headers_file = os.path.join(
                    study_dir, study_params["animals_samples_treatments"]["headers"]
                )
            else:
                headers_file = None
            skip_researcher_check = study_params["animals_samples_treatments"].get(
                "skip_researcher_check", False
            )
            call_command(
                "load_animals_and_samples",
                animal_and_sample_table_filename=animals_samples_table_file,
                table_headers=headers_file,
                skip_researcher_check=skip_researcher_check,
            )

        if "accucor_data" in study_params:

            # Get parameters for all accucor files
            protocol = study_params["accucor_data"]["msrun_protocol"]
            date = study_params["accucor_data"]["date"]
            researcher = study_params["accucor_data"]["researcher"]
            new_researcher = study_params["accucor_data"]["new_researcher"]
            skip_samples = None
            if "skip_samples" in study_params["accucor_data"]:
                skip_samples = study_params["accucor_data"]["skip_samples"]
            sample_name_prefix = None
            if "sample_name_prefix" in study_params["accucor_data"]:
                sample_name_prefix = study_params["accucor_data"]["sample_name_prefix"]

            # Read in accucor data files
            for accucor_file in study_params["accucor_data"]["accucor_files"]:
                accucor_file_name = accucor_file["name"]
                self.stdout.write(
                    self.style.MIGRATE_HEADING(
                        f"Loading accucor_data from {accucor_file_name}"
                    )
                )
                # Get parameters specific to each accucor file
                if "msrun_protocol" in accucor_file:
                    protocol = accucor_file["msrun_protocol"]
                if "date" in accucor_file:
                    date = accucor_file["date"]
                if "researcher" in accucor_file:
                    researcher = accucor_file["researcher"]
                if "new_researcher" in accucor_file:
                    new_researcher = accucor_file["new_researcher"]
                if "skip_samples" in accucor_file:
                    skip_samples = accucor_file["skip_samples"]
                if "sample_name_prefix" in accucor_file:
                    sample_name_prefix = accucor_file["sample_name_prefix"]
                    print(f"PREFIX: {sample_name_prefix}")

                call_command(
                    "load_accucor_msruns",
                    accucor_file=os.path.join(study_dir, accucor_file_name),
                    protocol=protocol,
                    date=date,
                    researcher=researcher,
                    new_researcher=new_researcher,
                    skip_samples=skip_samples,
                    sample_name_prefix=sample_name_prefix,
                )

        self.stdout.write(self.style.SUCCESS("Done loading study"))

import argparse
import os

import jsonschema
import yaml  # type: ignore
from django.apps import apps
from django.core.management import BaseCommand, call_command


class Command(BaseCommand):

    # Path to example config file
    example_configfile = os.path.relpath(
        os.path.join(
            apps.get_app_config("DataRepo").path,
            "example_data" "small_dataset" "small_obob_study_params.yaml",
        )
    )

    # Path to config file schema
    schema_path = os.path.relpath(
        os.path.join(
            apps.get_app_config("DataRepo").path,
            "schemas",
            "load_study.yaml",
        )
    )

    # Show this when the user types help
    help = (
        "Loads compounds, animals, samples, and accucor data using a YAML "
        "file to specify parameters. "
        "Example usage: manage.py load_study config_file.yaml "
    )

    def add_arguments(self, parser):
        parser.add_argument(
            "study_params",
            type=argparse.FileType("r"),
            help=(
                "File containing parameters used to load study data "
                f"EXAMPLE {self.example_configfile} : "
                f"SCHEMA {self.schema_path}"
            ),
        )

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

        # Read load study parameters
        study_params = yaml.safe_load(options["study_params"])
        study_dir = os.path.dirname(os.path.realpath(options["study_params"].name))

        with open(self.schema_path, "r") as stream:
            schema = yaml.safe_load(stream)

        # Validate the configuration
        jsonschema.validate(study_params, schema)
        self.stdout.write(
            self.style.SUCCESS(
                f"Validated study parameters {options['study_params'].name}"
            )
        )
        if "compounds" in study_params:
            compounds_file = os.path.join(study_dir, study_params["compounds"])
            self.stdout.write(
                self.style.MIGRATE_HEADING(f"Loading compounds from {compounds_file}")
            )
            call_command("load_compounds", compounds=compounds_file)

        if "animals_samples_treatments" in study_params:
            # Read in animals and samples file
            animals_samples_table_file = os.path.join(
                study_dir, study_params["animals_samples_treatments"]["table"]
            )
            self.stdout.write(
                self.style.MIGRATE_HEADING(
                    f"Loading animals and samples from {animals_samples_table_file}"
                )
            )
            if "headers" in study_params["animals_samples_treatments"]:
                headers_file = os.path.join(
                    study_dir, study_params["animals_samples_treatments"]["headers"]
                )
            else:
                headers_file = None
            skip_researcher_check = study_params["animals_samples_treatments"].get(
                "skip_researcher_check", False
            )
            call_command(
                "load_animals_and_samples",
                animal_and_sample_table_filename=animals_samples_table_file,
                table_headers=headers_file,
                skip_researcher_check=skip_researcher_check,
            )

        if "accucor_data" in study_params:

            # Get parameters for all accucor files
            protocol = study_params["accucor_data"]["msrun_protocol"]
            date = study_params["accucor_data"]["date"]
            researcher = study_params["accucor_data"]["researcher"]
            new_researcher = study_params["accucor_data"]["new_researcher"]
            skip_samples = None
            if "skip_samples" in study_params["accucor_data"]:
                skip_samples = study_params["accucor_data"]["skip_samples"]
            sample_name_prefix = None
            if "sample_name_prefix" in study_params["accucor_data"]:
                sample_name_prefix = study_params["accucor_data"]["sample_name_prefix"]

            # Read in accucor data files
            for accucor_file in study_params["accucor_data"]["accucor_files"]:
                accucor_file_name = accucor_file["name"]
                self.stdout.write(
                    self.style.MIGRATE_HEADING(
                        f"Loading accucor_data from {accucor_file_name}"
                    )
                )
                # Get parameters specific to each accucor file
                if "msrun_protocol" in accucor_file:
                    protocol = accucor_file["msrun_protocol"]
                if "date" in accucor_file:
                    date = accucor_file["date"]
                if "researcher" in accucor_file:
                    researcher = accucor_file["researcher"]
                if "new_researcher" in accucor_file:
                    new_researcher = accucor_file["new_researcher"]
                if "skip_samples" in accucor_file:
                    skip_samples = accucor_file["skip_samples"]
                if "sample_name_prefix" in accucor_file:
                    sample_name_prefix = accucor_file["sample_name_prefix"]
                    print(f"PREFIX: {sample_name_prefix}")

                call_command(
                    "load_accucor_msruns",
                    accucor_file=os.path.join(study_dir, accucor_file_name),
                    protocol=protocol,
                    date=date,
                    researcher=researcher,
                    new_researcher=new_researcher,
                    skip_samples=skip_samples,
                    sample_name_prefix=sample_name_prefix,
                )

        self.stdout.write(self.style.SUCCESS("Done loading study"))

# type: ignore # Path to example config file # Path to config file schema # Show this when the user types help # Read load study parameters # Validate the configuration # Read in animals and samples file # Get parameters for all accucor files # Read in accucor data files # Get parameters specific to each accucor file