from enum import Enum

import structlog

from vbv_lernwelt.core.base import LoggedCommand
from vbv_lernwelt.notify.email.reminders.assigment import (
    send_assignment_reminder_notifications,
)
from vbv_lernwelt.notify.email.reminders.attendance import (
    send_attendance_reminder_notifications,
)

logger = structlog.get_logger(__name__)


class ReminderType(Enum):
    ASSIGNMENT = "assignment"
    ATTENDANCE = "attendance"


ACTIONS = {
    ReminderType.ASSIGNMENT: send_assignment_reminder_notifications,
    ReminderType.ATTENDANCE: send_attendance_reminder_notifications,
}


class Command(LoggedCommand):
    help = "Sends Email Reminder Notifications"

    def add_arguments(self, parser):
        parser.add_argument(
            "--type",
            choices=[t.value for t in ReminderType] + ["all"],
            required=True,
            help="Type of reminder",
        )

    def handle(self, *args, **options):
        reminder_type = options["type"]

        if reminder_type == "all":
            types = [ReminderType.ASSIGNMENT, ReminderType.ATTENDANCE]
        else:
            types = [ReminderType(reminder_type)]

        results = {t.value: None for t in types}

        for reminder in types:
            logger.info(f"Starting {reminder.name} reminder notification job")
            results[reminder.value] = ACTIONS[reminder]()
            logger.info(f"{reminder.name} reminder notification job finished")

        self.job_log.json_data = results
        self.job_log.save()

        logger.info("Reminder notification job finished")
