package service

import (
	"u-desk/internal/storage"
	"u-desk/internal/storage/models"
)

// ProfileService 连接配置 CRUD
type ProfileService struct{}

func NewProfileService() *ProfileService { return &ProfileService{} }

func (s *ProfileService) ListProfiles() ([]models.ConnectionProfile, error) {
	db := storage.GetDB()
	var list []models.ConnectionProfile
	err := db.Order("sort_order asc, id asc").Find(&list).Error
	return list, err
}

func (s *ProfileService) GetProfile(id uint) (*models.ConnectionProfile, error) {
	db := storage.GetDB()
	var p models.ConnectionProfile
	err := db.First(&p, id).Error
	if err != nil {
		return nil, err
	}
	return &p, nil
}

func (s *ProfileService) SaveProfile(p *models.ConnectionProfile) error {
	db := storage.GetDB()
	if p.ID == 0 {
		return db.Create(p).Error
	}
	return db.Save(p).Error
}

func (s *ProfileService) DeleteProfile(id uint) error {
	db := storage.GetDB()
	return db.Delete(&models.ConnectionProfile{}, id).Error
}
