# app/modules/customers/services/customer_service.py
"""
Customer management service.

Handles customer registration, authentication, and profile management
with complete vendor isolation.
"""

import logging
from datetime import UTC, datetime, timedelta
from typing import Any

from sqlalchemy import and_
from sqlalchemy.orm import Session

from app.modules.customers.exceptions import (
    CustomerNotActiveException,
    CustomerNotFoundException,
    CustomerValidationException,
    DuplicateCustomerEmailException,
    InvalidCustomerCredentialsException,
    InvalidPasswordResetTokenException,
    PasswordTooShortException,
)
from app.modules.tenancy.exceptions import VendorNotActiveException, VendorNotFoundException
from app.modules.core.services.auth_service import AuthService
from app.modules.customers.models import Customer, PasswordResetToken
from app.modules.customers.schemas import CustomerRegister, CustomerUpdate
from app.modules.tenancy.models import Vendor

logger = logging.getLogger(__name__)


class CustomerService:
    """Service for managing vendor-scoped customers."""

    def __init__(self):
        self.auth_service = AuthService()

    def register_customer(
        self, db: Session, vendor_id: int, customer_data: CustomerRegister
    ) -> Customer:
        """
        Register a new customer for a specific vendor.

        Args:
            db: Database session
            vendor_id: Vendor ID
            customer_data: Customer registration data

        Returns:
            Customer: Created customer object

        Raises:
            VendorNotFoundException: If vendor doesn't exist
            VendorNotActiveException: If vendor is not active
            DuplicateCustomerEmailException: If email already exists for this vendor
            CustomerValidationException: If customer data is invalid
        """
        # Verify vendor exists and is active
        vendor = db.query(Vendor).filter(Vendor.id == vendor_id).first()
        if not vendor:
            raise VendorNotFoundException(str(vendor_id), identifier_type="id")

        if not vendor.is_active:
            raise VendorNotActiveException(vendor.vendor_code)

        # Check if email already exists for this vendor
        existing_customer = (
            db.query(Customer)
            .filter(
                and_(
                    Customer.vendor_id == vendor_id,
                    Customer.email == customer_data.email.lower(),
                )
            )
            .first()
        )

        if existing_customer:
            raise DuplicateCustomerEmailException(
                customer_data.email, vendor.vendor_code
            )

        # Generate unique customer number for this vendor
        customer_number = self._generate_customer_number(
            db, vendor_id, vendor.vendor_code
        )

        # Hash password
        hashed_password = self.auth_service.hash_password(customer_data.password)

        # Create customer
        customer = Customer(
            vendor_id=vendor_id,
            email=customer_data.email.lower(),
            hashed_password=hashed_password,
            first_name=customer_data.first_name,
            last_name=customer_data.last_name,
            phone=customer_data.phone,
            customer_number=customer_number,
            marketing_consent=(
                customer_data.marketing_consent
                if hasattr(customer_data, "marketing_consent")
                else False
            ),
            is_active=True,
        )

        try:
            db.add(customer)
            db.flush()
            db.refresh(customer)

            logger.info(
                f"Customer registered successfully: {customer.email} "
                f"(ID: {customer.id}, Number: {customer.customer_number}) "
                f"for vendor {vendor.vendor_code}"
            )

            return customer

        except Exception as e:
            logger.error(f"Error registering customer: {str(e)}")
            raise CustomerValidationException(
                message="Failed to register customer", details={"error": str(e)}
            )

    def login_customer(
        self, db: Session, vendor_id: int, credentials
    ) -> dict[str, Any]:
        """
        Authenticate customer and generate JWT token.

        Args:
            db: Database session
            vendor_id: Vendor ID
            credentials: Login credentials (UserLogin schema)

        Returns:
            Dict containing customer and token data

        Raises:
            VendorNotFoundException: If vendor doesn't exist
            InvalidCustomerCredentialsException: If credentials are invalid
            CustomerNotActiveException: If customer account is inactive
        """
        # Verify vendor exists
        vendor = db.query(Vendor).filter(Vendor.id == vendor_id).first()
        if not vendor:
            raise VendorNotFoundException(str(vendor_id), identifier_type="id")

        # Find customer by email (vendor-scoped)
        customer = (
            db.query(Customer)
            .filter(
                and_(
                    Customer.vendor_id == vendor_id,
                    Customer.email == credentials.email_or_username.lower(),
                )
            )
            .first()
        )

        if not customer:
            raise InvalidCustomerCredentialsException()

        # Verify password using auth_manager directly
        if not self.auth_service.auth_manager.verify_password(
            credentials.password, customer.hashed_password
        ):
            raise InvalidCustomerCredentialsException()

        # Check if customer is active
        if not customer.is_active:
            raise CustomerNotActiveException(customer.email)

        # Generate JWT token with customer context
        from jose import jwt

        auth_manager = self.auth_service.auth_manager
        expires_delta = timedelta(minutes=auth_manager.token_expire_minutes)
        expire = datetime.now(UTC) + expires_delta

        payload = {
            "sub": str(customer.id),
            "email": customer.email,
            "vendor_id": vendor_id,
            "type": "customer",
            "exp": expire,
            "iat": datetime.now(UTC),
        }

        token = jwt.encode(
            payload, auth_manager.secret_key, algorithm=auth_manager.algorithm
        )

        token_data = {
            "access_token": token,
            "token_type": "bearer",
            "expires_in": auth_manager.token_expire_minutes * 60,
        }

        logger.info(
            f"Customer login successful: {customer.email} "
            f"for vendor {vendor.vendor_code}"
        )

        return {"customer": customer, "token_data": token_data}

    def get_customer(self, db: Session, vendor_id: int, customer_id: int) -> Customer:
        """
        Get customer by ID with vendor isolation.

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

        Returns:
            Customer: Customer object

        Raises:
            CustomerNotFoundException: If customer not found
        """
        customer = (
            db.query(Customer)
            .filter(and_(Customer.id == customer_id, Customer.vendor_id == vendor_id))
            .first()
        )

        if not customer:
            raise CustomerNotFoundException(str(customer_id))

        return customer

    def get_customer_by_email(
        self, db: Session, vendor_id: int, email: str
    ) -> Customer | None:
        """
        Get customer by email (vendor-scoped).

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

        Returns:
            Optional[Customer]: Customer object or None
        """
        return (
            db.query(Customer)
            .filter(
                and_(Customer.vendor_id == vendor_id, Customer.email == email.lower())
            )
            .first()
        )

    def get_vendor_customers(
        self,
        db: Session,
        vendor_id: int,
        skip: int = 0,
        limit: int = 100,
        search: str | None = None,
        is_active: bool | None = None,
    ) -> tuple[list[Customer], int]:
        """
        Get all customers for a vendor with filtering and pagination.

        Args:
            db: Database session
            vendor_id: Vendor ID
            skip: Pagination offset
            limit: Pagination limit
            search: Search in name/email
            is_active: Filter by active status

        Returns:
            Tuple of (customers, total_count)
        """
        from sqlalchemy import or_

        query = db.query(Customer).filter(Customer.vendor_id == vendor_id)

        if search:
            search_pattern = f"%{search}%"
            query = query.filter(
                or_(
                    Customer.email.ilike(search_pattern),
                    Customer.first_name.ilike(search_pattern),
                    Customer.last_name.ilike(search_pattern),
                    Customer.customer_number.ilike(search_pattern),
                )
            )

        if is_active is not None:
            query = query.filter(Customer.is_active == is_active)

        # Order by most recent first
        query = query.order_by(Customer.created_at.desc())

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

        return customers, total

    # Note: Customer order methods have been moved to the orders module.
    # Use orders.services.customer_order_service for:
    #   - get_customer_orders()
    # Use orders.services.order_metrics.get_customer_order_metrics() for:
    #   - customer order statistics

    def toggle_customer_status(
        self, db: Session, vendor_id: int, customer_id: int
    ) -> Customer:
        """
        Toggle customer active status.

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

        Returns:
            Customer: Updated customer
        """
        customer = self.get_customer(db, vendor_id, customer_id)
        customer.is_active = not customer.is_active

        db.flush()
        db.refresh(customer)

        action = "activated" if customer.is_active else "deactivated"
        logger.info(f"Customer {action}: {customer.email} (ID: {customer.id})")

        return customer

    def update_customer(
        self,
        db: Session,
        vendor_id: int,
        customer_id: int,
        customer_data: CustomerUpdate,
    ) -> Customer:
        """
        Update customer profile.

        Args:
            db: Database session
            vendor_id: Vendor ID
            customer_id: Customer ID
            customer_data: Updated customer data

        Returns:
            Customer: Updated customer object

        Raises:
            CustomerNotFoundException: If customer not found
            CustomerValidationException: If update data is invalid
        """
        customer = self.get_customer(db, vendor_id, customer_id)

        # Update fields
        update_data = customer_data.model_dump(exclude_unset=True)

        for field, value in update_data.items():
            if field == "email" and value:
                # Check if new email already exists for this vendor
                existing = (
                    db.query(Customer)
                    .filter(
                        and_(
                            Customer.vendor_id == vendor_id,
                            Customer.email == value.lower(),
                            Customer.id != customer_id,
                        )
                    )
                    .first()
                )

                if existing:
                    raise DuplicateCustomerEmailException(value, "vendor")

                setattr(customer, field, value.lower())
            elif hasattr(customer, field):
                setattr(customer, field, value)

        try:
            db.flush()
            db.refresh(customer)

            logger.info(f"Customer updated: {customer.email} (ID: {customer.id})")

            return customer

        except Exception as e:
            logger.error(f"Error updating customer: {str(e)}")
            raise CustomerValidationException(
                message="Failed to update customer", details={"error": str(e)}
            )

    def deactivate_customer(
        self, db: Session, vendor_id: int, customer_id: int
    ) -> Customer:
        """
        Deactivate customer account.

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

        Returns:
            Customer: Deactivated customer object

        Raises:
            CustomerNotFoundException: If customer not found
        """
        customer = self.get_customer(db, vendor_id, customer_id)
        customer.is_active = False

        db.flush()
        db.refresh(customer)

        logger.info(f"Customer deactivated: {customer.email} (ID: {customer.id})")

        return customer

    def update_customer_stats(
        self, db: Session, customer_id: int, order_total: float
    ) -> None:
        """
        Update customer statistics after order.

        Args:
            db: Database session
            customer_id: Customer ID
            order_total: Order total amount
        """
        customer = db.query(Customer).filter(Customer.id == customer_id).first()

        if customer:
            customer.total_orders += 1
            customer.total_spent += order_total
            customer.last_order_date = datetime.utcnow()

            logger.debug(f"Updated stats for customer {customer.email}")

    def _generate_customer_number(
        self, db: Session, vendor_id: int, vendor_code: str
    ) -> str:
        """
        Generate unique customer number for vendor.

        Format: {VENDOR_CODE}-CUST-{SEQUENCE}
        Example: VENDORA-CUST-00001

        Args:
            db: Database session
            vendor_id: Vendor ID
            vendor_code: Vendor code

        Returns:
            str: Unique customer number
        """
        # Get count of customers for this vendor
        count = db.query(Customer).filter(Customer.vendor_id == vendor_id).count()

        # Generate number with padding
        sequence = str(count + 1).zfill(5)
        customer_number = f"{vendor_code.upper()}-CUST-{sequence}"

        # Ensure uniqueness (in case of deletions)
        while (
            db.query(Customer)
            .filter(
                and_(
                    Customer.vendor_id == vendor_id,
                    Customer.customer_number == customer_number,
                )
            )
            .first()
        ):
            count += 1
            sequence = str(count + 1).zfill(5)
            customer_number = f"{vendor_code.upper()}-CUST-{sequence}"

        return customer_number

    def get_customer_for_password_reset(
        self, db: Session, vendor_id: int, email: str
    ) -> Customer | None:
        """
        Get active customer by email for password reset.

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

        Returns:
            Customer if found and active, None otherwise
        """
        return (
            db.query(Customer)
            .filter(
                Customer.vendor_id == vendor_id,
                Customer.email == email.lower(),
                Customer.is_active == True,  # noqa: E712
            )
            .first()
        )

    def validate_and_reset_password(
        self,
        db: Session,
        vendor_id: int,
        reset_token: str,
        new_password: str,
    ) -> Customer:
        """
        Validate reset token and update customer password.

        Args:
            db: Database session
            vendor_id: Vendor ID
            reset_token: Password reset token from email
            new_password: New password

        Returns:
            Customer: Updated customer

        Raises:
            PasswordTooShortException: If password too short
            InvalidPasswordResetTokenException: If token invalid/expired
            CustomerNotActiveException: If customer not active
        """
        # Validate password length
        if len(new_password) < 8:
            raise PasswordTooShortException(min_length=8)

        # Find valid token
        token_record = PasswordResetToken.find_valid_token(db, reset_token)

        if not token_record:
            raise InvalidPasswordResetTokenException()

        # Get the customer and verify they belong to this vendor
        customer = (
            db.query(Customer)
            .filter(Customer.id == token_record.customer_id)
            .first()
        )

        if not customer or customer.vendor_id != vendor_id:
            raise InvalidPasswordResetTokenException()

        if not customer.is_active:
            raise CustomerNotActiveException(customer.email)

        # Hash the new password and update customer
        hashed_password = self.auth_service.hash_password(new_password)
        customer.hashed_password = hashed_password

        # Mark token as used
        token_record.mark_used(db)

        logger.info(f"Password reset completed for customer {customer.id}")

        return customer


# Singleton instance
customer_service = CustomerService()
