package repository

import (
	"context"

	"eslogad-be/internal/entities"

	"github.com/google/uuid"
	"gorm.io/gorm"
)

type LabelRepository struct{ db *gorm.DB }

func NewLabelRepository(db *gorm.DB) *LabelRepository { return &LabelRepository{db: db} }
func (r *LabelRepository) Create(ctx context.Context, e *entities.Label) error {
	return r.db.WithContext(ctx).Create(e).Error
}
func (r *LabelRepository) Update(ctx context.Context, e *entities.Label) error {
	return r.db.WithContext(ctx).Model(&entities.Label{}).Where("id = ?", e.ID).Updates(e).Error
}
func (r *LabelRepository) Delete(ctx context.Context, id uuid.UUID) error {
	return r.db.WithContext(ctx).Delete(&entities.Label{}, "id = ?", id).Error
}
func (r *LabelRepository) List(ctx context.Context) ([]entities.Label, error) {
	var list []entities.Label
	err := r.db.WithContext(ctx).Order("name ASC").Find(&list).Error
	return list, err
}
func (r *LabelRepository) Get(ctx context.Context, id uuid.UUID) (*entities.Label, error) {
	var e entities.Label
	if err := r.db.WithContext(ctx).First(&e, "id = ?", id).Error; err != nil {
		return nil, err
	}
	return &e, nil
}

type PriorityRepository struct{ db *gorm.DB }

func NewPriorityRepository(db *gorm.DB) *PriorityRepository { return &PriorityRepository{db: db} }
func (r *PriorityRepository) Create(ctx context.Context, e *entities.Priority) error {
	return r.db.WithContext(ctx).Create(e).Error
}
func (r *PriorityRepository) Update(ctx context.Context, e *entities.Priority) error {
	return r.db.WithContext(ctx).Model(&entities.Priority{}).Where("id = ?", e.ID).Updates(e).Error
}
func (r *PriorityRepository) Delete(ctx context.Context, id uuid.UUID) error {
	return r.db.WithContext(ctx).Delete(&entities.Priority{}, "id = ?", id).Error
}
func (r *PriorityRepository) List(ctx context.Context) ([]entities.Priority, error) {
	var list []entities.Priority
	err := r.db.WithContext(ctx).Order("level ASC").Find(&list).Error
	return list, err
}
func (r *PriorityRepository) Get(ctx context.Context, id uuid.UUID) (*entities.Priority, error) {
	var e entities.Priority
	if err := r.db.WithContext(ctx).First(&e, "id = ?", id).Error; err != nil {
		return nil, err
	}
	return &e, nil
}

func (r *PriorityRepository) GetByIDs(ctx context.Context, ids []uuid.UUID) (map[uuid.UUID]*entities.Priority, error) {
	if len(ids) == 0 {
		return make(map[uuid.UUID]*entities.Priority), nil
	}
	
	var priorities []entities.Priority
	if err := r.db.WithContext(ctx).Where("id IN ?", ids).Find(&priorities).Error; err != nil {
		return nil, err
	}
	
	result := make(map[uuid.UUID]*entities.Priority)
	for i := range priorities {
		result[priorities[i].ID] = &priorities[i]
	}
	
	return result, nil
}

type InstitutionRepository struct{ db *gorm.DB }

func NewInstitutionRepository(db *gorm.DB) *InstitutionRepository {
	return &InstitutionRepository{db: db}
}
func (r *InstitutionRepository) Create(ctx context.Context, e *entities.Institution) error {
	return r.db.WithContext(ctx).Create(e).Error
}
func (r *InstitutionRepository) Update(ctx context.Context, e *entities.Institution) error {
	return r.db.WithContext(ctx).Model(&entities.Institution{}).Where("id = ?", e.ID).Updates(e).Error
}
func (r *InstitutionRepository) Delete(ctx context.Context, id uuid.UUID) error {
	return r.db.WithContext(ctx).Delete(&entities.Institution{}, "id = ?", id).Error
}
func (r *InstitutionRepository) List(ctx context.Context) ([]entities.Institution, error) {
	var list []entities.Institution
	err := r.db.WithContext(ctx).Order("name ASC").Find(&list).Error
	return list, err
}

func (r *InstitutionRepository) ListWithSearch(ctx context.Context, search *string) ([]entities.Institution, error) {
	var list []entities.Institution
	q := r.db.WithContext(ctx).Model(&entities.Institution{})

	if search != nil && *search != "" {
		like := "%" + *search + "%"
		q = q.Where("name ILIKE ? OR type ILIKE ? OR address ILIKE ? OR contact_person ILIKE ?", like, like, like, like)
	}

	err := q.Order("name ASC").Find(&list).Error
	return list, err
}

func (r *InstitutionRepository) Get(ctx context.Context, id uuid.UUID) (*entities.Institution, error) {
	var e entities.Institution
	if err := r.db.WithContext(ctx).First(&e, "id = ?", id).Error; err != nil {
		return nil, err
	}
	return &e, nil
}

func (r *InstitutionRepository) GetByIDs(ctx context.Context, ids []uuid.UUID) (map[uuid.UUID]*entities.Institution, error) {
	if len(ids) == 0 {
		return make(map[uuid.UUID]*entities.Institution), nil
	}
	
	var institutions []entities.Institution
	if err := r.db.WithContext(ctx).Where("id IN ?", ids).Find(&institutions).Error; err != nil {
		return nil, err
	}
	
	result := make(map[uuid.UUID]*entities.Institution)
	for i := range institutions {
		result[institutions[i].ID] = &institutions[i]
	}
	
	return result, nil
}

type DispositionActionRepository struct{ db *gorm.DB }

func NewDispositionActionRepository(db *gorm.DB) *DispositionActionRepository {
	return &DispositionActionRepository{db: db}
}
func (r *DispositionActionRepository) Create(ctx context.Context, e *entities.DispositionAction) error {
	return r.db.WithContext(ctx).Create(e).Error
}
func (r *DispositionActionRepository) Update(ctx context.Context, e *entities.DispositionAction) error {
	return r.db.WithContext(ctx).Model(&entities.DispositionAction{}).Where("id = ?", e.ID).Updates(e).Error
}
func (r *DispositionActionRepository) Delete(ctx context.Context, id uuid.UUID) error {
	return r.db.WithContext(ctx).Delete(&entities.DispositionAction{}, "id = ?", id).Error
}
func (r *DispositionActionRepository) List(ctx context.Context) ([]entities.DispositionAction, error) {
	var list []entities.DispositionAction
	err := r.db.WithContext(ctx).Order("sort_order NULLS LAST, label ASC").Find(&list).Error
	return list, err
}
func (r *DispositionActionRepository) Get(ctx context.Context, id uuid.UUID) (*entities.DispositionAction, error) {
	var e entities.DispositionAction
	if err := r.db.WithContext(ctx).First(&e, "id = ?", id).Error; err != nil {
		return nil, err
	}
	return &e, nil
}

func (r *DispositionActionRepository) GetByIDs(ctx context.Context, ids []uuid.UUID) ([]entities.DispositionAction, error) {
	var actions []entities.DispositionAction
	if len(ids) == 0 {
		return actions, nil
	}
	if err := r.db.WithContext(ctx).Where("id IN ?", ids).Find(&actions).Error; err != nil {
		return nil, err
	}
	return actions, nil
}

type DepartmentRepository struct{ db *gorm.DB }

func NewDepartmentRepository(db *gorm.DB) *DepartmentRepository { return &DepartmentRepository{db: db} }

func (r *DepartmentRepository) GetByCode(ctx context.Context, code string) (*entities.Department, error) {
	db := DBFromContext(ctx, r.db)
	var dep entities.Department
	if err := db.WithContext(ctx).Where("code = ?", code).First(&dep).Error; err != nil {
		return nil, err
	}
	return &dep, nil
}

func (r *DepartmentRepository) Get(ctx context.Context, id uuid.UUID) (*entities.Department, error) {
	db := DBFromContext(ctx, r.db)
	var dep entities.Department
	if err := db.WithContext(ctx).First(&dep, "id = ?", id).Error; err != nil {
		return nil, err
	}
	return &dep, nil
}

func (r *DepartmentRepository) List(ctx context.Context, search string, limit, offset int) ([]entities.Department, int64, error) {
	db := DBFromContext(ctx, r.db)
	
	query := db.WithContext(ctx).Model(&entities.Department{})
	
	// Add search filter if provided
	if search != "" {
		query = query.Where("name ILIKE ?", "%"+search+"%")
	}
	
	// Get total count
	var total int64
	if err := query.Count(&total).Error; err != nil {
		return nil, 0, err
	}
	
	// Get paginated results
	var list []entities.Department
	if err := query.
		Order("name ASC").
		Limit(limit).
		Offset(offset).
		Find(&list).Error; err != nil {
		return nil, 0, err
	}
	
	return list, total, nil
}

func (r *DepartmentRepository) ListWithParentFilter(ctx context.Context, search string, limit, offset int, parentPath string, excludedPaths []string) ([]entities.Department, int64, error) {
	db := DBFromContext(ctx, r.db)
	
	query := db.WithContext(ctx).Model(&entities.Department{})
	
	// Filter by parent path if provided - include the parent itself and all descendants
	if parentPath != "" {
		query = query.Where("path = ? OR path <@ ?", parentPath, parentPath)
	}
	
	// Exclude specific paths
	for _, excludedPath := range excludedPaths {
		query = query.Where("NOT (path ~ ?)", excludedPath)
	}
	
	// Add search filter if provided
	if search != "" {
		query = query.Where("name ILIKE ? OR code ILIKE ?", "%"+search+"%", "%"+search+"%")
	}
	
	// Get total count
	var total int64
	if err := query.Count(&total).Error; err != nil {
		return nil, 0, err
	}
	
	// Get paginated results
	var list []entities.Department
	if err := query.
		Order("name ASC").
		Limit(limit).
		Offset(offset).
		Find(&list).Error; err != nil {
		return nil, 0, err
	}
	
	return list, total, nil
}

func (r *DepartmentRepository) GetByID(ctx context.Context, id uuid.UUID) (*entities.Department, error) {
	db := DBFromContext(ctx, r.db)
	var e entities.Department
	if err := db.WithContext(ctx).First(&e, "id = ?", id).Error; err != nil {
		return nil, err
	}
	return &e, nil
}

func (r *DepartmentRepository) Create(ctx context.Context, department *entities.Department) error {
	db := DBFromContext(ctx, r.db)
	return db.WithContext(ctx).Create(department).Error
}

func (r *DepartmentRepository) Update(ctx context.Context, department *entities.Department) error {
	db := DBFromContext(ctx, r.db)
	return db.WithContext(ctx).Save(department).Error
}

func (r *DepartmentRepository) Delete(ctx context.Context, id uuid.UUID) error {
	db := DBFromContext(ctx, r.db)
	return db.WithContext(ctx).Delete(&entities.Department{}, "id = ?", id).Error
}

func (r *DepartmentRepository) GetByPath(ctx context.Context, path string) (*entities.Department, error) {
	db := DBFromContext(ctx, r.db)
	var department entities.Department
	if err := db.WithContext(ctx).Where("path = ?", path).First(&department).Error; err != nil {
		return nil, err
	}
	return &department, nil
}

func (r *DepartmentRepository) GetAll(ctx context.Context) ([]entities.Department, error) {
	db := DBFromContext(ctx, r.db)
	var departments []entities.Department
	if err := db.WithContext(ctx).Order("path ASC").Find(&departments).Error; err != nil {
		return nil, err
	}
	return departments, nil
}

func (r *DepartmentRepository) GetAllWithParentFilter(ctx context.Context, parentPath string, excludedPaths []string) ([]entities.Department, error) {
	db := DBFromContext(ctx, r.db)
	var departments []entities.Department
	
	query := db.WithContext(ctx)
	
	// Filter by parent path if provided - include the parent itself and all descendants
	if parentPath != "" {
		query = query.Where("path = ? OR path <@ ?", parentPath, parentPath)
	}
	
	// Exclude specific paths
	for _, excludedPath := range excludedPaths {
		query = query.Where("NOT (path ~ ?)", excludedPath)
	}
	
	if err := query.Order("path ASC").Find(&departments).Error; err != nil {
		return nil, err
	}
	return departments, nil
}

func (r *DepartmentRepository) GetByPathPrefix(ctx context.Context, pathPrefix string) ([]entities.Department, error) {
	db := DBFromContext(ctx, r.db)
	var departments []entities.Department
	// Using ltree operators for hierarchical queries
	query := db.WithContext(ctx).Order("path ASC")
	if pathPrefix != "" {
		// Get all descendants of a path
		query = query.Where("path <@ ?", pathPrefix)
	}
	if err := query.Find(&departments).Error; err != nil {
		return nil, err
	}
	return departments, nil
}

func (r *DepartmentRepository) GetChildren(ctx context.Context, parentPath string) ([]entities.Department, error) {
	db := DBFromContext(ctx, r.db)
	var departments []entities.Department
	// Get direct children and all descendants
	if err := db.WithContext(ctx).
		Where("path <@ ? AND path != ?", parentPath, parentPath).
		Order("path ASC").
		Find(&departments).Error; err != nil {
		return nil, err
	}
	return departments, nil
}

func (r *DepartmentRepository) GetAllDescendants(ctx context.Context, parentID uuid.UUID) ([]entities.Department, error) {
	db := DBFromContext(ctx, r.db)
	
	// First get the parent department to get its path
	var parent entities.Department
	if err := db.WithContext(ctx).First(&parent, "id = ?", parentID).Error; err != nil {
		return nil, err
	}
	
	var departments []entities.Department
	// Get all descendants using ltree
	if err := db.WithContext(ctx).
		Where("path <@ ? AND path != ?", parent.Path, parent.Path).
		Order("path ASC").
		Find(&departments).Error; err != nil {
		return nil, err
	}
	return departments, nil
}

func (r *DepartmentRepository) UpdateChildrenPaths(ctx context.Context, oldPath, newPath string) error {
	db := DBFromContext(ctx, r.db)
	// Use raw SQL for ltree path update
	// This will update all children paths by replacing the old prefix with the new one
	query := `
		UPDATE departments 
		SET path = ? || subpath(path, nlevel(?))
		WHERE path <@ ? AND path != ?
	`
	return db.WithContext(ctx).Exec(query, newPath, oldPath, oldPath, oldPath).Error
}
