from datetime import datetime, timedelta

import pendulum
import pytz
from django.conf import settings
from django.utils import timezone


# need to translate to a non-naive timezone, even if timezone == settings.TIME_ZONE, so we can compare two dates
def to_user_timezone(date, profile):
    timezone = profile.timezone if profile.timezone else settings.TIME_ZONE
    return date.astimezone(pytz.timezone(timezone))


def to_system_timezone(date):
    return date.astimezone(pytz.timezone(settings.TIME_ZONE))


def now_user_timezone(profile):
    timezone.activate(pytz.timezone(profile.timezone))
    return timezone.localtime(timezone.now())


def start_of_day(dt, profile) -> datetime:
    """Get the start of the day in the profile's timezone"""
    timezone = profile.timezone if profile.timezone else settings.TIME_ZONE
    tzinfo = pytz.timezone(timezone)
    return datetime.combine(dt, datetime.min.time(), tzinfo)


def end_of_day(dt, profile) -> datetime:
    """Get the start of the day in the profile's timezone"""
    timezone = profile.timezone if profile.timezone else settings.TIME_ZONE
    tzinfo = pytz.timezone(timezone)
    return datetime.combine(dt, datetime.max.time(), tzinfo)


def start_of_week(dt, profile) -> datetime:
    # TODO allow profile to set start of week
    return start_of_day(dt, profile) - timedelta(dt.weekday())


def end_of_week(dt, profile) -> datetime:
    # TODO allow profile to set start of week
    return start_of_week(dt, profile) + timedelta(days=6)


def start_of_month(dt, profile) -> datetime:
    return start_of_day(dt, profile).replace(day=1)


def end_of_month(dt, profile) -> datetime:
    next_month = end_of_day(dt, profile).replace(day=28) + timedelta(days=4)
    # subtracting the number of the current day brings us back one month
    return next_month - timedelta(days=next_month.day)


def start_of_year(dt, profile) -> datetime:
    return start_of_day(dt, profile).replace(month=1, day=1)


def fix_profile_historic_timezones(profile):
    home_tz = "America/New_York"

    europe = "2023-10-15 06:00:00"
    europe_end = "2023-12-16 12:00:00"
    europe_tz = "Europe/Paris"

    washington = "2024-04-28 06:00:00"
    washington_end = "2024-05-04 12:00:00"
    washington_tz = "America/Los_Angeles"

    camp = "2024-08-04 17:00:00"
    camp_end = "2024-08-10 12:00:00"
    camp_tz = "America/Halifax"

    summer = "2025-07-09 06:00:00"
    summer_end = "2025-07-11 23:30:00"
    summer_tz = "America/Los_Angeles"

    profile.timezone_change_log = None

    profile.timezone_change_log = ""
    profile.timezone_change_log += f"{europe_tz} - {pendulum.parse(europe)}\n"
    profile.timezone_change_log += (
        f"{home_tz} - {pendulum.parse(europe_end)}\n"
    )
    profile.timezone_change_log += (
        f"{washington_tz} - {pendulum.parse(washington)}\n"
    )
    profile.timezone_change_log += (
        f"{home_tz} - {pendulum.parse(washington_end)}\n"
    )
    profile.timezone_change_log += f"{camp_tz} - {pendulum.parse(camp)}\n"
    profile.timezone_change_log += f"{home_tz} - {pendulum.parse(camp_end)}\n"
    profile.timezone_change_log += f"{summer_tz} - {pendulum.parse(summer)}\n"
    profile.timezone_change_log += f"{home_tz} - {pendulum.parse(summer_end)}"
    profile.save()
