package repositories

import (
	"GoMembership/internal/database"

	"gorm.io/gorm"

	"GoMembership/internal/models"
)

type MembershipRepositoryInterface interface {
	CreateMembership(membership *models.Membership) (uint, error)
	FindMembershipByUserID(userID uint) (*models.Membership, error)
}

type MembershipRepository struct{}

func (repo *MembershipRepository) CreateMembership(membership *models.Membership) (uint, error) {
	result := database.DB.Create(membership)
	if result.Error != nil {
		return 0, result.Error
	}
	return membership.ID, nil
}

func (repo *MembershipRepository) FindMembershipByUserID(userID uint) (*models.Membership, error) {

	var membership models.Membership
	result := database.DB.First(&membership, userID)
	if result.Error != nil {
		if result.Error == gorm.ErrRecordNotFound {
			return nil, gorm.ErrRecordNotFound
		}
		return nil, result.Error
	}
	return &membership, nil
}
