package util

import (
	"time"
)

const DateFormatDDMMYYYY = "02-01-2006"

// ParseDateToJakartaTime parses a date string in DD-MM-YYYY format and converts it to Jakarta timezone
// Returns start of day (00:00:00) in Jakarta timezone
func ParseDateToJakartaTime(dateStr string) (*time.Time, error) {
	if dateStr == "" {
		return nil, nil
	}

	date, err := time.Parse(DateFormatDDMMYYYY, dateStr)
	if err != nil {
		return nil, err
	}

	jakartaLoc, err := time.LoadLocation("Asia/Jakarta")
	if err != nil {
		return nil, err
	}

	jakartaTime := time.Date(date.Year(), date.Month(), date.Day(), 0, 0, 0, 0, jakartaLoc)
	return &jakartaTime, nil
}

// ParseDateToJakartaTimeEndOfDay parses a date string in DD-MM-YYYY format and converts it to Jakarta timezone
// Returns end of day (23:59:59.999999999) in Jakarta timezone
func ParseDateToJakartaTimeEndOfDay(dateStr string) (*time.Time, error) {
	if dateStr == "" {
		return nil, nil
	}

	date, err := time.Parse(DateFormatDDMMYYYY, dateStr)
	if err != nil {
		return nil, err
	}

	jakartaLoc, err := time.LoadLocation("Asia/Jakarta")
	if err != nil {
		return nil, err
	}

	jakartaTime := time.Date(date.Year(), date.Month(), date.Day(), 23, 59, 59, 999999999, jakartaLoc)
	return &jakartaTime, nil
}

// ParseDateRangeToJakartaTime parses date_from and date_to strings and returns them in Jakarta timezone
// date_from will be start of day (00:00:00), date_to will be end of day (23:59:59.999999999)
func ParseDateRangeToJakartaTime(dateFrom, dateTo string) (*time.Time, *time.Time, error) {
	var fromTime, toTime *time.Time
	var err error

	if dateFrom != "" {
		fromTime, err = ParseDateToJakartaTime(dateFrom)
		if err != nil {
			return nil, nil, err
		}
	}

	if dateTo != "" {
		toTime, err = ParseDateToJakartaTimeEndOfDay(dateTo)
		if err != nil {
			return nil, nil, err
		}
	}

	return fromTime, toTime, nil
}
