# Copyright (c) 2011-2022 Berkeley Model United Nations. All rights reserved.
# Use of this source code is governed by a BSD License (see LICENSE).

import json
import requests

import os

from decimal import Decimal

from django.apps import apps
from django.conf import settings
from django.core.exceptions import ValidationError
from django.core.mail import send_mail
from django.db import models, transaction
from django.db.models.signals import post_init, post_save, pre_delete, pre_save
from django.utils import timezone
# from django.contrib.postgres.fields import DateRangeField

from huxley.core.constants import ContactGender, ContactType, ProgramTypes
from huxley.logging.models import WaiverLog


class Conference(models.Model):
    session = models.PositiveSmallIntegerField(default=0, primary_key=True)
    start_date = models.DateField()
    end_date = models.DateField()
    reg_open = models.DateField()
    round_one_end = models.DateField()
    round_one_fees_due = models.DateField()
    round_two_end = models.DateField()
    round_two_fees_due = models.DateField()
    round_three_end = models.DateField()
    round_three_fees_due = models.DateField()
    reg_close = models.DateField()
    round_four_fees_due = models.DateField()
    part_refund_deadline = models.DateField()
    min_attendance = models.PositiveSmallIntegerField(default=0)
    max_attendance = models.PositiveSmallIntegerField(default=0)
    open_reg = models.BooleanField(default=True)
    waitlist_reg = models.BooleanField(default=False)
    position_papers_accepted = models.BooleanField(default=False)
    notes_enabled = models.BooleanField(default=False)
    polling_interval = models.PositiveIntegerField(default=60000)
    max_refresh_interval = models.PositiveIntegerField(default=10000)
    advisor_edit_deadline = models.DateField()

    note_checkpoint_padding = models.PositiveIntegerField(default=5000)
    early_paper_deadline = models.DateField()
    paper_deadline = models.DateField()
    waiver_avail_date = models.DateField()
    waiver_deadline = models.DateField()
    waiver_link = models.CharField(max_length=300)
    opi_link = models.URLField(default="https://zoom.us")
    external = models.CharField(max_length=128)
    treasurer = models.CharField(max_length=128)
    registration_fee = models.DecimalField(
        max_digits=6, decimal_places=2, default=Decimal('50.00'))
    delegate_fee = models.DecimalField(
        max_digits=6, decimal_places=2, default=Decimal('50.00'))

    @classmethod
    def get_current(cls):
        return Conference.objects.get(session=settings.SESSION)

    def __str__(self):
        return 'BMUN %d' % self.session

    class Meta:
        db_table = u'conference'
        get_latest_by = 'start_date'


class Country(mode
ls.Model):
    name = models.CharField(max_length=128, unique=True)
    special = models.BooleanField(default=False)

    def __str__(self):
        return self.name

    class Meta:
        db_
table = u'country'


class Rubric(models.Model):
    topic_one = models.CharField(max_length=64, default='', blank=True)
    grade_category_1 = models.CharField(max_length=128, default='', blank=True)
    grade_category_2 = models.CharField(max_length=128, default='', blank=True)
    grade_category_3 = models.CharField(max_length=128, default='', blank=True)
    grade_category_4 = models.CharField(max_length=128, default='', blank=True)
    grade_category_5 = models.CharField(max_length=128, default='', blank=True)

    grade_value_1 = models.PositiveSmallIntegerField(default=10)
    grade_value_2 = models.PositiveSmallIntegerField(default=10)
    grade_value_3 = models.PositiveSmallIntegerField(default=10)
    grade_value_4 = models.PositiveSmallIntegerField(default=10)
    grade_value_5 = models.PositiveSmallIntegerField(default=10)

    # Values below this for the second paper topic
    topic_two = models.CharField(max_length=64, default='', blank=True)
    use_topic_2 = models.BooleanField(default=True)
    grade_t2_category_1 = models.CharField(
        max_length=128, default='', blank=True)
    grade_t2_category_2 = models.CharField(
        max_length=128, default='', blank=True)
    grade_t2_category_3 = models.CharField(
        max_length=128, default='', blank=True)
    grade_t2_category_4 = models.CharField(
        max_length=128, default='', blank=True)
    grade_t2_category_5 = models.CharField(
        max_length=128, default='', blank=True)

    grade_t2_value_1 = models.PositiveSmallIntegerField(default=10)
    grade_t2_value_2 = models.PositiveSmallIntegerField(default=10)
    grade_t2_value_3 = models.PositiveSmallIntegerField(default=10)
    grade_t2_value_4 = models.PositiveSmallIntegerField(default=10)
    grade_t2_value_5 = models.PositiveSmallIntegerField(default=10)

    def __str__(self):
        return '%s' % self.committee.name if self.committee else '%d' % self.id

    class Meta:
        db_table = u'rubric'


class Committee(models.Model):
    name = models.CharField(max_length=8)
    full_name = models.CharField(max_length=128)
    countries = models.ManyToManyField(Country, through='Assignment')
    delegation_size = models.PositiveSmallIntegerField(default=2)
    special = models.BooleanField(default=False)
    rubric = models.OneToOneField(
        Rubric, on_delete=models.SET_NULL, blank=True, null=True)
    zoom_link = models.URLField(default="https://zoom.us")
    notes_activated = models.BooleanField(default=False)

    @classmethod
    def create_rubric(cls, **kwargs):
        committee = kwargs['instance']
        if not committee.rubric:
            committee.rubric = Rubric.objects.create()

    def __str__(self):
        return self.name

    class Meta:
        db_table = u'committee'


class CommitteeFeedback(models.Model):
    CHOICES = ((0, 0),
               (1, 1),
               (2, 2),
               (3, 3),
               (4, 4),
               (5, 5),
               (6, 6),
               (7, 7),
               (8, 8),
               (9, 9),
               (10, 10), )

    LIKELY_CHOICES = ((0, 'Blank'), (1, 'No'),
                      (2, 'No change/unsure'), (3, 'Yes'))

    committee = models.ForeignKey(Committee, on_delete=models.CASCADE)
    comment = models.TextField(blank=True, default='')
    rating = models.IntegerField(blank=True, default=0, choices=CHOICES)

    chair_1_name = models.CharField(blank=True, default='', max_length=100)
    chair_2_name = models.CharField(blank=True, default='', max_length=100)
    chair_3_name = models.CharField(blank=True, default='', max_length=100)
    chair_4_name = models.CharField(blank=True, default='', max_length=100)
    chair_5_name = models.CharField(blank=True, default='', max_length=100)
    chair_6_name = models.CharField(blank=True, default='', max_length=100)
    chair_7_name = models.CharField(blank=True, default='', max_length=100)
    chair_8_name = models.CharField(blank=True, default='', max_length=100)
    chair_9_name = models.CharField(blank=True, default='', max_length=100)
    chair_10_name = models.CharField(blank=True, default='', max_length=100)

    chair_1_comment = models.TextField(blank=True, default='')
    chair_2_comment = models.TextField(blank=True, default='')
    chair_3_comment = models.TextField(blank=True, default='')
    chair_4_comment = models.TextField(blank=True, default='')
    chair_5_comment = models.TextField(blank=True, default='')
    chair_6_comment = models.TextField(blank=True, default='')
    chair_7_comment = models.TextField(blank=True, default='')
    chair_8_comment = models.TextField(blank=True, default='')
    chair_9_comment = models.TextField(blank=True, default='')
    chair_10_comment = models.TextField(blank=True, default='')

    chair_1_rating = models.IntegerField(
        blank=True, default=0, choices=CHOICES)
    chair_2_rating = models.IntegerField(
        blank=True, default=0, choices=CHOICES)
    chair_3_rating = models.IntegerField(
        blank=True, default=0, choices=CHOICES)
    chair_4_rating = models.IntegerField(
        blank=True, default=0, choices=CHOICES)
    chair_5_rating = models.IntegerField(
        blank=True, def