"""Tests for the artip module."""

import pytest
import polars as pl
from datetime import date
from artip import Artip, InvalidIntervalError


class TestArtip:
    """Test cases for the Artip class."""

    def test_init_valid(self):
        """Test valid initialization."""
        artip = Artip(min_time=date(2020, 1, 1), max_time=date(2025, 12, 31))
        assert artip.min_time == date(2020, 1, 1)
        assert artip.max_time == date(2025, 12, 31)
        assert artip.granularity == 'day'

    def test_init_invalid_min_max(self):
        """Test invalid min_time >= max_time."""
        with pytest.raises(ValueError):
            Artip(min_time=date(2025, 1, 1), max_time=date(2020, 1, 1))

    def test_init_invalid_granularity(self):
        """Test invalid granularity."""
        with pytest.raises(ValueError):
            Artip(min_time=date(2020, 1, 1), max_time=date(2025, 12, 31), granularity='invalid')

    def test_make_period_id_basic(self):
        """Test basic period ID generation."""
        artip = Artip(min_time=date(2020, 1, 1), max_time=date(2025, 12, 31))
        period_id = artip.make_period_id(start=date(2023, 1, 1), end=date(2023, 1, 2))
        assert isinstance(period_id, int)

    def test_make_period_id_too_large(self):
        """Test period ID with size exceeding max."""
        artip = Artip(min_time=date(2020, 1, 1), max_time=date(2025, 12, 31), period_max_size=1)
        with pytest.raises(InvalidIntervalError):
            artip.make_period_id(start=date(2023, 1, 1), end=date(2023, 1, 3))

    def test_make_calendar_table(self):
        """Test calendar table generation."""
        artip = Artip(min_time=date(2020, 1, 1), max_time=date(2025, 12, 31))
        artip.make_period_id(start=date(2023, 1, 1), end=date(2023, 1, 2))
        calendar = artip.make_calendar_table()
        assert isinstance(calendar, pl.LazyFrame)

        result = calendar.collect()
        assert result.filter(pl.col('label') == 'débute').height == 1
        assert result.filter(pl.col('label') == 'termine').height == 1
        assert result.filter(pl.col('label') == 'dans').height == 2

    def test_get_DAX_UDF(self):
        """Test DAX UDF generation."""
        artip = Artip(min_time=date(2020, 1, 1), max_time=date(2025, 12, 31))
        dax = artip.get_DAX_UDF()
        assert isinstance(dax, str)