package initialize

import (
	"context"

	"kra/internal/modules/system/biz"
	datapayment "kra/internal/modules/system/data/payment"
	datasystem "kra/internal/modules/system/data/repository"

	"gorm.io/gorm"
)

type Repo struct{ backend Backend }

func NewRepo(backend Backend) biz.InitializationRepo {
	return &Repo{backend: backend}
}

func (r *Repo) IsInitialized(ctx context.Context) (bool, error) {
	return r.backend.IsInitialized(ctx)
}

func (r *Repo) Initialize(ctx context.Context, input *biz.DatabaseConfig) error {
	return r.backend.InitializeDatabase(ctx, input, func(ctx context.Context, db *gorm.DB) error {
		return datasystem.SeedSystem(ctx, db, input, datapayment.AdminSurface())
	})
}

func (r *Repo) PersistConfig(ctx context.Context) error {
	return r.backend.PersistConfig(ctx)
}

func (r *Repo) PersistAdminConfig(ctx context.Context, value []byte) error {
	return r.backend.PersistAdminConfig(ctx, value)
}

func (r *Repo) PersistRuntimeConfig(ctx context.Context, data, admin []byte) error {
	return r.backend.PersistRuntimeConfig(ctx, data, admin)
}

func (r *Repo) ReloadConfig(ctx context.Context) error {
	return r.backend.ReloadConfig(ctx)
}
