package model

import (
	"crawler/config"
	"crawler/service"
	"database/sql"
	"log"
)

type G2AOrderModel struct {
	G2AOrderParams
	G2AOrderResult
	G2AOrderDetailResult
	G2AOrderPayResult
	G2AOrderKeyResult
	G2AOrder
}

type G2AOrderParams struct {
	ProductID string  `form:"product_id" json:"product_id" binding:"required"`
	Currency  string  `form:"currency" json:"currency"`
	MaxPrice  float64 `form:"max_price" json:"max_price"`
}

// Add an Order
type G2AOrderResult struct {
	OrderID  string  `json:"order_id"`
	Price    float32 `json:"price"`
	Currency string  `json:"currency"`
}

// Get Order Detail
type G2AOrderDetailResult struct {
	Status   string  `json:"status"`
	Price    float32 `json:"price"`
	Currency string  `json:"currency"`
}

// Get Order Key
type G2AOrderKeyResult struct {
	Key string `json:"key"`
}

// Pay for an order
type G2AOrderPayResult struct {
	Status        bool   `json:"status"`
	TransactionID string `json:"transaction_id"`
}

type G2AOrder struct {
	RequestKey           string  `json:"request_key"`
	RequestOrderID       int     `json:"request_order_id"`
	RequestOrderDetailID int     `json:"request_order_detail_id"`
	Status               string  `json:"status"`
	OrderID              string  `json:"order_id"`
	ProductID            string  `json:"product_id"`
	Code                 string  `json:"code"`
	Price                float32 `json:"price"`
	Currency             string  `json:"currency"`
	TransactionID        string  `json:"transaction_id"`
}

func (this *G2AOrderModel) IsExists(requestKey string) bool {
	var (
		db     = service.DB_PLAYR
		conn   = db.SQLDB
		query  = "SELECT IF(COUNT(*) <= 0, 0, 1) AS `exists` FROM tb_g2a_order WHERE request_key = ?;"
		exists = false
	)

	stmt, err := conn.Prepare(query)
	if err != nil {
		log.Fatal(err)
	}

	err = stmt.QueryRow(requestKey).Scan(&exists)
	if err != nil {
		db.SetErrorLog(err, query)
		return exists
	}

	db.SetGeneralLog(config.GL_ACTION_SELECT, query, "select exists g2a order")
	return exists
}

func (this *G2AOrderModel) Info(requestKey string) (G2AOrder, error) {
	var (
		db    = service.DB_PLAYR
		conn  = db.SQLDB
		query = `SELECT request_key, request_order_id, request_order_detail_id, status, order_id, product_id, code, price, currency, transaction_id FROM tb_g2a_order WHERE request_key = ?;`
		info  G2AOrder
	)

	err := conn.QueryRow(query, requestKey).Scan(
		&info.RequestKey, &info.RequestOrderID, &info.RequestOrderDetailID, &info.Status, &info.OrderID, &info.ProductID, &info.Code, &info.Price, &info.Currency, &info.TransactionID,
	)

	if err != nil && err != sql.ErrNoRows {
		db.SetErrorLog(err, query)
		return info, err
	}

	db.SetGeneralLog(config.GL_ACTION_SELECT, query, "select g2a info")
	return info, nil
}

func (this *G2AOrderModel) Insert(order G2AOrder) {
	var (
		db   = service.DB_PLAYR
		conn = db.SQLDB
	)

	sql := `
		INSERT INTO tb_g2a_order
		SET 
			request_key = ?,
			request_order_id = ?,
			request_order_detail_id = ?,
			status = ?,
			order_id = ?,
			product_id = ?,
			code = ?,
			price = ?,
			currency = ?,
			transaction_id = ?,
			created_at = NOW();
	`
	_, err := conn.Exec(sql,
		order.RequestKey, order.RequestOrderID, order.RequestOrderDetailID, order.Status, order.OrderID, order.ProductID, order.Code, order.Price, order.Currency, order.TransactionID,
	)

	if err != nil {
		db.SetErrorLog(err, sql)
	}

	db.SetGeneralLog(config.GL_ACTION_WRITE, sql, "insert g2a order info")
}
