package utils

import (
	"encoding/json"
	"fmt"
	"os"
	"time"
)

func StructToJsonString(object any) (string, error) {

	jsonBytes, err := json.Marshal(object)

	if err != nil {
		return "", fmt.Errorf("Error in creating json from struct")
	}

	jsonString := string(jsonBytes)
	return jsonString, nil
}

func IndianEpoch() int64 {
	loc, err := time.LoadLocation("Asia/Kolkata")

	if err != nil {
		return time.Now().Unix()
	}
	return time.Now().In(loc).Unix()
}

func IndianTime() time.Time {
	loc, err := time.LoadLocation("Asia/Kolkata")
	if err != nil {
		return time.Now()
	}
	return time.Now().In(loc)
}

func CheckFileExists(filePath string) error {
	if _, err := os.Stat(filePath); err == nil {
		return nil
	} else if os.IsNotExist(err) {
		return fmt.Errorf("File does not exists: %s", filePath)
	} else {
		return fmt.Errorf("Error in file path checking")
	}
}
