""" Email template management and context builders. """ from typing import Dict, Any, Optional from django.conf import settings try: from .dto.email_dto import EmailTemplate except ImportError: from recruitment.dto.email_dto import EmailTemplate class EmailTemplates: """Centralized email template management.""" @staticmethod def get_base_context() -> Dict[str, Any]: """Get base context for all email templates.""" return { "logo_url": getattr(settings, "MEDIA_URL", "/static/") + "images/kaauh-logo.png", "company_name": getattr(settings, "COMPANY_NAME", "KAAUH"), "site_url": getattr(settings, "SITE_URL", "https://kaauh.edu.sa"), "support_email": getattr(settings, "SUPPORT_EMAIL", "support@kaauh.edu.sa"), } @staticmethod def build_interview_context(candidate, job, meeting_details=None) -> Dict[str, Any]: """Build context for interview invitation emails.""" base_context = EmailTemplates.get_base_context() context = { "candidate_name": candidate.full_name or candidate.name, "candidate_email": candidate.email, "candidate_phone": getattr(candidate, "phone", ""), "job_title": job.title, "department": getattr(job, "department", ""), "company_name": getattr(job, "company", {}).get( "name", base_context["company_name"] ), } if meeting_details: context.update( { "meeting_topic": meeting_details.get( "topic", f"Interview for {job.title}" ), "meeting_date_time": meeting_details.get("date_time", ""), "meeting_duration": meeting_details.get("duration", "60 minutes"), "join_url": meeting_details.get("join_url", ""), "meeting_id": meeting_details.get("meeting_id", ""), } ) return {**base_context, **context} @staticmethod def build_job_reminder_context( job, application_count, reminder_type="1_day" ) -> Dict[str, Any]: """Build context for job deadline reminder emails.""" base_context = EmailTemplates.get_base_context() urgency_level = { "1_day": "tomorrow", "15_min": "in 15 minutes", "closed": "has closed", }.get(reminder_type, "soon") context = { "job_title": job.title, "job_id": job.pk, "application_deadline": job.application_deadline, "application_count": application_count, "job_status": job.get_status_display(), "urgency_level": urgency_level, "reminder_type": reminder_type, } return {**base_context, **context} @staticmethod def build_agency_welcome_context(agency, access_link=None) -> Dict[str, Any]: """Build context for agency welcome emails.""" base_context = EmailTemplates.get_base_context() context = { "agency_name": agency.name, "agency_email": agency.email, "access_link": access_link, "portal_url": getattr( settings, "AGENCY_PORTAL_URL", "https://kaauh.edu.sa/portal/" ), } return {**base_context, **context} @staticmethod def build_assignment_context(assignment, message_type="created") -> Dict[str, Any]: """Build context for assignment notification emails.""" base_context = EmailTemplates.get_base_context() context = { "assignment": assignment, "agency": assignment.agency, "job": assignment.job, "message_type": message_type, "portal_url": getattr( settings, "AGENCY_PORTAL_URL", "https://kaauh.edu.sa/portal/" ), } return {**base_context, **context} @staticmethod def build_bulk_email_context(recipient_data, base_message) -> Dict[str, Any]: """Build context for bulk emails with personalization.""" base_context = EmailTemplates.get_base_context() context = { "user_name": recipient_data.get( "name", recipient_data.get("email", "Valued User") ), "user_email": recipient_data.get("email"), "email_message": base_message, "personalization": recipient_data.get("personalization", {}), } # Merge any additional context data for key, value in recipient_data.items(): if key not in ["name", "email", "personalization"]: context[key] = value return {**base_context, **context} @staticmethod def get_template_path(template_type: EmailTemplate) -> str: """Get template path for given template type.""" return template_type.value @staticmethod def get_subject_line(template_type: EmailTemplate, context: Dict[str, Any]) -> str: """Generate subject line based on template type and context.""" subjects = { EmailTemplate.INTERVIEW_INVITATION: f"Interview Invitation: {context.get('job_title', 'Position')}", EmailTemplate.INTERVIEW_INVITATION_ALT: f"Interview Confirmation: {context.get('job_title', 'Position')}", EmailTemplate.AGENCY_WELCOME: f"Welcome to {context.get('company_name', 'KAAUH')} Recruitment Portal", EmailTemplate.ASSIGNMENT_NOTIFICATION: f"Assignment {context.get('message_type', 'Notification')}: {context.get('job_title', 'Position')}", EmailTemplate.JOB_REMINDER: f"Job Reminder: {context.get('job_title', 'Position')}", EmailTemplate.REJECTION_SCREENING: f"Application Update: {context.get('job_title', 'Position')}", } return subjects.get( template_type, context.get("subject", "Notification from KAAUH") or "Notification from KAAUH", )