from pathlib import Path


def create_job(
        create_job_header,
        create_job_body,
        create_job_tail,
        job_name_prefix,
        scenario_name,
        job_name_suffix,
        queue_name,
        ncores,
        work_dir,
        run_dir,
        #
        config_file,
        ):
    """
    Create the job file.

    The jobs is created by assembling three parts: the job header,
    the body, and the final tail (post execution process).

    The different parameters will be injected in the respective job
    creation functions.

    Parameters
    ----------
    create_job_header : callable
        The function that will create the header.

    create_job_body : callable
        The function that will create the job body.

    create_job_tail: callable
        The function that will create the job tail.

    job_name_prefix : str
        A prefix for the job name. Normally this is the name of the job
        test case, for example the PDB ID.
        Injected in `create_job_header`.

    scenario_name : str
        The name of the benchmark scenario.
        Injected in `create_job_header`.

    job_name_suffix : str
        An additional suffix for the job name. Normally, `BM5`.
        Injected in `create_job_header`.

    queue_name : str
        The name of the queue. Injected in `create_job_header`.

    ncores : int
        The number of cpu cores to use in the jobs. Injected in
        `create_job_header`.

    work_dir : pathlib.Path
        The working dir of the example. That is, the directory where
        `input`, `jobs`, and `logs` reside. Injected in `create_job_header`.

    run_dir : pathlib.Path
        The running directory of the scenario.

    config_file : pathlib.Path
        Path to the scenario configuration file.
        Injected in `create_job_body`.

    Returns
    -------
    str
        The job file in the form of string.
    """
    # create job header
    job_name = f'{job_name_prefix}-{scenario_name}-{job_name_suffix}'
    std_out = str(Path('logs', 'haddock.out'))
    std_err = str(Path('logs', 'haddock.err'))

    job_header = create_job_header(
        job_name,
        work_dir=work_dir,
        stdout_path=std_out,
        stderr_path=std_err,
        queue=queue_name,
        ncores=ncores,
        )

    available_flag = str(Path(run_dir, 'AVAILABLE'))
    running_flag = str(Path(run_dir, 'RUNNING'))
    done_flag = str(Path(run_dir, 'DONE'))
    fail_flag = str(Path(run_dir, 'FAIL'))

    job_body = create_job_body(available_flag, running_flag, config_file)

    job_tail = create_job_tail(std_err, done_flag, fail_flag)

    return job_header + job_body + job_tail