"""
Unit tests for Payments models.

Tests model methods, properties, and validation logic without database access.
"""
from unittest.mock import Mock, patch
from decimal import Decimal
import pytest


class TestTransactionLinkModel:
    """Test TransactionLink model methods."""

    def test_str_method(self):
        """Should return payment intent ID, status, and amount."""
        from smoothschedule.commerce.payments.models import TransactionLink

        transaction = TransactionLink()
        transaction.payment_intent_id = 'pi_test_123'
        transaction.status = TransactionLink.Status.SUCCEEDED
        transaction.amount = Decimal('99.99')

        result = str(transaction)

        assert 'pi_test_123' in result
        assert 'Succeeded' in result
        assert '99.99' in result

    def test_str_method_pending_status(self):
        """Should show Pending status."""
        from smoothschedule.commerce.payments.models import TransactionLink

        transaction = TransactionLink()
        transaction.payment_intent_id = 'pi_test_456'
        transaction.status = TransactionLink.Status.PENDING
        transaction.amount = Decimal('50.00')

        result = str(transaction)

        assert 'Pending' in result

    def test_str_method_failed_status(self):
        """Should show Failed status."""
        from smoothschedule.commerce.payments.models import TransactionLink

        transaction = TransactionLink()
        transaction.payment_intent_id = 'pi_test_789'
        transaction.status = TransactionLink.Status.FAILED
        transaction.amount = Decimal('25.00')

        result = str(transaction)

        assert 'Failed' in result

    def test_platform_revenue_property(self):
        """Should return application fee amount."""
        from smoothschedule.commerce.payments.models import TransactionLink

        transaction = TransactionLink()
        transaction.application_fee_amount = Decimal('4.99')

        result = transaction.platform_revenue

        assert result == Decimal('4.99')

    def test_tenant_revenue_property(self):
        """Should return amount minus application fee."""
        from smoothschedule.commerce.payments.models import TransactionLink

        transaction = TransactionLink()
        transaction.amount = Decimal('100.00')
        transaction.application_fee_amount = Decimal('5.00')

        result = transaction.tenant_revenue

        assert result == Decimal('95.00')

    def test_tenant_revenue_property_with_zero_fee(self):
        """Should return full amount when no fee."""
        from smoothschedule.commerce.payments.models import TransactionLink

        transaction = TransactionLink()
        transaction.amount = Decimal('50.00')
        transaction.application_fee_amount = Decimal('0.00')

        result = transaction.tenant_revenue

        assert result == Decimal('50.00')


class TestTransactionLinkStatus:
    """Test TransactionLink status choices."""

    def test_status_choices_exist(self):
        """Should have all status choices."""
        from smoothschedule.commerce.payments.models import TransactionLink

        assert TransactionLink.Status.PENDING == 'PENDING'
        assert TransactionLink.Status.PROCESSING == 'PROCESSING'
        assert TransactionLink.Status.SUCCEEDED == 'SUCCEEDED'
        assert TransactionLink.Status.FAILED == 'FAILED'
        assert TransactionLink.Status.REFUNDED == 'REFUNDED'
        assert TransactionLink.Status.CANCELED == 'CANCELED'


class TestTransactionLinkMeta:
    """Test TransactionLink Meta options."""

    def test_app_label(self):
        """Should have correct app label."""
        from smoothschedule.commerce.payments.models import TransactionLink

        assert TransactionLink._meta.app_label == 'payments'

    def test_ordering(self):
        """Should order by -created_at."""
        from smoothschedule.commerce.payments.models import TransactionLink

        assert TransactionLink._meta.ordering == ['-created_at']
