import subprocess
from pathlib import Path
from typing import Literal, override

from pydantic import BaseModel

from potent.directives._base import BaseDirective, DirectiveResult
from potent.util import truthy_list


class Config(BaseModel):
    branch: str
    create: bool = False


class SwitchBranch(BaseDirective):
    """
    Creates a branch if missing. Re-verifies that you're on that branch during every run.
    """

    slug: Literal["switch-branch"]
    config: Config
    """
    branch name
    """
    # base: Optional[str]
    # """
    # Branch to base the new branch off of. Will switch to this first to create the branch.
    # """

    @override
    def _run(self, directory: Path) -> DirectiveResult:
        result = self._run_cmd(
            directory,
            ["git", "switch", "-c" if self.config.create else "", self.config.branch],
        )

        return DirectiveResult.from_process(result)

        if result.returncode == 0:
            return True

        print(result.stderr)
        return False

    # def verify(self) -> None:
    #     """
    #     verify branch on every run
    #     """
