from __future__ import annotations

import os
from pathlib import Path
import sys
import tempfile
import unittest

from click.testing import CliRunner


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 import cli  # noqa: E402


class CutOptionCliTests(unittest.TestCase):
    def invoke_convert(self, *args: str):
        runner = CliRunner()

        with tempfile.TemporaryDirectory() as home_dir:
            result = runner.invoke(
                cli.ffx,
                [
                    "--database-file",
                    os.path.join(home_dir, "ffx.db"),
                    "--dry-run",
                    "convert",
                    "--no-tmdb",
                    *args,
                ],
                env={**os.environ, "HOME": home_dir},
            )

        self.assertEqual(0, result.exit_code, result.output)
        return result.output

    def test_convert_without_cut_prints_no_cut_message(self):
        output = self.invoke_convert()

        self.assertNotIn("Cutting enabled:", output)

    def test_convert_with_cut_flag_prints_default_cut_message(self):
        output = self.invoke_convert("--cut")

        self.assertIn("Cutting enabled: start 60 s, duration 180 s.", output)

    def test_convert_with_cut_duration_prints_zero_start_message(self):
        output = self.invoke_convert("--cut", "45")

        self.assertIn("Cutting enabled: start 0 s, duration 45 s.", output)

    def test_convert_with_cut_start_and_duration_prints_both_values(self):
        output = self.invoke_convert("--cut", "12,34")

        self.assertIn("Cutting enabled: start 12 s, duration 34 s.", output)


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