"""
Unit tests for Contracts models.

Tests model methods, properties, and validation logic.
"""
from unittest.mock import Mock, patch
import pytest


class TestContractTemplateModel:
    """Test ContractTemplate model methods."""

    def test_str_method(self):
        """Should return name with version."""
        from smoothschedule.scheduling.contracts.models import ContractTemplate

        template = ContractTemplate()
        template.name = 'Terms of Service'
        template.version = '2.1'

        result = str(template)

        assert result == 'Terms of Service (v2.1)'


class TestServiceContractRequirementModel:
    """Test ServiceContractRequirement model methods."""

    def test_str_method(self):
        """Should return service name requires template name."""
        from smoothschedule.scheduling.contracts.models import ServiceContractRequirement

        requirement = ServiceContractRequirement()

        mock_service = Mock()
        mock_service.name = 'Haircut'
        requirement._state.fields_cache['service'] = mock_service

        mock_template = Mock()
        mock_template.name = 'Waiver'
        requirement._state.fields_cache['template'] = mock_template

        result = str(requirement)

        assert result == 'Haircut requires Waiver'


class TestContractModel:
    """Test Contract model methods."""

    def test_str_method(self):
        """Should return title, customer email, and status."""
        from smoothschedule.scheduling.contracts.models import Contract

        contract = Contract()
        contract.title = 'Service Agreement'
        contract.status = Contract.Status.PENDING

        mock_customer = Mock()
        mock_customer.email = 'customer@example.com'
        contract._state.fields_cache['customer'] = mock_customer

        result = str(contract)

        assert result == 'Service Agreement - customer@example.com (PENDING)'

    def test_get_signing_url_with_frontend_url_setting(self):
        """Should use FRONTEND_URL setting for signing URL."""
        from smoothschedule.scheduling.contracts.models import Contract

        contract = Contract()
        contract.signing_token = 'test_token_123'

        with patch('django.conf.settings', spec=['FRONTEND_URL']) as mock_settings:
            mock_settings.FRONTEND_URL = 'https://app.example.com'
            result = contract.get_signing_url()

        assert result == 'https://app.example.com/sign/test_token_123'

    def test_get_signing_url_with_request_fallback(self):
        """Should use request host when FRONTEND_URL not set."""
        from smoothschedule.scheduling.contracts.models import Contract

        contract = Contract()
        contract.signing_token = 'test_token_456'

        mock_request = Mock()
        mock_request.scheme = 'https'
        mock_request.get_host.return_value = 'fallback.example.com'

        with patch('django.conf.settings', spec=[]) as mock_settings:
            # No FRONTEND_URL attr
            result = contract.get_signing_url(request=mock_request)

        assert result == 'https://fallback.example.com/sign/test_token_456'


class TestContractSignatureModel:
    """Test ContractSignature model methods."""

    def test_str_method(self):
        """Should return signer name and signed time."""
        from smoothschedule.scheduling.contracts.models import ContractSignature
        from datetime import datetime

        signature = ContractSignature()
        signature.signer_name = 'John Doe'
        signature.signed_at = datetime(2024, 6, 15, 10, 30, 0)

        # Bypass FK descriptor
        mock_contract = Mock()
        signature._state.fields_cache['contract'] = mock_contract

        result = str(signature)

        assert 'Signature by John Doe' in result
        assert '2024-06-15' in result
