package system

import (
	"context"
	"time"

	bizsystem "kra/internal/biz/system"

	"gorm.io/gorm"
)

type maintenanceRepo struct{ data Provider }

func NewMaintenanceRepo(data Provider) bizsystem.MaintenanceRepo {
	return &maintenanceRepo{data: data}
}

func (r *maintenanceRepo) CleanupExpired(ctx context.Context) error {
	now := time.Now()
	return r.data.DB().WithContext(ctx).Transaction(func(tx *gorm.DB) error {
		if err := tx.Unscoped().Where("created_at < ?", now.Add(-2160*time.Hour)).Delete(&operationPO{}).Error; err != nil {
			return err
		}
		return tx.Unscoped().Where("created_at < ?", now.Add(-168*time.Hour)).Delete(&jwtBlacklistPO{}).Error
	})
}
