digraph BackendArchitecture {
    // Graph settings
    rankdir=TB
    compound=true
    splines=ortho
    node [shape=box, style="rounded,filled", fontname="Helvetica", fontsize=11]
    edge [fontname="Helvetica", fontsize=9]

    // Color scheme
    // Django Core: #092E20 (dark green)
    // Mascotas: #4A90D9 (blue)
    // Productos: #50C878 (green)
    // Solicitudes: #FF6B6B (coral)
    // Common: #9B59B6 (purple)
    // External: #F39C12 (orange)
    // Payments: #E74C3C (red)

    label="AMAR Mascotas - Backend Architecture (Django)\n\n"
    labelloc="t"
    fontsize=16
    fontname="Helvetica-Bold"

    // Django Core Cluster
    subgraph cluster_django_core {
        label="Django Core"
        style="rounded,filled"
        fillcolor="#E8F5E9"
        color="#2E7D32"

        auth_user [label="auth.User\n(Django Auth)", fillcolor="#C8E6C9"]
        django_admin [label="Django Admin\nInterface", fillcolor="#C8E6C9"]
        drf [label="Django REST\nFramework", fillcolor="#C8E6C9"]
        jwt_auth [label="JWT Authentication\n(SimpleJWT)", fillcolor="#C8E6C9"]
    }

    // Mascotas App Cluster
    subgraph cluster_mascotas {
        label="mascotas (Pets & Veterinarians)"
        style="rounded,filled"
        fillcolor="#E3F2FD"
        color="#1565C0"

        petowner [label="PetOwner\n(Cliente/Tutor)", fillcolor="#BBDEFB"]
        pet [label="Pet\n(Mascota)", fillcolor="#BBDEFB"]
        veterinarian [label="Veterinarian\n(Profesional)", fillcolor="#BBDEFB"]
        vetvisit [label="VetVisit\n(Consulta)", fillcolor="#BBDEFB"]
        vetvisitreport [label="VetVisitReport\n(Informe Clinico)", fillcolor="#BBDEFB"]
        availability [label="Availability /\nUnavailability", fillcolor="#BBDEFB"]
        pet_health [label="PetVaccine /\nPetStudy", fillcolor="#BBDEFB"]
    }

    // Productos App Cluster
    subgraph cluster_productos {
        label="productos (Services & Pricing)"
        style="rounded,filled"
        fillcolor="#E8F5E9"
        color="#2E7D32"

        service [label="Service\n(Servicio)", fillcolor="#C8E6C9"]
        category [label="Category / Group\n(Categorias)", fillcolor="#C8E6C9"]
        prices [label="Prices\n(Precios)", fillcolor="#C8E6C9"]
        discounts [label="Discounts\n(Descuentos)", fillcolor="#C8E6C9"]
        cart [label="Cart / CartItem\n(Carrito)", fillcolor="#C8E6C9"]
        combo [label="ServiceCombo\n(Paquetes)", fillcolor="#C8E6C9"]
    }

    // Solicitudes App Cluster
    subgraph cluster_solicitudes {
        label="solicitudes (Service Requests)"
        style="rounded,filled"
        fillcolor="#FFEBEE"
        color="#C62828"

        servicerequest [label="ServiceRequest\n(Solicitud)", fillcolor="#FFCDD2"]
        statehistory [label="StateHistory\n(Historial)", fillcolor="#FFCDD2"]
        vetasked [label="VeterinarianAsked\n(Vet Consultado)", fillcolor="#FFCDD2"]
        reminders [label="Reminders\n(Recordatorios)", fillcolor="#FFCDD2"]
    }

    // Common App Cluster
    subgraph cluster_common {
        label="common (Shared Models)"
        style="rounded,filled"
        fillcolor="#F3E5F5"
        color="#7B1FA2"

        campaign [label="Campaign\n(Marketing)", fillcolor="#E1BEE7"]
        tag [label="Tag\n(Etiquetas)", fillcolor="#E1BEE7"]
        specialty [label="Specialty\n(Especialidades)", fillcolor="#E1BEE7"]
        medication [label="Medication\n(Medicamentos)", fillcolor="#E1BEE7"]
        breed [label="PetBreed / Vaccine\n/ Study", fillcolor="#E1BEE7"]
        neighborhood [label="Neighborhood /\nProvince / Locality", fillcolor="#E1BEE7"]
        turnfee [label="IndividualTurnFee\nGroup", fillcolor="#E1BEE7"]
    }

    // Payments App Cluster
    subgraph cluster_payments {
        label="payments (Payment Processing)"
        style="rounded,filled"
        fillcolor="#FCE4EC"
        color="#AD1457"

        mercadopago [label="MercadoPago\nAccount", fillcolor="#F8BBD9"]
        mpnotification [label="MP Notification\n(Webhooks)", fillcolor="#F8BBD9"]
    }

    // External Integrations Cluster
    subgraph cluster_external {
        label="External Integrations"
        style="rounded,filled"
        fillcolor="#FFF3E0"
        color="#E65100"

        google_cal [label="Google Calendar\n(Agenda)", fillcolor="#FFE0B2"]
        google_sheets [label="Google Sheets\n(Exports)", fillcolor="#FFE0B2"]
        mercately [label="Mercately\n(WhatsApp)", fillcolor="#FFE0B2"]
        afip [label="AFIP\n(Facturacion)", fillcolor="#FFE0B2"]
        celery [label="Celery\n(Async Tasks)", fillcolor="#FFE0B2"]
    }

    // AFIP Integration
    subgraph cluster_afip {
        label="django_afip (Invoicing)"
        style="rounded,filled"
        fillcolor="#FFFDE7"
        color="#F9A825"

        receipt [label="Receipt\n(Comprobante)", fillcolor="#FFF9C4"]
        taxpayer [label="TaxPayer\n(Contribuyente)", fillcolor="#FFF9C4"]
        pos [label="PointOfSales\n(Punto de Venta)", fillcolor="#FFF9C4"]
    }

    // Relationships - Core
    auth_user -> petowner [label="1:1 optional", color="#666"]
    auth_user -> veterinarian [label="1:1", color="#666"]
    drf -> jwt_auth [style=dashed, color="#999"]

    // Relationships - Mascotas
    petowner -> pet [label="1:N", color="#1565C0"]
    petowner -> servicerequest [label="1:N", color="#1565C0"]
    pet -> vetvisit [label="N:M", color="#1565C0"]
    pet -> pet_health [label="1:N", color="#1565C0"]
    veterinarian -> vetvisit [label="1:N", color="#1565C0"]
    veterinarian -> availability [label="1:N", color="#1565C0"]
    vetvisit -> vetvisitreport [label="1:N", color="#1565C0"]
    vetvisit -> servicerequest [label="1:1", style=dashed, color="#1565C0"]

    // Relationships - Productos
    service -> category [label="N:1", color="#2E7D32"]
    service -> prices [label="1:N", color="#2E7D32"]
    service -> discounts [label="1:N", color="#2E7D32"]
    cart -> petowner [label="N:1", color="#2E7D32"]
    cart -> veterinarian [label="N:1 optional", color="#2E7D32", style=dashed]
    service -> cart [label="via CartItem", color="#2E7D32"]
    combo -> service [label="contains", color="#2E7D32"]

    // Relationships - Solicitudes
    servicerequest -> cart [label="1:1", color="#C62828"]
    servicerequest -> statehistory [label="1:N", color="#C62828"]
    servicerequest -> vetasked [label="1:N", color="#C62828"]
    vetasked -> reminders [label="1:N", color="#C62828"]
    veterinarian -> vetasked [label="N:1", color="#C62828"]

    // Relationships - Common
    petowner -> neighborhood [label="N:1", color="#7B1FA2"]
    veterinarian -> specialty [label="N:M", color="#7B1FA2"]
    veterinarian -> neighborhood [label="N:M coverage", color="#7B1FA2"]
    servicerequest -> campaign [label="N:1 optional", color="#7B1FA2", style=dashed]
    servicerequest -> tag [label="N:M", color="#7B1FA2"]
    vetvisitreport -> medication [label="references", color="#7B1FA2", style=dashed]
    veterinarian -> turnfee [label="N:M", color="#7B1FA2"]

    // Relationships - Payments & External
    servicerequest -> mercadopago [label="payment", color="#AD1457"]
    mpnotification -> servicerequest [label="confirms", color="#AD1457"]
    vetvisit -> google_cal [label="sync", color="#E65100", style=dashed]
    servicerequest -> mercately [label="notify", color="#E65100", style=dashed]
    reminders -> celery [label="scheduled", color="#E65100", style=dashed]

    // AFIP relationships
    vetvisit -> receipt [label="1:1 optional", color="#F9A825", style=dashed]
    receipt -> taxpayer [label="N:1", color="#F9A825"]
    receipt -> pos [label="N:1", color="#F9A825"]
}
