package data

import (
	"context"
	"errors"

	"kra/internal/config"
	dataintegration "kra/internal/data/integration"
)

func (d *Data) persistStorageIntegrationConfig(ctx context.Context, storage *config.Storage) error {
	if !d.databaseReady.Load() {
		return errors.New("database is not initialized")
	}
	db := d.gormDB.load().WithContext(ctx)
	if !dataintegration.HasConfigTable(db) {
		return errors.New("integration configuration table does not exist")
	}
	return dataintegration.SaveStorageConfig(db, storage)
}

func (d *Data) persistEmailIntegrationConfig(ctx context.Context, email *config.Email) error {
	if !d.databaseReady.Load() {
		return errors.New("database is not initialized")
	}
	db := d.gormDB.load().WithContext(ctx)
	if !dataintegration.HasConfigTable(db) {
		return errors.New("integration configuration table does not exist")
	}
	return dataintegration.SaveEmailConfig(db, email)
}
