from datetime import datetime
from zoneinfo import ZoneInfo


class DatetimeUtil:
    @staticmethod
    def now():
        """Waktu UTC (timezone-aware)"""
        return datetime.now(tz=ZoneInfo("UTC"))

    @staticmethod
    def now_iso():
        """Waktu UTC dalam format ISO 8601 string"""
        return datetime.now(tz=ZoneInfo("UTC")).isoformat()

    @staticmethod
    def now_jakarta():
        """Waktu sekarang di zona Asia/Jakarta (WIB)"""
        return datetime.now(tz=ZoneInfo("Asia/Jakarta"))

    @staticmethod
    def to_string(dt: datetime, fmt: str = "%Y-%m-%d %H:%M:%S") -> str:
        """Convert UTC datetime to Asia/Jakarta time and format as string"""
        if dt.tzinfo is None:
            dt = dt.replace(tzinfo=ZoneInfo("UTC"))
        jakarta_time = dt.astimezone(ZoneInfo("Asia/Jakarta"))
        return jakarta_time.strftime(fmt)

    @staticmethod
    def from_string(
        date_str: str, fmt: str = "%Y-%m-%d %H:%M:%S", tz: str = "UTC"
    ) -> datetime:
        """Convert string ke datetime dengan timezone"""
        dt = datetime.strptime(date_str, fmt)
        return dt.replace(tzinfo=ZoneInfo(tz))
