package main

import (
	"strings"

	"github.com/spf13/viper"
)

func ReadConfig() (*viper.Viper, error) {
	defaults := map[string]any{
		"name":    "COOPGO Carpool Service",
		"dev_env": false,
		"storage": map[string]any{
			"db": map[string]any{
				"type": "psql",
				"mongodb": map[string]any{
					"host":    "localhost",
					"port":    27017,
					"db_name": "coopgo_platform",
					"collections": map[string]any{
						"regular_routes":               "carpool_regular_routes",
						"punctual_routes":              "carpool_punctual_routes",
						"bookings":                     "carpool_bookings",
						"driver_candidate_journeys":    "carpool_driver_candidate_journeys",
						"passenger_candidate_journeys": "carpool_passenger_candidate_journeys",
						"persisted_kv":                 "carpool_persisted_kv",
					},
				},
				"psql": map[string]any{
					"host":     "localhost",
					"port":     5432,
					"user":     "postgres",
					"password": "postgres",
					"dbname":   "coopgo_platform",
					"schema":   "carpool_service",
					"sslmode":  "disable",
					"tables": map[string]any{
						"regular_routes":          "regular_routes",
						"regular_route_schedules": "regular_route_schedules",
						"bookings":                "bookings",
						"journeys_cache":          "journeys_cache",
					},
				},
			},
		},
		"services": map[string]any{
			"grpc": map[string]any{
				"enable": true,
				"port":   8080,
			},
			"ocss_api": map[string]any{
				"enable": true,
				"port":   80,
			},
		},
		"interoperability": map[string]any{
			"internal_operator_id": "example.coopgo.fr",
			"ocss": map[string]any{
				"operator_id": "example.coopgo.fr",
				"web_urls": map[string]string{
					"driver_journeys":    "https://example.coopgo.fr/driver_journeys/%s",
					"passenger_journeys": "https://example.coopgo.fr/passenger_journeys/%s",
				},
			},
		},
	}

	v := viper.New()
	for key, value := range defaults {
		v.SetDefault(key, value)
	}
	v.SetConfigName("config")
	v.AddConfigPath(".")
	v.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
	v.AutomaticEnv()
	err := v.ReadInConfig()
	return v, err
}