package models

import (
	"fmt"
	"time"
)

type ShareLog struct {
	WorkInfoID uint64  `json:"workinfoid"` // ID of the associated work unit
	ClientID   int     `json:"clientid"`   // ID of the client/miner connection
	Enonce1    string  `json:"enonce1"`    // Extra nonce 1 (assigned by pool)
	Nonce2     string  `json:"nonce2"`     // Extra nonce 2 (sent by miner)
	Nonce      string  `json:"nonce"`      // Nonce used to generate the solution
	NTime      string  `json:"ntime"`      // Network time used in the block
	Diff       float64 `json:"diff"`       // Target difficulty for the share
	SDiff      float64 `json:"sdiff"`      // Share difficulty achieved
	Hash       string  `json:"hash"`       // Resulting hash from the share
	Result     bool    `json:"result"`     // Was this share a valid result
	Errn       int     `json:"errn"`       // Error code (0 = no error)

	CreateDate string `json:"createdate"` // Timestamp: "seconds,nanoseconds"
	CreateBy   string `json:"createby"`   // Origin of share creation ("code")
	CreateCode string `json:"createcode"` // Component that created this entry
	CreateInet string `json:"createinet"` // IP + port of submit origin

	WorkerName string `json:"workername"` // Full worker name (username.worker)
	Username   string `json:"username"`   // User's Bitcoin address
	Address    string `json:"address"`    // IP address of the worker
	Agent      string `json:"agent"`      // Miner agent string (e.g., bitaxe/BM1370)
}

type ShareStat struct {
	Label string
	Diff  string
	Time  string
}

// ParseCreateDate can be used to convert ShareLog.CreateDate to time.Time
func (s *ShareLog) ParseCreateDate() (time.Time, error) {
	var sec, nsec int64
	_, err := fmt.Sscanf(s.CreateDate, "%d,%d", &sec, &nsec)
	if err != nil {
		return time.Time{}, err
	}
	return time.Unix(sec, nsec), nil
}