// Package repository 提供数据访问层实现
// 包含设备控制等数据实体的仓库接口和实现
package repository

import (
	"strconv"

	"git.huangwc.com/pig/pig-farm-controller/internal/model"
	"gorm.io/gorm"
)

// DeviceRepo 设备仓库接口
type DeviceRepo interface {
	// Create 创建设备
	Create(device *model.Device) error

	// FindByID 根据ID查找设备
	FindByID(id uint) (*model.Device, error)

	// FindByIDString 根据ID字符串查找设备
	FindByIDString(id string) (*model.Device, error)

	// FindByParentID 根据上级设备ID查找设备
	FindByParentID(parentID uint) ([]*model.Device, error)

	// FindByType 根据设备类型查找设备
	FindByType(deviceType model.DeviceType) ([]*model.Device, error)

	// Update 更新设备信息
	Update(device *model.Device) error

	// Delete 删除设备
	Delete(id uint) error
}

// DeviceControlRepo 设备控制仓库接口
type DeviceControlRepo interface {
	// Create 创建设备控制记录
	Create(control *model.DeviceControl) error

	// FindByUserID 根据用户ID查找设备控制记录
	FindByUserID(userID uint) ([]*model.DeviceControl, error)

	// FindByID 根据ID查找设备控制记录
	FindByID(id uint) (*model.DeviceControl, error)

	// List 获取设备控制记录列表(分页)
	List(offset, limit int) ([]*model.DeviceControl, error)
}

// deviceRepo 设备仓库实现
type deviceRepo struct {
	db *gorm.DB
}

// deviceControlRepo 设备控制仓库实现
type deviceControlRepo struct {
	db *gorm.DB
}

// NewDeviceRepo 创建设备仓库实例
func NewDeviceRepo(db *gorm.DB) DeviceRepo {
	return &deviceRepo{
		db: db,
	}
}

// NewDeviceControlRepo 创建设备控制仓库实例
func NewDeviceControlRepo(db *gorm.DB) DeviceControlRepo {
	return &deviceControlRepo{
		db: db,
	}
}

// Create 创建设备
func (r *deviceRepo) Create(device *model.Device) error {
	result := r.db.Create(device)
	return result.Error
}

// FindByID 根据ID查找设备
func (r *deviceRepo) FindByID(id uint) (*model.Device, error) {
	var device model.Device
	result := r.db.First(&device, id)
	if result.Error != nil {
		return nil, result.Error
	}
	return &device, nil
}

// FindByIDString 根据ID字符串查找设备
func (r *deviceRepo) FindByIDString(id string) (*model.Device, error) {
	deviceID, err := strconv.ParseUint(id, 10, 64)
	if err != nil {
		return nil, err
	}

	var device model.Device
	result := r.db.First(&device, deviceID)
	if result.Error != nil {
		return nil, result.Error
	}
	return &device, nil
}

// FindByParentID 根据上级设备ID查找设备
func (r *deviceRepo) FindByParentID(parentID uint) ([]*model.Device, error) {
	var devices []*model.Device
	result := r.db.Where("parent_id = ?", parentID).Find(&devices)
	if result.Error != nil {
		return nil, result.Error
	}
	return devices, nil
}

// FindByType 根据设备类型查找设备
func (r *deviceRepo) FindByType(deviceType model.DeviceType) ([]*model.Device, error) {
	var devices []*model.Device
	result := r.db.Where("type = ?", deviceType).Find(&devices)
	if result.Error != nil {
		return nil, result.Error
	}
	return devices, nil
}

// Update 更新设备信息
func (r *deviceRepo) Update(device *model.Device) error {
	result := r.db.Save(device)
	return result.Error
}

// Delete 删除设备
func (r *deviceRepo) Delete(id uint) error {
	result := r.db.Delete(&model.Device{}, id)
	return result.Error
}

// Create 创建设备控制记录
func (r *deviceControlRepo) Create(control *model.DeviceControl) error {
	result := r.db.Create(control)
	return result.Error
}

// FindByUserID 根据用户ID查找设备控制记录
func (r *deviceControlRepo) FindByUserID(userID uint) ([]*model.DeviceControl, error) {
	var controls []*model.DeviceControl
	result := r.db.Where("user_id = ?", userID).Order("created_at DESC").Find(&controls)
	if result.Error != nil {
		return nil, result.Error
	}
	return controls, nil
}

// FindByID 根据ID查找设备控制记录
func (r *deviceControlRepo) FindByID(id uint) (*model.DeviceControl, error) {
	var control model.DeviceControl
	result := r.db.First(&control, id)
	if result.Error != nil {
		return nil, result.Error
	}
	return &control, nil
}

// List 获取设备控制记录列表(分页)
func (r *deviceControlRepo) List(offset, limit int) ([]*model.DeviceControl, error) {
	var controls []*model.DeviceControl
	result := r.db.Offset(offset).Limit(limit).Order("created_at DESC").Find(&controls)
	if result.Error != nil {
		return nil, result.Error
	}
	return controls, nil
}
