import datetime
import jdatetime


def convert_to_shamsi(year=None, month=None, day=None,datetime=None,date=None,in_value=None):
    if datetime is not None:
        date = jdatetime.datetime.fromgregorian(year=year, month=month, day=day,datetime=datetime).strftime("%Y-%m-%d (%H:%M)")
        if in_value and date is not None:
            if "(" in date:  # اگر زمان وجود دارد
                date_part, time_part = date.split(" (")
                parts = date_part.split("-")
                reversed_date = "-".join(reversed(parts))
                date = f"{reversed_date} ({time_part}"
            else:
                parts = date.split("-")
                reversed_date = "-".join(reversed(parts))
                date = reversed_date
    elif date:
        date = jdatetime.datetime.fromgregorian(date=date).strftime(
            "%Y-%m-%d")
    else:
        date = jdatetime.datetime.fromgregorian(year=int(year), month=int(month), day=int(day), datetime=datetime).strftime(
            "%Y-%m-%d")
    return date


def convert_to_miladi(year=None, month=None, day=None):
    date = jdatetime.datetime(year, month, day).togregorian()
    return date


def get_month_start_end_dates(period_type):
    today = jdatetime.date.today()

    day_of_month = today.day

    # Function to convert Jalali date to Gregorian date
    def to_gregorian(jalali_date):
        return jalali_date.togregorian()

    # Function to get start and end dates for a given Jalali month
    def get_start_end_of_month(jalali_date):
        start_of_month = jdatetime.date(jalali_date.year, jalali_date.month, 1)
        # Calculate the end of the month
        if jalali_date.month == 12:
            end_of_month = jdatetime.date(jalali_date.year + 1, 1, 1) - jdatetime.timedelta(days=1)
        else:
            end_of_month = jdatetime.date(jalali_date.year, jalali_date.month + 1, 1) - jdatetime.timedelta(days=1)
        return to_gregorian(start_of_month), to_gregorian(end_of_month)

    # Helper function to get the previous month in Jalali calendar
    def get_previous_month(jalali_date):
        if jalali_date.month == 1:
            return jdatetime.date(jalali_date.year - 1, 12, jalali_date.day)
        else:
            return jdatetime.date(jalali_date.year, jalali_date.month - 1, jalali_date.day)

    # Determine the months to consider based on the period type and current day
    months_to_consider = 1 if period_type == 'oneMonth' else 3 if period_type == 'threeMonths' else 6 if period_type == 'sixMonths' else 12

    # List to hold the start and end dates
    start_end_dates = []

    if day_of_month >= 30:
        # Include the current month if today is the 30th or 31st
        for _ in range(months_to_consider):
            start, end = get_start_end_of_month(today)
            start_end_dates.append({"start_date": start, "end_date": end})
            today = get_previous_month(today)
    else:
        # Exclude the current month if today is less than the 30th
        today = get_previous_month(today)
        for _ in range(months_to_consider):
            start, end = get_start_end_of_month(today)
            start_end_dates.append({"start_date": start, "end_date": end})
            today = get_previous_month(today)

    return start_end_dates[::-1]  # Reverse to get chronological order


