"""
CLM Module — Contract Tests.

تست‌های public contracts ماژول CLM.
"""
from datetime import date, datetime
from decimal import Decimal
from uuid import uuid4

from modules.clm.contracts.v1.events import (
    CLMEvent,
    ContractCreated,
    ContractApproved,
    ContractActivated,
    ContractTerminated,
    ContractRenewed,
    ContractAmended,
    MilestoneCompleted,
    ContractExpiryWarning,
)
from modules.clm.contracts.v1.types import (
    ContractSummary,
    ContractCategory,
    ContractStatus,
    ContractType,
)
from modules.clm.contracts.v1.commands import (
    CreateContract,
    ApproveContract,
    RenewContract,
    CreateAmendment,
)
from modules.clm.contracts.v1.queries import (
    GetContract,
    ListContracts,
    ListExpiringContracts,
)


class TestCLMPublicEvents:
    """تست‌های رویدادهای عمومی CLM."""

    def test_contract_created_event(self):
        event = ContractCreated(
            event_id=uuid4(),
            tenant_id=uuid4(),
            occurred_at=datetime.now(),
            contract_id=uuid4(),
            contract_number="CNT-1404-0001",
            title="قرارداد خرید",
            category="COMMERCIAL",
            contract_type="COMMERCIAL_PURCHASE",
            total_value=Decimal("1000000"),
        )
        assert event.event_type == "clm.contract.created"
        assert event.version == 1

    def test_contract_approved_event(self):
        event = ContractApproved(
            event_id=uuid4(),
            tenant_id=uuid4(),
            occurred_at=datetime.now(),
            contract_id=uuid4(),
            contract_number="CNT-1404-0001",
        )
        assert event.event_type == "clm.contract.approved"

    def test_contract_renewed_event(self):
        event = ContractRenewed(
            event_id=uuid4(),
            tenant_id=uuid4(),
            occurred_at=datetime.now(),
            contract_id=uuid4(),
            renewal_number=1,
            old_end_date=date.today(),
            new_end_date=date.today(),
        )
        assert event.event_type == "clm.contract.renewed"
        assert event.renewal_number == 1

    def test_milestone_completed_event(self):
        event = MilestoneCompleted(
            event_id=uuid4(),
            tenant_id=uuid4(),
            occurred_at=datetime.now(),
            contract_id=uuid4(),
            milestone_id=uuid4(),
            milestone_type="PAYMENT",
            amount=Decimal("500000"),
        )
        assert event.event_type == "clm.milestone.completed"


class TestCLMPublicTypes:
    """تست‌های تایپ‌های عمومی CLM."""

    def test_contract_summary(self):
        summary = ContractSummary(
            id=uuid4(),
            contract_number="CNT-1404-0001",
            title="قرارداد خرید",
            category="COMMERCIAL",
            contract_type="COMMERCIAL_PURCHASE",
            status="ACTIVE",
            total_value=Decimal("1000000"),
        )
        assert summary.contract_number == "CNT-1404-0001"

    def test_contract_category_choices(self):
        assert len(ContractCategory.CHOICES) == 4

    def test_contract_status_choices(self):
        assert len(ContractStatus.CHOICES) == 9

    def test_contract_type_choices(self):
        assert len(ContractType.CHOICES) == 13


class TestCLMPublicCommands:
    """تست‌های دستورات عمومی CLM."""

    def test_create_contract_command(self):
        cmd = CreateContract(
            tenant_id=uuid4(),
            title="قرارداد تست",
            category="COMMERCIAL",
            contract_type="COMMERCIAL_PURCHASE",
            start_date=date.today(),
            end_date=date.today(),
            total_value=Decimal("1000000"),
        )
        assert cmd.title == "قرارداد تست"
        assert cmd.priority == "NORMAL"


class TestCLMPublicQueries:
    """تست‌های کوئری‌های عمومی CLM."""

    def test_list_contracts_query(self):
        query = ListContracts(
            tenant_id=uuid4(),
            category="COMMERCIAL",
            status="ACTIVE",
        )
        assert query.page == 1
        assert query.page_size == 20

    def test_list_expiring_query(self):
        query = ListExpiringContracts(
            tenant_id=uuid4(),
            days_ahead=60,
        )
        assert query.days_ahead == 60
