package repositories

import (
	"gorm.io/gorm"

	"GoMembership/internal/constants"
	"GoMembership/internal/database"

	"gorm.io/gorm/clause"

	"GoMembership/internal/models"
	"GoMembership/pkg/errors"
)

type UserRepositoryInterface interface {
	CreateUser(user *models.User) (uint, error)
	UpdateUser(user *models.User) (*models.User, error)
	GetUsers(where map[string]interface{}) (*[]models.User, error)
	GetUserByID(userID *uint) (*models.User, error)
	GetUserByEmail(email string) (*models.User, error)
	SetVerificationToken(verification *models.Verification) (uint, error)
	IsVerified(userID *uint) (bool, error)
	GetVerificationOfToken(token *string) (*models.Verification, error)
}

type UserRepository struct{}

func (ur *UserRepository) CreateUser(user *models.User) (uint, error) {
	result := database.DB.Create(user)
	if result.Error != nil {
		return 0, result.Error
	}
	return user.ID, nil
}

func (ur *UserRepository) UpdateUser(user *models.User) (*models.User, error) {
	if user == nil {
		return nil, errors.ErrNoData
	}

	err := database.DB.Transaction(func(tx *gorm.DB) error {
		if err := tx.First(&models.User{}, user.ID).Error; err != nil {
			return err
		}

		result := tx.Session(&gorm.Session{FullSaveAssociations: true}).Updates(user)
		if result.Error != nil {
			return result.Error
		}
		if result.RowsAffected == 0 {
			return errors.ErrNoRowsAffected
		}
		return nil
	})

	if err != nil {
		return nil, err
	}

	var updatedUser models.User
	if err := database.DB.First(&updatedUser, user.ID).Error; err != nil {
		return nil, err
	}

	return &updatedUser, nil
}

func (ur *UserRepository) GetUsers(where map[string]interface{}) (*[]models.User, error) {
	var users []models.User
	result := database.DB.
		Preload("Consents").
		Preload("BankAccount").
		Preload("Verification").
		Preload("Membership", func(db *gorm.DB) *gorm.DB {
			return db.Preload("SubscriptionModel")
		}).Where(where).Find(&users)
	if result.Error != nil {
		if result.Error == gorm.ErrRecordNotFound {
			return nil, gorm.ErrRecordNotFound
		}
		return nil, result.Error
	}
	return &users, nil
}

func (ur *UserRepository) GetUserByID(userID *uint) (*models.User, error) {
	var user models.User
	result := database.DB.
		Preload("Consents").
		Preload("BankAccount").
		Preload("Verification").
		Preload("Membership", func(db *gorm.DB) *gorm.DB {
			return db.Preload("SubscriptionModel")
		}).First(&user, userID)
	if result.Error != nil {
		if result.Error == gorm.ErrRecordNotFound {
			return nil, gorm.ErrRecordNotFound
		}
		return nil, result.Error
	}
	return &user, nil
}

func (ur *UserRepository) GetUserByEmail(email string) (*models.User, error) {
	var user models.User
	result := database.DB.Where("email = ?", email).First(&user)
	if result.Error != nil {
		if result.Error == gorm.ErrRecordNotFound {
			return nil, gorm.ErrRecordNotFound
		}
		return nil, result.Error
	}
	return &user, nil
}

func (ur *UserRepository) IsVerified(userID *uint) (bool, error) {
	var user models.User
	result := database.DB.Select("status").First(&user, userID)
	if result.Error != nil {
		if result.Error == gorm.ErrRecordNotFound {
			return false, gorm.ErrRecordNotFound
		}
		return false, result.Error
	}
	return user.Status != constants.UnverifiedStatus, nil
}

func (ur *UserRepository) GetVerificationOfToken(token *string) (*models.Verification, error) {

	var emailVerification models.Verification
	result := database.DB.Where("verification_token = ?", token).First(&emailVerification)
	if result.Error != nil {
		if result.Error == gorm.ErrRecordNotFound {
			return nil, gorm.ErrRecordNotFound
		}
		return nil, result.Error
	}
	return &emailVerification, nil
}

func (ur *UserRepository) SetVerificationToken(verification *models.Verification) (uint, error) {
	// Use GORM to insert or update the Verification record
	result := database.DB.Clauses(clause.OnConflict{
		Columns:   []clause.Column{{Name: "user_id"}},
		DoUpdates: clause.AssignmentColumns([]string{"verification_token", "created_at"}),
	}).Create(&verification)

	if result.Error != nil {
		return 0, result.Error
	}

	return verification.ID, nil
}
