package models

import (
	"github.com/jinzhu/gorm"
)

type Device struct {
	BaseModel
	DeviceID            string        `json:"deviceId"`
	DeviceName          string        `json:"deviceName"`
	IMEI                string        `json:"imei"`
	IMSI                string        `json:"imsi"`
	DeviceConfiguration string        `json:"deviceConfiguration" gorm:"type:json"`
	CompanyID           uint          `json:"companyId"`
	DeviceInfos         *[]DeviceInfo `json:"deviceInfos"`
	CurrentContractID   *uint         `json:"currentContractId"`
}

type DeviceResponse struct {
	BaseModel
	DeviceID            string        `json:"deviceId"`
	DeviceName          string        `json:"deviceName"`
	IMEI                string        `json:"imei"`
	IMSI                string        `json:"imsi"`
	DeviceConfiguration string        `json:"deviceConfiguration" gorm:"type:json"`
	CompanyID           uint          `json:"companyId"`
	DeviceInfos         *[]DeviceInfo `json:"deviceInfos"`
}

type ContractDeviceInfo struct {
	ID   uint   `json:"id"`
	IMEI string `json:"imei"`
}

func ConvertDeviceToResponse(devices []Device) []DeviceResponse {
	var deviceResponses []DeviceResponse
	for _, device := range devices {
		if device.DeviceInfos == nil {
			emptySlice := make([]DeviceInfo, 0)
			device.DeviceInfos = &emptySlice
		}

		deviceResponse := DeviceResponse{
			BaseModel: BaseModel{
				ID:        device.ID,
				CreatedAt: device.CreatedAt,
				UpdatedAt: device.UpdatedAt,
			},
			DeviceID:            device.DeviceID,
			DeviceName:          device.DeviceName,
			IMEI:                device.IMEI,
			IMSI:                device.IMSI,
			DeviceConfiguration: device.DeviceConfiguration,
			CompanyID:           device.CompanyID,
			DeviceInfos:         device.DeviceInfos,
		}
		deviceResponses = append(deviceResponses, deviceResponse)
	}
	return deviceResponses
}

func (Device) Update() (bool, error) {
	return false, nil
}
func (Device) Create() (bool, error) {
	return false, nil
}
func (d *Device) Delete(db *gorm.DB) (bool, error) {

	// Soft delete the device record.
	if err := db.Delete(d).Error; err != nil {
		return false, err
	}

	return true, nil
}
