import pytest

from src.domain.entities import (
    PharmacyEntity,
    PharmacyStatus,
    MedicationEntity,
    ERPType,
    SyncStrategy,
    UserRole,
    ConnectorEntity,
    ConnectorStatus,
    ReservationEntity,
    ReservationStatus,
    AuditLogEntity,
)
from uuid import uuid4


class TestPharmacyEntity:
    def test_create_pharmacy(self):
        entity = PharmacyEntity(
            code="PH-001",
            name="Farmacia Central",
            address="Calle Mayor 1",
            city="Madrid",
            status=PharmacyStatus.ACTIVE,
            erp_type=ERPType.FARMATIC,
            sync_strategy=SyncStrategy.INCREMENTAL,
        )
        assert entity.code == "PH-001"
        assert entity.name == "Farmacia Central"
        assert entity.status == PharmacyStatus.ACTIVE
        assert entity.erp_type == ERPType.FARMATIC
        assert entity.sync_strategy == SyncStrategy.INCREMENTAL

    def test_pharmacy_default_values(self):
        entity = PharmacyEntity(code="PH-002", name="Test")
        assert entity.country == "ES"
        assert entity.status == PharmacyStatus.PENDING
        assert entity.sync_strategy == SyncStrategy.INCREMENTAL
        assert entity.sync_interval_minutes == 30

    def test_pharmacy_status_enum(self):
        assert PharmacyStatus.ACTIVE.value == "active"
        assert PharmacyStatus.INACTIVE.value == "inactive"
        assert PharmacyStatus.SUSPENDED.value == "suspended"
        assert PharmacyStatus.PENDING.value == "pending"


class TestMedicationEntity:
    def test_create_medication(self):
        entity = MedicationEntity(
            nregistro="12345",
            name="Ibuprofeno 400mg",
            active_ingredient="Ibuprofeno",
            dosage="400mg",
            form="Comprimidos",
            prescription_required=False,
        )
        assert entity.nregistro == "12345"
        assert entity.name == "Ibuprofeno 400mg"
        assert entity.prescription_required is False


class TestConnectorEntity:
    def test_create_connector(self):
        entity = ConnectorEntity(
            erp_system_id=uuid4(),
            pharmacy_id=uuid4(),
            name="Farmatic Connector",
            connector_type=ERPType.FARMATIC,
        )
        assert entity.connector_type == ERPType.FARMATIC
        assert entity.version == "1.0.0"
        assert entity.status == ConnectorStatus.PENDING


class TestReservationEntity:
    def test_create_reservation(self):
        entity = ReservationEntity(
            pharmacy_id=uuid4(),
            medication_id=uuid4(),
            quantity=5,
            idempotency_key="unique-key-123",
        )
        assert entity.quantity == 5
        assert entity.status == ReservationStatus.PENDING
        assert entity.idempotency_key == "unique-key-123"


class TestAuditLogEntity:
    def test_create_audit_log(self):
        entity = AuditLogEntity(
            actor_id=uuid4(),
            actor_type="technical_user",
            action="pharmacy.create",
            resource_type="pharmacy",
            resource_id="PH-001",
            ip_address="192.168.1.1",
        )
        assert entity.action == "pharmacy.create"
        assert entity.resource_type == "pharmacy"

    def test_enums(self):
        assert ERPType.FARMATIC.value == "farmatic"
        assert ERPType.NIXFARMA.value == "nixfarma"
        assert ERPType.UNYCOP.value == "unycop"
        assert ERPType.MOCK.value == "mock"
        assert SyncStrategy.FULL.value == "full"
        assert SyncStrategy.HYBRID.value == "hybrid"
        assert UserRole.SUPERADMIN.value == "superadmin"
