"""Shared fixtures for pgt TUI tests."""

from __future__ import annotations

from pathlib import Path
from unittest.mock import MagicMock

import pytest
from pygit2 import Repository, Signature, init_repository

from pygittools.tui.types import PageContext

SIG = Signature("tester", "tester@example.com", 0)


@pytest.fixture
def repo(tmp_path: Path) -> Repository:
	repo_path = tmp_path / "repo"
	repo_path.mkdir()
	return init_repository(str(repo_path), bare=False)


@pytest.fixture
def page_ctx(repo: Repository) -> PageContext:
	return PageContext(repo=repo, repo_path=Path(repo.path))


@pytest.fixture
def committed_repo(repo: Repository) -> Repository:
	workdir = Path(repo.workdir)
	(workdir / "tracked.txt").write_text("v1\n", encoding="utf-8")
	repo.index.add("tracked.txt")
	repo.index.write()
	tree = repo.index.write_tree()
	repo.create_commit("HEAD", SIG, SIG, "initial", tree, [])
	return repo


@pytest.fixture(autouse=True)
def _mock_unicurses_io(monkeypatch: pytest.MonkeyPatch) -> None:
	"""Avoid requiring a real terminal for draw/status/tab helpers."""
	mock = MagicMock()
	for name in (
		"has_colors",
		"start_color",
		"use_default_colors",
		"init_pair",
		"can_change_color",
		"init_color",
	):
		monkeypatch.setattr(f"pygittools.tui.git_colors.{name}", mock, raising=False)
		monkeypatch.setattr(f"pygittools.tui.status_bar.{name}", mock, raising=False)
		monkeypatch.setattr(f"pygittools.tui.tabs.{name}", mock, raising=False)
	for name in ("move", "clrtoeol", "mvaddstr"):
		monkeypatch.setattr(f"pygittools.tui.draw.{name}", mock, raising=False)
		monkeypatch.setattr(f"pygittools.tui.status_bar.{name}", mock, raising=False)