from __future__ import annotations

import logging
from pathlib import Path
import sys
import unittest


SRC_ROOT = Path(__file__).resolve().parents[2] / "src"

if str(SRC_ROOT) not in sys.path:
    sys.path.insert(0, str(SRC_ROOT))


from ffx.constants import (
    DEFAULT_SHOW_INDEX_EPISODE_DIGITS,
    DEFAULT_SHOW_INDEX_SEASON_DIGITS,
    DEFAULT_SHOW_INDICATOR_EPISODE_DIGITS,
    DEFAULT_SHOW_INDICATOR_SEASON_DIGITS,
)
from ffx.helper import getEpisodeFileBasename
from ffx.show_descriptor import ShowDescriptor


class StaticConfig:
    def __init__(self, data: dict | None = None):
        self._data = data or {}

    def getData(self):
        return self._data


class ShowDescriptorDefaultTests(unittest.TestCase):
    def make_context(self, config_data: dict | None = None) -> dict:
        logger = logging.getLogger("ffx-test-show-descriptor-defaults")
        logger.handlers = []
        logger.addHandler(logging.NullHandler())
        return {"config": StaticConfig(config_data), "logger": logger}

    def test_show_descriptor_uses_config_defaults_when_context_is_present(self):
        descriptor = ShowDescriptor(
            context=self.make_context(
                {
                    "defaultIndexSeasonDigits": "1",
                    "defaultIndexEpisodeDigits": "3",
                    "defaultIndicatorSeasonDigits": "3",
                    "defaultIndicatorEpisodeDigits": "4",
                }
            ),
            id=1,
            name="Configured Show",
            year=2024,
        )

        self.assertEqual(1, descriptor.getIndexSeasonDigits())
        self.assertEqual(3, descriptor.getIndexEpisodeDigits())
        self.assertEqual(3, descriptor.getIndicatorSeasonDigits())
        self.assertEqual(4, descriptor.getIndicatorEpisodeDigits())
        self.assertEqual(0, descriptor.getQuality())
        self.assertEqual("", descriptor.getNotes())

    def test_show_descriptor_without_context_uses_shared_constants(self):
        descriptor = ShowDescriptor(id=1, name="Default Show", year=2024)

        self.assertEqual(DEFAULT_SHOW_INDEX_SEASON_DIGITS, descriptor.getIndexSeasonDigits())
        self.assertEqual(DEFAULT_SHOW_INDEX_EPISODE_DIGITS, descriptor.getIndexEpisodeDigits())
        self.assertEqual(
            DEFAULT_SHOW_INDICATOR_SEASON_DIGITS,
            descriptor.getIndicatorSeasonDigits(),
        )
        self.assertEqual(
            DEFAULT_SHOW_INDICATOR_EPISODE_DIGITS,
            descriptor.getIndicatorEpisodeDigits(),
        )
        self.assertEqual(0, descriptor.getQuality())
        self.assertEqual("", descriptor.getNotes())

    def test_show_descriptor_preserves_explicit_quality(self):
        descriptor = ShowDescriptor(id=1, name="Quality Show", year=2024, quality=23)

        self.assertEqual(23, descriptor.getQuality())

    def test_show_descriptor_preserves_explicit_notes(self):
        descriptor = ShowDescriptor(id=1, name="Notes Show", year=2024, notes="show notes")

        self.assertEqual("show notes", descriptor.getNotes())

    def test_episode_basename_uses_configured_digit_defaults_when_omitted(self):
        basename = getEpisodeFileBasename(
            "Configured Show",
            "Episode Name",
            2,
            7,
            context=self.make_context(
                {
                    "defaultIndexSeasonDigits": 1,
                    "defaultIndexEpisodeDigits": 3,
                    "defaultIndicatorSeasonDigits": 3,
                    "defaultIndicatorEpisodeDigits": 4,
                }
            ),
        )

        self.assertEqual(
            "Configured Show - 2007 Episode Name - S002E0007",
            basename,
        )


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