import argparse
import os
from os import PathLike
import time

from model import DecoderBase, make_model
from rich.progress import (
    BarColumn,
    MofNCompleteColumn,
    Progress,
    TextColumn,
    TimeElapsedColumn,
)

from evalplus.data import get_human_eval_plus


def construct_contract_prompt(prompt: str, contract_type: str, contract: str) -> str:
    if contract_type == "no":
        return prompt
    elif contract_type == "docstring":
        # embed within the docstring
        sep = ""
        if '"""' in prompt:
            sep = '"""'
        elif "'''" in prompt:
            sep = "'''"
        assert sep != ""
        l = prompt.split(sep)
        contract = "\n".join([x.split("#")[0] for x in contract.splitlines()])
        l[1] = (
            l[1] + contract + "\n" + " " * (len(contract) - len(contract.lstrip()) - 1)
        )
        return sep.join(l)
    elif contract_type == "code":
        # at the beginning of the function
        contract = "\n".join([x.split("#")[0] for x in contract.splitlines()])
        return prompt + contract


def code_generate(args, workdir: PathLike, model: DecoderBase):
    with Progress(
        TextColumn(
            f"{args.dataset} •" + "[progress.percentage]{task.percentage:>3.0f}%"
        ),
        BarColumn(),
        MofNCompleteColumn(),
        TextColumn("•"),
        TimeElapsedColumn(),
    ) as p:
        for task_id, task in p.track(get_human_eval_plus().items()):
            if int(task_id.split("/")[1])<2:
                continue
            p_name = task_id.replace("/", "_")
            if args.use_contracts != "no" and task["contract"] == "":
                continue
            os.makedirs(os.path.join(workdir, p_name), exist_ok=True)
            log = f"Codegen: {p_name} @ {model}"
            n_existing = 0
            if args.resume:
                # count existing .py files
                n_existing = len(
                    [
                        f
                        for f in os.listdir(os.path.join(workdir, p_name))
                        if f.endswith(".py")
                    ]
                )
                if n_existing > 0:
                    log += f" (resuming from {n_existing})"

            nsamples = args.n_samples - n_existing
            p.console.print(log)

            sidx = args.n_samples - nsamples
            while sidx < args.n_samples:
                
                outputs = model.codegen(
                    "Please complete the following code snippet.\n"+task["prompt"],
                    do_sample=not args.greedy,
                    num_samples=args.n_samples - sidx,
                    )
                
                # print(outputs)
                if args.model == "claude":
                    outputs = outputs.replace("python\n", "")
                    with open(
                            os.path.join(workdir, p_name, f"{sidx}.py"),
                            "w",
                            encoding="utf-8",
                        ) as f:
                           f.write(outputs.strip())
                    sidx = sidx +  1
                    time.sleep(1)
                else:   
                    for impl in outputs:
                        try:
                            with open(
                                os.path.join(workdir, p_name, f"{sidx}.py"),
                                "w",
                                encoding="utf-8",
                            ) as f:
                                if args.model in {"chatgpt", "claude"}:
                                    f.write(impl)
                                else:
                                    f.write(task["prompt"] + impl)
                        except UnicodeEncodeError:
                            continue
                        sidx += 1


def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("--model", required=True, type=str)
    parser.add_argument("--path", required=True, type=str)
    parser.add_argument("--bs", default=1, type=int)
    parser.add_argument("--temperature", default=0.1, type=float)
    parser.add_argument("--dataset", default="humaneval", type=str)
    parser.add_argument("--n_samples", default=10, type=int)
    parser.add_argument("--resume", default=True)
    parser.add_argument("--use_contracts", default="no", type=str)
    parser.add_argument("--greedy", action="store_true")
    args = parser.parse_args()

    if args.dataset not in ["humaneval"]:
        raise NotImplementedError("Unsupported dataset: {}".format(args.dataset))

    if args.use_contracts not in ["no", "code", "docstring"]:
        raise NotImplementedError(
            "Unsupported contract usage: {}".format(args.use_contracts)
        )
    if args.greedy and (args.temperature != 0 or args.bs != 1 or args.n_samples != 1):
        raise ValueError(
            f"Greedy decoding is only supported with temperature({args.temperature}) = 0, batch_size({args.bs}) = 1"
            f" and n_samples({args.n_samples}) = 1"
        )

    # Make project dir
    root = os.getcwd()
    os.makedirs(root, exist_ok=True)
    # Make dataset dir
    os.makedirs(os.path.join(root, args.dataset), exist_ok=True)
    # Make dir for codes generated by each model
    args.model = args.model.lower()
    model = make_model(
        name=args.model, batch_size=args.bs, temperature=args.temperature, path=args.path
    )
    workdir = os.path.join(
        root,
        args.dataset,
        args.model
        + f"_temp_{args.temperature}"
        + ("" if args.use_contracts == "no" else f"-contract-{args.use_contracts}"),
    )
    os.makedirs(workdir, exist_ok=True)

    with open(os.path.join(workdir, "args.txt"), "w") as f:
        f.write(str(args))

    code_generate(args, workdir=workdir, model=model)


if __name__ == "__main__":
    main()