import unittest

from indexer.chain import follow_chain, novel_from_wiki
from indexer.cluster import Post
from indexer.links import Link
from indexer.similarity import LexicalEmbedder
from indexer.wiki import WikiSeries


def _post(pid, title, author="WriterOne", nxt=None, created=1):
    next_ids = tuple(nxt or ())
    links = tuple(Link(i, "Next", "next") for i in next_ids)
    return Post(
        id=pid,
        title=title,
        author=author,
        created_utc=created,
        permalink=f"/r/HFY/comments/{pid}/",
        flair="OC-Series",
        next_ids=next_ids,
        links=links,
    )


class ChainTests(unittest.TestCase):
    def test_follows_next_with_similar_titles(self):
        posts = {
            "a1": _post("a1", "The Last Terran Part 1", nxt=["a2"], created=1),
            "a2": _post("a2", "The Last Terran Part 2", nxt=["a3"], created=2),
            "a3": _post("a3", "The Last Terran Part 3", created=3),
        }
        chain = follow_chain(posts["a1"], posts, embedder=LexicalEmbedder())
        self.assertEqual([p.id for p in chain], ["a1", "a2", "a3"])

    def test_skips_unrelated_next_link(self):
        posts = {
            "a1": _post("a1", "The Last Terran Part 1", nxt=["zzz"], created=1),
            "zzz": _post("zzz", "Completely Different Cookbook", author="SomeoneElse", created=2),
        }
        chain = follow_chain(posts["a1"], posts, embedder=LexicalEmbedder())
        self.assertEqual([p.id for p in chain], ["a1"])

    def test_fetcher_fills_missing_posts(self):
        seed = _post("a1", "Terra Part 1", nxt=["a2"], created=1)
        hidden = _post("a2", "Terra Part 2", created=2)
        by_id = {seed.id: seed}

        def fetch(ids):
            return [hidden] if "a2" in ids else []

        chain = follow_chain(seed, by_id, embedder=LexicalEmbedder(), fetcher=fetch)
        self.assertEqual([p.id for p in chain], ["a1", "a2"])
        self.assertIn("a2", by_id)

    def test_wiki_toc_then_follow_tail(self):
        by_id = {
            "a1": _post("a1", "Terra Part 1", nxt=["a2"], created=1),
            "a2": _post("a2", "Terra Part 2", nxt=["a3"], created=2),
            "a3": _post("a3", "Terra Part 3", created=3),
        }
        entry = WikiSeries(
            title="Terra",
            wiki_path="/r/hfy/wiki/series/terra",
            author="WriterOne",
            chapter_ids=("a1", "a2"),
            first_id="a1",
            page_found=True,
        )
        novel = novel_from_wiki(entry, by_id, embedder=LexicalEmbedder())
        self.assertIsNotNone(novel)
        self.assertEqual(novel.name, "Terra")
        self.assertEqual([c.id for c in novel.chapters], ["a1", "a2", "a3"])
        self.assertEqual(novel.wiki_path, "/r/hfy/wiki/series/terra")


if __name__ == "__main__":
    unittest.main()
