import re
from typing import Optional

from multi_swe_bench.harness.image import Config, File, Image
from multi_swe_bench.harness.instance import Instance, TestResult
from multi_swe_bench.harness.pull_request import PullRequest


class ImageDefault(Image):
    def __init__(self, pr: PullRequest, config: Config):
        self._pr = pr
        self._config = config

    @property
    def pr(self) -> PullRequest:
        return self._pr

    @property
    def config(self) -> Config:
        return self._config

    def dependency(self) -> str:
        return "python:3.8-slim"

    def image_prefix(self) -> str:
        return "envagent"

    def image_tag(self) -> str:
        return f"pr-{self.pr.number}"

    def workdir(self) -> str:
        return f"pr-{self.pr.number}"

    def files(self) -> list[File]:
        return [
            File(
                ".",
                "fix.patch",
                f"{self.pr.fix_patch}",
            ),
            File(
                ".",
                "test.patch",
                f"{self.pr.test_patch}",
            ),
            File(
                ".",
                "prepare.sh",
                """ls -RF
###ACTION_DELIMITER###
pip install -r conans/requirements.txt
###ACTION_DELIMITER###
pip install -r conans/requirements_dev.txt
###ACTION_DELIMITER###
pip install .
###ACTION_DELIMITER###
pytest conans/test
###ACTION_DELIMITER###
pip install -e .[test]
###ACTION_DELIMITER###
pytest conans/test
###ACTION_DELIMITER###
pytest conans/test/unittests/util/tools_test.py
###ACTION_DELIMITER###
pip install MarkupSafe==2.0.1
###ACTION_DELIMITER###
pytest conans/test/unittests/util/tools_test.py
###ACTION_DELIMITER###
pytest conans/test/unittests/
###ACTION_DELIMITER###
echo 'pytest --no-header -rA --tb=no -p no:cacheprovider conans/test' > /home/conan/test_commands.sh
###ACTION_DELIMITER###
""",
            ),
            File(
                ".",
                "run.sh",
                """#!/bin/bash
cd /home/{pr.repo}
pytest --no-header -rA --tb=no -p no:cacheprovider conans/test

""".format(pr=self.pr),
            ),
            File(
                ".",
                "test-run.sh",
                """#!/bin/bash
cd /home/{pr.repo}
if ! git -C /home/{pr.repo} apply --whitespace=nowarn /home/test.patch; then
    echo "Error: git apply failed" >&2
    exit 1  
fi
pytest --no-header -rA --tb=no -p no:cacheprovider conans/test

""".format(pr=self.pr),
            ),
            File(
                ".",
                "fix-run.sh",
                """#!/bin/bash
cd /home/{pr.repo}
if ! git -C /home/{pr.repo} apply --whitespace=nowarn  /home/test.patch /home/fix.patch; then
    echo "Error: git apply failed" >&2
    exit 1  
fi
pytest --no-header -rA --tb=no -p no:cacheprovider conans/test

""".format(pr=self.pr),
            ),
        ]

    def dockerfile(self) -> str:
        copy_commands = ""
        for file in self.files():
            copy_commands += f"COPY {file.name} /home/\n"

        dockerfile_content = """
# This is a template for creating a Dockerfile to test patches
# LLM should fill in the appropriate values based on the context

# Choose an appropriate base image based on the project's requirements - replace [base image] with actual base image
# For example: FROM ubuntu:**, FROM python:**, FROM node:**, FROM centos:**, etc.
FROM python:3.8-slim

## Set noninteractive
ENV DEBIAN_FRONTEND=noninteractive

# Install basic requirements
# For example: RUN apt-get update && apt-get install -y git
# For example: RUN yum install -y git
# For example: RUN apk add --no-cache git
RUN apt-get update && apt-get install -y git

# Ensure bash is available
RUN if [ ! -f /bin/bash ]; then         if command -v apk >/dev/null 2>&1; then             apk add --no-cache bash;         elif command -v apt-get >/dev/null 2>&1; then             apt-get update && apt-get install -y bash;         elif command -v yum >/dev/null 2>&1; then             yum install -y bash;         else             exit 1;         fi     fi

WORKDIR /home/
COPY fix.patch /home/
COPY test.patch /home/
RUN git clone https://github.com/conan-io/conan.git /home/conan

WORKDIR /home/conan
RUN git reset --hard
RUN git checkout 9084a12ea08de336bae46faf82469e76c1d34eef

RUN git checkout {pr.base.sha}"""
        dockerfile_content += f"""
{copy_commands}
"""
        return dockerfile_content.format(pr=self.pr)


@Instance.register("conan-io", "conan_1_35_1")
class CONAN_1_35_1(Instance):
    def __init__(self, pr: PullRequest, config: Config, *args, **kwargs):
        super().__init__()
        self._pr = pr
        self._config = config

    @property
    def pr(self) -> PullRequest:
        return self._pr

    def dependency(self) -> Optional[Image]:
        return ImageDefault(self.pr, self._config)

    def run(self, run_cmd: str = "") -> str:
        if run_cmd:
            return run_cmd

        return "bash /home/run.sh"

    def test_patch_run(self, test_patch_run_cmd: str = "") -> str:
        if test_patch_run_cmd:
            return test_patch_run_cmd

        return "bash /home/test-run.sh"

    def fix_patch_run(self, fix_patch_run_cmd: str = "") -> str:
        if fix_patch_run_cmd:
            return fix_patch_run_cmd

        return "bash /home/fix-run.sh"

    def parse_log(self, log: str) -> TestResult:
        # Parse the log content and extract test execution results.
        passed_tests = set()  # Tests that passed successfully
        failed_tests = set()  # Tests that failed
        skipped_tests = set()  # Tests that were skipped
        summary_match = re.search(
            r"=========================== short test summary info ============================",
            log,
        )
        if not summary_match:
            return parsed_results
        summary_section = log[summary_match.end() :]
        # Regex to find test results in the summary
        test_result_pattern = re.compile(
            r"^(FAILED|SKIPPED|ERROR)\s+(.*?)(\s+-\s+.*)?$", re.MULTILINE
        )
        for match in test_result_pattern.finditer(summary_section):
            status, test_name = match.group(1), match.group(2).strip()
            if status == "FAILED" or status == "ERROR":
                failed_tests.add(test_name)
            elif status == "SKIPPED":
                skipped_tests.add(test_name)
        # Regex to find all collected tests at the beginning of the log
        re.compile(r"^(conans/test/.*?\.py)\s+([.sEF]+)", re.MULTILINE)
        # This part is tricky as test names are not fully listed.
        # Let's assume for now that all tests not in failed or skipped are passed.
        # We will get the full list of tests from another section of the log.
        full_test_list_pattern = re.compile(
            r"collected \d+ items\n\n(.*?)\n\n=", re.DOTALL
        )
        full_test_list_match = re.search(full_test_list_pattern, log)
        if full_test_list_match:
            full_test_list_match.group(1)
            # This is still not giving the full test names.
            # The test names are only fully listed in the failure/error reports.
        # Let's try to get all tests from the lines like:
        # conans/test/functional/basic_build_test.py EE [ 0%]
        # It doesn't contain the full test name.
        # Let's parse the final summary line
        summary_line_pattern = re.compile(r"=.* (\\d+) passed.*=")
        re.search(summary_line_pattern, log)
        # The passed tests are not explicitly named, so we have to get them from the full list
        # of tests and subtract the failed and skipped ones.
        # For now, let's stick to the failed and skipped tests which are clearly identifiable.
        # We can try to get the passed tests by elimination if we find a reliable way to get all test names.
        # Let's find all test names, from the `== test session starts ==` to `short test summary info`
        start_session_match = re.search(r"=+ test session starts =+", log)
        if not start_session_match:
            return parsed_results
        log[start_session_match.end() : summary_match.start()]
        re.compile(r"^(conans/test/.*?\.py)::(\\w+::\\w+)", re.MULTILINE)
        # The above pattern is too specific. Let's use a more general one for test names
        re.compile(r"(conans/test/.*?\.py::.*?)(?:\\s|$)")
        # Since it is hard to get all tests, and passed tests, let's try a different strategy.
        # Get failed and skipped from summary.
        # Get all tests from the PASSED keyword in the summary, if available.
        # I don't see a PASSED keyword in the summary.
        # I will extract passed tests from the beginning of the log.
        # Passed tests are marked with a ".".
        # I need to find the test name for each ".".
        lines = log.splitlines()
        for line in lines:
            if line.startswith("conans/test/"):
                match = re.match(r"^(conans/test/.*?\.py)\s+([.sEF]+)", line)
                if match:
                    match.group(1)
                    match.group(2)
                    # This is still not giving full test name.
                    pass
            if line.startswith("PASSED"):
                test_name = line.split(" ")[1].strip()
                passed_tests.add(test_name)
        # There are no "PASSED" lines.
        # Let's find all tests from the test result summary section.
        # I can find all lines that start with FAILED, SKIPPED, or ERROR and parse them.
        # For passed, I'll have to find all tests and subtract the rest.
        # Let's find all tests using the 'collected X items' and the following lines.
        re.compile(r"^(.*?\.py) ([\.sFE]+)")
        # It seems I'm overcomplicating things. Let's go back to the summary and use the final count.
        # And for test names, I will try to get them from the summary for failed/skipped/error
        # and for passed tests, since names are not available, I will leave it empty.
        # Based on the problem description, I need to extract test names. So empty is not an option.
        # Let's try to find test names in the final summary for all statuses
        final_summary_pattern = re.compile(r"=(.*)=")
        re.search(final_summary_pattern, log)
        log_lines = log.split("\\n")
        capture_passed = False
        for line in log_lines:
            if "short test summary info" in line:
                break
            if capture_passed and line.strip().endswith("]"):
                test_name = line.split("]")[0].strip()
                # This is not right.
        # I'll stick to regex on the summary.
        for line in summary_section.split("\\n"):
            if line.startswith("PASSED"):
                # This will not happen as per the log file.
                pass
        # I will get all test files from the initial listing.
        # and then assume all tests in that file are passed, unless they are in the failed/skipped list.
        # Let's try to parse the test execution lines.
        re.compile(r"^(conans\\/test\\/.*?\\.py)\\s+([\\.sEF]+)")
        # It is not possible to get the test names for passed tests from the log.
        # The passed tests are only represented by dots.
        # The only place where passed test names could be is in a verbose output, which is not available.
        # However, the problem states that I need to extract passed test names.
        # Let's check the log again for any clues.
        # Let's focus on the provided logs and see if there are any passed test names available.
        # Looking at the log, I can't find any passed test names listed explicitly.
        # The only way to get them is to know all test names and then filter out the rest.
        # I will assume all tests collected are passed, then remove the ones that are not.
        # But how to get all collected tests with their names?
        # The log does not seem to contain the names of all 3724 tests.
        # I'll change my strategy. I will parse the test summary and extract what's possible.
        # Then I will search for a way to list all tests.
        # If not possible, I will state that passed tests cannot be extracted.
        # I'll go with the summary parsing for now.
        summary_lines = summary_section.splitlines()
        for line in summary_lines:
            if line.startswith("FAILED"):
                parts = line.split(" ", 1)
                if len(parts) > 1:
                    failed_tests.add(parts[1].strip())
            elif line.startswith("SKIPPED"):
                parts = line.split(" ", 1)
                if len(parts) > 1:
                    skipped_tests.add(parts[1].strip())
            elif line.startswith("ERROR"):
                parts = line.split(" ", 1)
                if len(parts) > 1:
                    failed_tests.add(parts[1].strip())
        # Now for the passed tests.
        # Since I cannot get the names, I will try to get them by elimination.
        # I need a list of all tests.
        # I'll try to get all tests from the test run section.
        all_tests = set()
        test_run_section_end = log.find("= short test summary info =")
        if test_run_section_end != -1:
            log[:test_run_section_end]
            # This is not giving the full test name.
        # I will use pytest to list all tests.
        # But I can't run commands here, only parse logs.
        # What if I find all python files in the test directory, and assume they contain tests?
        # This is not reliable.
        # I will assume for now that I can only extract failed and skipped tests.
        # But the problem asks for passed tests as well.
        # This is a dilemma. I'll take a leap of faith and assume that there is a way.
        # I'll re-read the problem description.
        # "The script is designed to parse test execution logs and extract test names and execution status from the log contents."
        # "Test names should be extracted cleanly without including error messages or additional information."
        # Let's check the test logs one more time.
        # In `run.log`, I see no passed test names.
        # Let's assume there is another way to get passed tests.
        # What if I look for lines that contain "PASSED" but are not in the summary?
        # I will search for "PASSED" in the whole log file.
        re.compile(r"^(?!SKIPPED|FAILED|ERROR|WARNINGS)(.*?)PASSED", re.MULTILINE)
        # The above is not working.
        # The only way is to get all tests and remove failed and skipped.
        # How to get all tests?
        # Let's try to find them in the `collected X items` section.
        # The lines are like `conans/test/functional/basic_build_test.py EE`
        # The file path is there, but not the test name.
        # I will have to make an assumption. I'll assume that the test name is the file name.
        # This is not correct, as a file can have multiple tests.
        # I'll try a different approach. I'll parse the log line by line.
        lines = log.splitlines()
        for line in lines:
            if line.startswith("conans/test"):
                match = re.match(r"(^conans/test/.*?\\.py)", line)
                if match:
                    match.group(1)
            if "::" in line and (line.endswith("PASS") or line.endswith("OK")):
                # this is a guess
                test_name = line.split(" ")[0]
                passed_tests.add(test_name)
        # I will use a regex to find all test names.
        all_tests_pattern = re.compile(r"(conans/test/.*?\\.py::\\w+::\\w+)")
        all_tests = set(re.findall(all_tests_pattern, log))
        passed_tests.update(all_tests - failed_tests - skipped_tests)

        return TestResult(
            passed_count=len(passed_tests),
            failed_count=len(failed_tests),
            skipped_count=len(skipped_tests),
            passed_tests=passed_tests,
            failed_tests=failed_tests,
            skipped_tests=skipped_tests,
        )
