"""No-repository fallback page."""

from __future__ import annotations

from pathlib import Path

from pyratatui import Frame, Paragraph, Rect, Style, Text

from pygittools.tui.keys import KeyLike, is_quit
from pygittools.tui.pages.help import help_page
from pygittools.tui.types import PageAction, PageContext, PageResult


class NoRepoPage:
	title = "pgt"

	def __init__(self, start: Path) -> None:
		self._start = start

	def on_enter(self, ctx: PageContext) -> None:
		del ctx

	def status_text(self) -> str:
		return "no repository"

	def draw(self, frame: Frame, area: Rect) -> None:
		lines = "\n".join([
			"pgt",
			"",
			f"No git repository found under {self._start}",
			"",
			"Run pgt from inside a repository, or pass --repo PATH.",
		])
		frame.render_widget(
			Paragraph(Text.from_string(lines)).style(Style().bold()),
			area,
		)

	def handle_key(self, key: KeyLike) -> PageResult:
		if is_quit(key):
			return PageResult(action=PageAction.QUIT)
		if key.code.casefold() == "h":
			return PageResult(action=PageAction.PUSH, next_page=help_page())
		return PageResult()