#!/usr/bin/env python3
"""
Set up sales qualification quality parameters for Windlass (BID 1713).
"""
from config import Config
from quality_parameters_handler import QualityParametersHandler

BID = '1713'


class ConfigWrapper:
    def __init__(self, config):
        self._config = config
    def get(self, key, default=None):
        return getattr(self._config, key, default)
    def __getattr__(self, key):
        return getattr(self._config, key)


QUALITY_PARAMETERS = [
    {
        'parameter_group': 'Opening & Rapport',
        'parameter_name': 'Professional greeting and introduction',
        'parameter_type': 'Required',
        'check_description': 'Did the agent greet, introduce themselves, and confirm the customer?',
        'detailed_description': 'Agent opens politely, states name/company, and establishes who they are speaking with.',
        'max_score': 8,
        'sample_utterances': 'Hello, this is [Name] from [Company]. Am I speaking with [Customer]?',
        'auto_detect_na': False
    },
    {
        'parameter_group': 'Opening & Rapport',
        'parameter_name': 'Call purpose and agenda clarity',
        'parameter_type': 'Required',
        'check_description': 'Did the agent explain the purpose and set a clear agenda?',
        'detailed_description': 'Agent frames the call and sets expectations before starting discovery.',
        'max_score': 6,
        'sample_utterances': 'I wanted to understand your requirements and see if we are a fit.',
        'auto_detect_na': False
    },
    {
        'parameter_group': 'Discovery & Qualification',
        'parameter_name': 'Need discovery and pain points',
        'parameter_type': 'Required',
        'check_description': 'Did the agent explore the customer needs, use case, and pain points?',
        'detailed_description': 'Agent asks open questions to uncover context, challenges, and desired outcomes.',
        'max_score': 14,
        'sample_utterances': 'Can you walk me through your current process and the main challenges?',
        'auto_detect_na': False
    },
    {
        'parameter_group': 'Discovery & Qualification',
        'parameter_name': 'Budget discovery',
        'parameter_type': 'Required',
        'check_description': 'Did the agent ask about budget or price expectations?',
        'detailed_description': 'Agent probes for budget range or buying constraints to qualify fit.',
        'max_score': 10,
        'sample_utterances': 'Do you have a budget range in mind for this project?',
        'auto_detect_na': False
    },
    {
        'parameter_group': 'Discovery & Qualification',
        'parameter_name': 'Authority identification',
        'parameter_type': 'Required',
        'check_description': 'Did the agent identify decision makers or approval process?',
        'detailed_description': 'Agent confirms who is involved in the decision and the approval path.',
        'max_score': 10,
        'sample_utterances': 'Who else will be involved in reviewing this decision?',
        'auto_detect_na': False
    },
    {
        'parameter_group': 'Discovery & Qualification',
        'parameter_name': 'Timeline and urgency',
        'parameter_type': 'Required',
        'check_description': 'Did the agent ask about timeline or urgency?',
        'detailed_description': 'Agent confirms when the customer wants to decide or go live.',
        'max_score': 8,
        'sample_utterances': 'What timeline are you targeting for implementation or purchase?',
        'auto_detect_na': False
    },
    {
        'parameter_group': 'Solution & Value',
        'parameter_name': 'Solution alignment to need',
        'parameter_type': 'Required',
        'check_description': 'Did the agent align the solution to the stated needs?',
        'detailed_description': 'Agent connects benefits/features to the customer context with relevant examples.',
        'max_score': 12,
        'sample_utterances': 'Based on what you shared, our solution helps by...',
        'auto_detect_na': False
    },
    {
        'parameter_group': 'Solution & Value',
        'parameter_name': 'Objection handling',
        'parameter_type': 'Conditional',
        'check_description': 'Did the agent address objections or concerns constructively?',
        'detailed_description': 'Agent acknowledges concerns and provides clear responses or options.',
        'max_score': 8,
        'sample_utterances': 'I understand the concern. Here is how we can address it...',
        'auto_detect_na': True
    },
    {
        'parameter_group': 'Communication',
        'parameter_name': 'Active listening and empathy',
        'parameter_type': 'Required',
        'check_description': 'Did the agent demonstrate active listening and empathy?',
        'detailed_description': 'Agent confirms understanding, paraphrases, and responds thoughtfully.',
        'max_score': 8,
        'sample_utterances': 'Let me summarize to make sure I understand...',
        'auto_detect_na': False
    },
    {
        'parameter_group': 'Communication',
        'parameter_name': 'Clear communication and confidence',
        'parameter_type': 'Required',
        'check_description': 'Was the information clear, structured, and confident?',
        'detailed_description': 'Agent speaks clearly, avoids jargon, and explains next steps simply.',
        'max_score': 6,
        'sample_utterances': 'To keep it simple, there are two main options...',
        'auto_detect_na': False
    },
    {
        'parameter_group': 'Closing & Next Steps',
        'parameter_name': 'Next steps and follow-up commitment',
        'parameter_type': 'Required',
        'check_description': 'Did the agent agree on next steps and confirm follow-up?',
        'detailed_description': 'Agent proposes a clear next action and confirms timing or ownership.',
        'max_score': 10,
        'sample_utterances': 'I will send the proposal and schedule a demo for Tuesday.',
        'auto_detect_na': False
    }
]


def main():
    config = Config()
    config_wrapped = ConfigWrapper(config)
    handler = QualityParametersHandler(config_wrapped)

    with handler.get_connection() as conn:
        cursor = conn.cursor()
        cursor.execute('DELETE FROM quality_parameters WHERE bid = %s', (BID,))
        conn.commit()

    total_score = 0
    for parameter in QUALITY_PARAMETERS:
        handler.save_parameter(BID, parameter)
        total_score += parameter['max_score']

    print(f"Inserted {len(QUALITY_PARAMETERS)} quality parameters for BID {BID}")
    print(f"Total possible score: {total_score}")


if __name__ == '__main__':
    main()
