from apps.calls.models import BotInstructionTemplate
from apps.companies.constants import BotInstructionTemplateType


class BotInstruction:

    base_content = {
        'general': "General Bot Messages",
        'advisor_transfer': "If the user wants to reach, speak or talk to {advisor_name} then transfer to {advisor_phone}",
        'bdc_transfer': "If the user wants to schedule a meeting then transfer the call to BDC {bdc_number} and say 'I'll transfer you to our BDC';",
        'advisor_not_found': "If the user asks for an advisor who is not in the list, say 'I'm sorry, we don't have an advisor by that name at our company. Would you like to connect with someone else?'",
        'working_hours': "Working Hours",
        'off_hours': "Cannot transfer calls in {off_hours_or_holidays}. Please record a message for the advisor instead.",
        'holiday': "Holiday",
        'dealership_transfer': "If the user wants to talk, speak, transfer or reach to {dealership_name} then transfer to {dealership_phone}; ",

        # 🔥 Service AI Instructions
        'service_list': "Our company provides the following services: {services}.",
        'service_line': "The base price for the {service_name} service starts at {base_price}. Only if the customer asks about the labor rate for {service_name}, tell them: The labor rate for {service_name} is {labor_rate} per hour.",
        'service_line_no_labor': "{service_name} Service starting at {base_price}",
        'service_no_price': "{service_name} Service is available. Pricing will be provided upon request",
        'service_detail': (
            "When the user asks about a specific service, explain clearly what the service includes, "
            "its purpose, and any important information in simple words. "
            "Use the provided service description and pricing if available."
        ),

        'service_price': (
            "When the user asks about service pricing, provide the starting price first. "
            "If labor rate exists, explain that labor is charged per hour. "
            "Always be polite and transparent about pricing."
        ),
        'service_not_found': "Currently, the company has no service to provide on call. You need to visit us.",
    }

    @staticmethod
    def get_or_create_template(company, bot_name, template_type):
        template = BotInstructionTemplate.objects.filter(
            company=company,
            bot_name=bot_name,
            template_type=template_type,
            is_active=True,
        ).first()

        if template:
            return template

        title = BotInstructionTemplateType(template_type).label
        content = BotInstruction.base_content.get(template_type)

        template = BotInstructionTemplate.objects.create(
            company=company,
            bot_name=bot_name,
            template_type=template_type,
            title=title,
            content=content,
            is_active=True,
        )

        return template
