package business_profile

import (
	"context"

	"github.com/UpmeshLTD/urus-aio/infrastructure/db/entities"
	"github.com/UpmeshLTD/urus-aio/shipment/db/gormdb"
)

type Repository interface {
	CreateBusinessProfile(ctx context.Context, profile *entities.BusinessProfile) (*entities.BusinessProfile, error)
	UpdateBusinessProfile(ctx context.Context, id string, profile *entities.BusinessProfile) error
	DeleteBusinessProfile(ctx context.Context, id string) error
	GetBusinessProfileByID(ctx context.Context, id string) (*entities.BusinessProfile, error)
}

type repositoryImpl struct {
	db gormdb.GormDB
}

func NewRepository(db gormdb.GormDB) Repository {
	return &repositoryImpl{
		db: db,
	}
}

func (r *repositoryImpl) CreateBusinessProfile(ctx context.Context, address *entities.BusinessProfile) (*entities.BusinessProfile, error) {
	if err := r.db.DB().Create(&address).Error; err != nil {
		return nil, err
	}
	return address, nil
}

func (r *repositoryImpl) UpdateBusinessProfile(ctx context.Context, id string, address *entities.BusinessProfile) error {
	return r.db.DB().Model(&entities.BusinessProfile{}).Where("id = ?", id).Updates(&address).Error
}

func (r *repositoryImpl) DeleteBusinessProfile(ctx context.Context, id string) error {
	return r.db.DB().Delete(&entities.BusinessProfile{ID: id}).Error
}

func (r *repositoryImpl) GetBusinessProfileByID(ctx context.Context, id string) (*entities.BusinessProfile, error) {
	res := &entities.BusinessProfile{}
	if err := r.db.DB().First(res, "id = ?", id).Error; err != nil {
		return nil, err
	}
	return res, nil
}
