package repository

import (
	"context"
	"veza-backend-api/internal/models"
	"gorm.io/gorm"
)

// {{REPOSITORY_NAME}}Repository implements persistence for {{ENTITY}}
type {{REPOSITORY_NAME}}Repository struct {
	db *gorm.DB
}

// New{{REPOSITORY_NAME}}Repository creates a new {{REPOSITORY_NAME}}Repository instance
func New{{REPOSITORY_NAME}}Repository(db *gorm.DB) *{{REPOSITORY_NAME}}Repository {
	return &{{REPOSITORY_NAME}}Repository{
		db: db,
	}
}

// Create creates a new {{ENTITY}} in the database
func (r *{{REPOSITORY_NAME}}Repository) Create(
	ctx context.Context,
	{{ENTITY_LOWER}} *models.{{ENTITY}},
) error {
	if err := r.db.WithContext(ctx).Create({{ENTITY_LOWER}}).Error; err != nil {
		return fmt.Errorf("failed to create {{ENTITY_LOWER}}: %w", err)
	}
	return nil
}

// FindByID finds a {{ENTITY}} by ID
func (r *{{REPOSITORY_NAME}}Repository) FindByID(
	ctx context.Context,
	id uuid.UUID,
) (*models.{{ENTITY}}, error) {
	var {{ENTITY_LOWER}} models.{{ENTITY}}
	if err := r.db.WithContext(ctx).
		Where("id = ?", id).
		First(&{{ENTITY_LOWER}}).Error; err != nil {
		if errors.Is(err, gorm.ErrRecordNotFound) {
			return nil, types.ErrNotFound("{{ENTITY_LOWER}} not found")
		}
		return nil, fmt.Errorf("failed to find {{ENTITY_LOWER}}: %w", err)
	}
	return &{{ENTITY_LOWER}}, nil
}

// Update updates an existing {{ENTITY}}
func (r *{{REPOSITORY_NAME}}Repository) Update(
	ctx context.Context,
	{{ENTITY_LOWER}} *models.{{ENTITY}},
) error {
	if err := r.db.WithContext(ctx).
		Save({{ENTITY_LOWER}}).Error; err != nil {
		return fmt.Errorf("failed to update {{ENTITY_LOWER}}: %w", err)
	}
	return nil
}

// Delete deletes a {{ENTITY}} by ID
func (r *{{REPOSITORY_NAME}}Repository) Delete(
	ctx context.Context,
	id uuid.UUID,
) error {
	if err := r.db.WithContext(ctx).
		Delete(&models.{{ENTITY}}{}, "id = ?", id).Error; err != nil {
		return fmt.Errorf("failed to delete {{ENTITY_LOWER}}: %w", err)
	}
	return nil
}







