"""Tests for billing app models."""

import pytest
from django.utils import timezone

from .factories import PlanFactory, SubscriptionFactory


@pytest.mark.django_db
class TestPlan:
    """Tests for the Plan model."""

    def test_create_plan(self):
        """Test creating a plan with the factory."""
        plan = PlanFactory()
        assert plan.pk is not None
        assert plan.name.startswith("Plan")

    def test_plan_str(self):
        """Test plan string representation."""
        plan = PlanFactory(name="Pro", price_cents=9999, currency="usd")
        assert str(plan) == "Pro (USD 99.99)"

    def test_plan_price_property(self):
        """Test plan price property converts cents to dollars."""
        plan = PlanFactory(price_cents=1999)
        assert plan.price == 19.99

    def test_plan_price_zero(self):
        """Test plan with zero price (free tier)."""
        plan = PlanFactory(price_cents=0)
        assert plan.price == 0
        assert str(plan).endswith("0.00)")

    def test_plan_default_currency(self):
        """Test plan default currency is USD."""
        plan = PlanFactory()
        assert plan.currency == "usd"

    def test_plan_custom_currency(self):
        """Test plan with custom currency."""
        plan = PlanFactory(currency="eur")
        assert plan.currency == "eur"

    def test_plan_default_is_active(self):
        """Test plan is active by default."""
        plan = PlanFactory()
        assert plan.is_active is True

    def test_plan_can_be_inactive(self):
        """Test plan can be deactivated."""
        plan = PlanFactory(is_active=False)
        assert plan.is_active is False

    def test_plan_with_features(self):
        """Test plan with features list."""
        features = ["Unlimited members", "API access", "Priority support"]
        plan = PlanFactory(features=features)
        assert plan.features == features

    def test_plan_default_features(self):
        """Test plan has empty list as default features."""
        plan = PlanFactory()
        assert plan.features == []

    def test_plan_has_timestamps(self):
        """Test that plan has created_at and updated_at."""
        plan = PlanFactory()
        assert plan.created_at is not None
        assert plan.updated_at is not None


@pytest.mark.django_db
class TestSubscription:
    """Tests for the Subscription model."""

    def test_create_subscription(self):
        """Test creating a subscription with the factory."""
        subscription = SubscriptionFactory()
        assert subscription.pk is not None
        assert subscription.workspace is not None
        assert subscription.plan is not None

    def test_subscription_str(self):
        """Test subscription string representation."""
        subscription = SubscriptionFactory()
        expected = f"{subscription.workspace} on {subscription.plan}"
        assert str(subscription) == expected

    def test_subscription_default_status(self):
        """Test subscription default status is active."""
        subscription = SubscriptionFactory()
        assert subscription.status == "active"

    def test_subscription_status_choices(self):
        """Test subscription with different status choices."""
        statuses = ["active", "past_due", "canceled"]
        for status in statuses:
            subscription = SubscriptionFactory(status=status)
            assert subscription.status == status

    def test_subscription_with_period_end(self):
        """Test subscription with current_period_end."""
        future_time = timezone.now() + timezone.timedelta(days=30)
        subscription = SubscriptionFactory(current_period_end=future_time)
        assert subscription.current_period_end is not None

    def test_subscription_without_period_end(self):
        """Test subscription without current_period_end."""
        subscription = SubscriptionFactory(current_period_end=None)
        assert subscription.current_period_end is None

    def test_subscription_has_timestamps(self):
        """Test that subscription has created_at and updated_at."""
        subscription = SubscriptionFactory()
        assert subscription.created_at is not None
        assert subscription.updated_at is not None

    def test_subscription_belongs_to_workspace(self):
        """Test subscription belongs to a workspace."""
        subscription = SubscriptionFactory()
        assert subscription.workspace is not None
        assert subscription in subscription.workspace.subscriptions.all()
