"""
Mixins for ride views.

This module contains mixins that provide reusable functionality
for ride-related views, reducing code duplication.
"""

from typing import Any

from django.contrib import messages

from apps.rides.services import RideService


class RideFormMixin:
    """
    Mixin for handling ride form submissions with entity suggestions.

    Provides common functionality for RideCreateView and RideUpdateView
    to handle new manufacturer, designer, and ride model suggestions.
    """

    def handle_entity_suggestions(self, form) -> dict[str, Any]:
        """
        Process new entity suggestions from form.

        Creates moderation submissions for any new manufacturers,
        designers, or ride models that were suggested but don't
        exist in the system.

        Args:
            form: Validated form instance with cleaned_data

        Returns:
            Dictionary with submission results from RideService
        """
        result = RideService.handle_new_entity_suggestions(form_data=form.cleaned_data, submitter=self.request.user)

        if result["total_submissions"] > 0:
            messages.info(
                self.request, f"Created {result['total_submissions']} moderation submission(s) " "for new entities"
            )

        return result
