package repository

import (
	"campaign-pool-be/app/database"
	"campaign-pool-be/app/database/entity"
	"campaign-pool-be/app/module/education_history/request"
	"campaign-pool-be/utils/paginator"
)

type educationHistoryRepository struct {
	DB *database.Database
}

type EducationHistoryRepository interface {
	GetAll(userId uint, req request.EducationHistoryQueryRequest) (educationHistories []*entity.EducationHistory, paging paginator.Pagination, err error)
	FindOneByUserAndId(userId uint, id uint) (educationHistory *entity.EducationHistory, err error)
	Create(educationHistory *entity.EducationHistory) (result *entity.EducationHistory, err error)
	Update(userId uint, id uint, educationHistory *entity.EducationHistory) (err error)
	Delete(userId uint, id uint) (err error)
}

func NewEducationHistoryRepository(db *database.Database) EducationHistoryRepository {
	return &educationHistoryRepository{
		DB: db,
	}
}

func (_i *educationHistoryRepository) GetAll(userId uint, req request.EducationHistoryQueryRequest) (educationHistories []*entity.EducationHistory, paging paginator.Pagination, err error) {
	query := _i.DB.DB.Where("user_id = ?", userId)

	// Apply filters
	if req.SchoolName != nil {
		query = query.Where("school_name ILIKE ?", "%"+*req.SchoolName+"%")
	}
	if req.Major != nil {
		query = query.Where("major ILIKE ?", "%"+*req.Major+"%")
	}
	if req.EducationLevel != nil {
		query = query.Where("education_level = ?", *req.EducationLevel)
	}
	if req.GraduationYear != nil {
		query = query.Where("graduation_year = ?", *req.GraduationYear)
	}

	// Include user relationship
	query = query.Preload("User")

	// Order by graduation year desc
	query = query.Order("graduation_year DESC")

	// Apply pagination
	var count int64
	query.Count(&count)
	req.Pagination.Count = count
	req.Pagination = paginator.Paging(req.Pagination)

	err = query.Offset(req.Pagination.Offset).Limit(req.Pagination.Limit).Find(&educationHistories).Error
	paging = *req.Pagination

	return
}

func (_i *educationHistoryRepository) FindOneByUserAndId(userId uint, id uint) (educationHistory *entity.EducationHistory, err error) {
	err = _i.DB.DB.Where("user_id = ? AND id = ?", userId, id).Preload("User").First(&educationHistory).Error
	return
}

func (_i *educationHistoryRepository) Create(educationHistory *entity.EducationHistory) (result *entity.EducationHistory, err error) {
	err = _i.DB.DB.Create(educationHistory).Error
	if err != nil {
		return nil, err
	}

	// Reload with relationships
	err = _i.DB.DB.Preload("User").First(&result, educationHistory.ID).Error
	return
}

func (_i *educationHistoryRepository) Update(userId uint, id uint, educationHistory *entity.EducationHistory) (err error) {
	err = _i.DB.DB.Where("user_id = ? AND id = ?", userId, id).Updates(educationHistory).Error
	return
}

func (_i *educationHistoryRepository) Delete(userId uint, id uint) (err error) {
	err = _i.DB.DB.Where("user_id = ? AND id = ?", userId, id).Delete(&entity.EducationHistory{}).Error
	return
}
