# app/modules/orders/services/customer_order_service.py
"""
Customer-order service for managing customer-order relationships.

This service handles the orders-specific logic for customer order operations.
It follows the principle that:
- Customers module provides generic customer storage (agnostic to consumers)
- Orders module defines how it relates to customers (owns the relationship)

Similar to how:
- CMS provides generic media storage
- Catalog defines ProductMedia association
"""

import logging

from sqlalchemy.orm import Session

from app.modules.orders.models import Order

logger = logging.getLogger(__name__)


class CustomerOrderService:
    """Service for customer-order operations owned by orders module."""

    def get_customer_orders(
        self,
        db: Session,
        vendor_id: int,
        customer_id: int,
        skip: int = 0,
        limit: int = 50,
    ) -> tuple[list[Order], int]:
        """
        Get orders for a specific customer.

        Args:
            db: Database session
            vendor_id: Vendor ID (for ownership verification)
            customer_id: Customer ID
            skip: Pagination offset
            limit: Pagination limit

        Returns:
            Tuple of (orders, total_count)
        """
        query = (
            db.query(Order)
            .filter(
                Order.customer_id == customer_id,
                Order.vendor_id == vendor_id,
            )
            .order_by(Order.created_at.desc())
        )

        total = query.count()
        orders = query.offset(skip).limit(limit).all()

        return orders, total

    def get_recent_orders(
        self,
        db: Session,
        vendor_id: int,
        customer_id: int,
        limit: int = 5,
    ) -> list[Order]:
        """
        Get recent orders for a customer.

        Args:
            db: Database session
            vendor_id: Vendor ID
            customer_id: Customer ID
            limit: Maximum orders to return

        Returns:
            List of recent orders
        """
        return (
            db.query(Order)
            .filter(
                Order.customer_id == customer_id,
                Order.vendor_id == vendor_id,
            )
            .order_by(Order.created_at.desc())
            .limit(limit)
            .all()
        )

    def get_order_count(
        self,
        db: Session,
        vendor_id: int,
        customer_id: int,
    ) -> int:
        """
        Get total order count for a customer.

        Args:
            db: Database session
            vendor_id: Vendor ID
            customer_id: Customer ID

        Returns:
            Total order count
        """
        return (
            db.query(Order)
            .filter(
                Order.customer_id == customer_id,
                Order.vendor_id == vendor_id,
            )
            .count()
        )


# Singleton instance
customer_order_service = CustomerOrderService()

__all__ = ["CustomerOrderService", "customer_order_service"]
