// Copyright (c) 2020-2024 Blockwatch Data Inc.
// Author: alex@blockwatch.cc

package tables

import (
	"encoding/hex"
	"encoding/json"
	"fmt"
	"io"
	"math"
	"net/http"
	"strconv"
	"strings"
	"time"

	"blockwatch.cc/packdb/encoding/csv"
	"blockwatch.cc/packdb/pack"
	"blockwatch.cc/packdb/util"
	"blockwatch.cc/tzgo/tezos"
	"blockwatch.cc/tzindex/etl/model"
	"blockwatch.cc/tzindex/rpc"
	"blockwatch.cc/tzindex/server"
)

var (
	// long -> short form
	contractSourceNames map[string]string
	// all aliases as list
	contractAllAliases []string
)

func init() {
	fields, err := pack.Fields(&model.Contract{})
	if err != nil {
		log.Fatalf("contract field type error: %v\n", err)
	}
	contractSourceNames = fields.NameMapReverse()
	contractAllAliases = fields.Aliases()

	// add extra transalations for accounts
	contractSourceNames["creator"] = "C"
	contractSourceNames["first_seen_time"] = "f"
	contractSourceNames["last_seen_time"] = "l"
	contractAllAliases = append(contractAllAliases, "creator")
}

// configurable marshalling helper
type Contract struct {
	model.Contract
	verbose bool            // cond. marshal
	columns util.StringList // cond. cols & order when brief
	params  *rpc.Params     // blockchain amount conversion
	ctx     *server.Context
}

func (c *Contract) MarshalJSON() ([]byte, error) {
	if c.verbose {
		return c.MarshalJSONVerbose()
	} else {
		return c.MarshalJSONBrief()
	}
}

func (c *Contract) MarshalJSONVerbose() ([]byte, error) {
	cc := struct {
		RowId         uint64  `json:"row_id"`
		AccountId     uint64  `json:"account_id"`
		Address       string  `json:"address"`
		CreatorId     uint64  `json:"creator_id"`
		Creator       string  `json:"creator"`
		FirstSeen     int64   `json:"first_seen"`
		LastSeen      int64   `json:"last_seen"`
		FirstSeenTime int64   `json:"first_seen_time"`
		LastSeenTime  int64   `json:"last_seen_time"`
		StorageSize   int64   `json:"storage_size"`
		StoragePaid   int64   `json:"storage_paid"`
		StorageBurn   float64 `json:"storage_burn"`
		Script        string  `json:"script"`
		Storage       string  `json:"storage"`
		InterfaceHash string  `json:"iface_hash"`
		CodeHash      string  `json:"code_hash"`
		StorageHash   string  `json:"storage_hash"`
		CallStats     string  `json:"call_stats"`
		Features      string  `json:"features"`
		Interfaces    string  `json:"interfaces"`
	}{
		RowId:         c.RowId.U64(),
		AccountId:     c.AccountId.U64(),
		Address:       c.String(),
		CreatorId:     c.CreatorId.U64(),
		Creator:       c.ctx.Indexer.LookupAddress(c.ctx, c.CreatorId).String(),
		FirstSeen:     c.FirstSeen,
		LastSeen:      c.LastSeen,
		FirstSeenTime: c.ctx.Indexer.LookupBlockTimeMs(c.ctx.Context, c.FirstSeen),
		LastSeenTime:  c.ctx.Indexer.LookupBlockTimeMs(c.ctx.Context, c.LastSeen),
		StorageSize:   c.StorageSize,
		StoragePaid:   c.StoragePaid,
		StorageBurn:   c.params.ConvertValue(c.StorageBurn),
		Script:        hex.EncodeToString(c.Script),
		Storage:       hex.EncodeToString(c.Storage),
		CallStats:     hex.EncodeToString(c.CallStats),
		Features:      c.Features.String(),
		Interfaces:    c.Interfaces.String(),
		InterfaceHash: util.U64String(c.InterfaceHash).Hex(),
		CodeHash:      util.U64String(c.CodeHash).Hex(),
		StorageHash:   util.U64String(c.StorageHash).Hex(),
	}
	return json.Marshal(cc)
}

func (c *Contract) MarshalJSONBrief() ([]byte, error) {
	buf := make([]byte, 0, 2048)
	buf = append(buf, '[')
	for i, v := range c.columns {
		switch v {
		case "row_id":
			buf = strconv.AppendUint(buf, c.RowId.U64(), 10)
		case "account_id":
			buf = strconv.AppendUint(buf, c.AccountId.U64(), 10)
		case "address":
			buf = strconv.AppendQuote(buf, c.String())
		case "creator_id":
			buf = strconv.AppendUint(buf, c.CreatorId.U64(), 10)
		case "creator":
			buf = strconv.AppendQuote(buf, c.ctx.Indexer.LookupAddress(c.ctx, c.CreatorId).String())
		case "first_seen":
			buf = strconv.AppendInt(buf, c.FirstSeen, 10)
		case "last_seen":
			buf = strconv.AppendInt(buf, c.LastSeen, 10)
		case "first_seen_time":
			buf = strconv.AppendInt(buf, c.ctx.Indexer.LookupBlockTimeMs(c.ctx.Context, c.FirstSeen), 10)
		case "last_seen_time":
			buf = strconv.AppendInt(buf, c.ctx.Indexer.LookupBlockTimeMs(c.ctx.Context, c.LastSeen), 10)
		case "storage_size":
			buf = strconv.AppendInt(buf, c.StorageSize, 10)
		case "storage_paid":
			buf = strconv.AppendInt(buf, c.StoragePaid, 10)
		case "storage_burn":
			buf = strconv.AppendFloat(buf, c.params.ConvertValue(c.StorageBurn), 'f', c.params.Decimals, 64)
		case "script":
			// code is binary
			if c.Script != nil {
				buf = strconv.AppendQuote(buf, hex.EncodeToString(c.Script))
			} else {
				buf = append(buf, null...)
			}
		case "storage":
			// code is binary
			if c.Storage != nil {
				buf = strconv.AppendQuote(buf, hex.EncodeToString(c.Storage))
			} else {
				buf = append(buf, null...)
			}
		case "iface_hash":
			buf = strconv.AppendQuote(buf, util.U64String(c.InterfaceHash).Hex())
		case "code_hash":
			buf = strconv.AppendQuote(buf, util.U64String(c.CodeHash).Hex())
		case "storage_hash":
			buf = strconv.AppendQuote(buf, util.U64String(c.StorageHash).Hex())
		case "call_stats":
			buf = strconv.AppendQuote(buf, hex.EncodeToString(c.CallStats))
		case "features":
			buf = strconv.AppendQuote(buf, c.Features.String())
		case "interfaces":
			buf = strconv.AppendQuote(buf, c.Interfaces.String())
		default:
			continue
		}
		if i < len(c.columns)-1 {
			buf = append(buf, ',')
		}
	}
	buf = append(buf, ']')
	return buf, nil
}

func (c *Contract) MarshalCSV() ([]string, error) {
	res := make([]string, len(c.columns))
	for i, v := range c.columns {
		switch v {
		case "row_id":
			res[i] = strconv.FormatUint(c.RowId.U64(), 10)
		case "account_id":
			res[i] = strconv.FormatUint(c.AccountId.U64(), 10)
		case "address":
			res[i] = strconv.Quote(c.String())
		case "creator_id":
			res[i] = strconv.FormatUint(c.CreatorId.U64(), 10)
		case "creator":
			res[i] = strconv.Quote(c.ctx.Indexer.LookupAddress(c.ctx, c.CreatorId).String())
		case "first_seen":
			res[i] = strconv.FormatInt(c.FirstSeen, 10)
		case "last_seen":
			res[i] = strconv.FormatInt(c.LastSeen, 10)
		case "first_seen_time":
			res[i] = strconv.Quote(c.ctx.Indexer.LookupBlockTime(c.ctx.Context, c.FirstSeen).Format(time.RFC3339))
		case "last_seen_time":
			res[i] = strconv.Quote(c.ctx.Indexer.LookupBlockTime(c.ctx.Context, c.LastSeen).Format(time.RFC3339))
		case "storage_size":
			res[i] = strconv.FormatInt(c.StorageSize, 10)
		case "storage_burn":
			res[i] = strconv.FormatFloat(c.params.ConvertValue(c.StorageBurn), 'f', c.params.Decimals, 64)
		case "storage_paid":
			res[i] = strconv.FormatInt(c.StoragePaid, 10)
		case "script":
			res[i] = strconv.Quote(hex.EncodeToString(c.Script))
		case "storage":
			res[i] = strconv.Quote(hex.EncodeToString(c.Storage))
		case "iface_hash":
			res[i] = strconv.Quote(util.U64String(c.InterfaceHash).Hex())
		case "code_hash":
			res[i] = strconv.Quote(util.U64String(c.CodeHash).Hex())
		case "storage_hash":
			res[i] = strconv.Quote(util.U64String(c.StorageHash).Hex())
		case "call_stats":
			res[i] = strconv.Quote(hex.EncodeToString(c.CallStats))
		case "features":
			res[i] = strconv.Quote(c.Features.String())
		case "interfaces":
			res[i] = strconv.Quote(c.Interfaces.String())
		default:
			continue
		}
	}
	return res, nil
}

func StreamContractTable(ctx *server.Context, args *TableRequest) (interface{}, int) {
	// use chain params at current height
	params := ctx.Params

	// access table
	table, err := ctx.Indexer.Table(args.Table)
	if err != nil {
		panic(server.ENotFound(server.EC_RESOURCE_NOTFOUND, fmt.Sprintf("cannot access table '%s'", args.Table), err))
	}

	// translate long column names to short names used in pack tables
	var srcNames []string
	if len(args.Columns) > 0 {
		// resolve short column names
		srcNames = make([]string, 0, len(args.Columns))
		for _, v := range args.Columns {
			n, ok := contractSourceNames[v]
			if !ok {
				panic(server.EBadRequest(server.EC_PARAM_INVALID, fmt.Sprintf("unknown column '%s'", v), nil))
			}
			if n == "-" {
				continue
			}
			switch v {
			case "first_seen_time":
				srcNames = append(srcNames, "f")
			case "last_seen_time":
				srcNames = append(srcNames, "l")
			}
			srcNames = append(srcNames, n)
		}
	} else {
		// use all table columns in order and reverse lookup their long names
		srcNames = table.Fields().Names()
		args.Columns = contractAllAliases
	}

	// build table query
	q := pack.NewQuery(ctx.RequestID).
		WithTable(table).
		WithFields(srcNames...).
		WithLimit(int(args.Limit)).
		WithOrder(args.Order)

	// build dynamic filter conditions from query (will panic on error)
	for key, val := range ctx.Request.URL.Query() {
		keys := strings.Split(key, ".")
		prefix := keys[0]
		mode := pack.FilterModeEqual
		field := contractSourceNames[prefix]
		if len(keys) > 1 {
			mode = pack.ParseFilterMode(keys[1])
			if !mode.IsValid() {
				panic(server.EBadRequest(server.EC_PARAM_INVALID, fmt.Sprintf("invalid filter mode '%s'", keys[1]), nil))
			}
		}
		switch prefix {
		case "columns", "limit", "order", "verbose", "filename":
			// skip these fields
		case "cursor":
			// add row id condition: id > cursor (new cursor == last row id)
			id, err := strconv.ParseUint(val[0], 10, 64)
			if err != nil {
				panic(server.EBadRequest(server.EC_PARAM_INVALID, fmt.Sprintf("invalid cursor value '%s'", val), err))
			}
			cursorMode := pack.FilterModeGt
			if args.Order == pack.OrderDesc {
				cursorMode = pack.FilterModeLt
			}
			q = q.And("I", cursorMode, id)
		case "address":
			switch mode {
			case pack.FilterModeEqual, pack.FilterModeNotEqual:
				// single-address lookup and compile condition
				addr, err := tezos.ParseAddress(val[0])
				if err != nil || !addr.IsValid() {
					panic(server.EBadRequest(server.EC_PARAM_INVALID, fmt.Sprintf("invalid address '%s'", val[0]), err))
				}
				if addr.Type() != tezos.AddressTypeContract {
					panic(server.EBadRequest(server.EC_PARAM_INVALID, fmt.Sprintf("invalid contract address '%s'", val[0]), err))
				}
				q = q.And(field, mode, addr[:])
			case pack.FilterModeIn, pack.FilterModeNotIn:
				// multi-address lookup (Note: does not check for address type so may
				// return duplicates)
				hashes := make([][]byte, 0)
				for _, v := range strings.Split(val[0], ",") {
					addr, err := tezos.ParseAddress(v)
					if err != nil || !addr.IsValid() {
						panic(server.EBadRequest(server.EC_PARAM_INVALID, fmt.Sprintf("invalid address '%s'", v), err))
					}
					if addr.Type() != tezos.AddressTypeContract {
						panic(server.EBadRequest(server.EC_PARAM_INVALID, fmt.Sprintf("invalid contract address '%s'", v), err))
					}
					hashes = append(hashes, addr[:])
				}
				q = q.And(field, mode, hashes)
			default:
				panic(server.EBadRequest(server.EC_PARAM_INVALID, fmt.Sprintf("invalid filter mode '%s' for column '%s'", mode, prefix), nil))
			}
		case "first_seen_time", "last_seen_time":
			// translate time into height, use val[0] only
			bestTime := ctx.Tip.BestTime
			bestHeight := ctx.Tip.BestHeight
			cond, err := pack.ParseCondition(key, val[0], pack.FieldList{
				pack.Field{
					Name: prefix,
					Type: pack.FieldTypeDatetime,
				},
			})
			if err != nil {
				panic(server.EBadRequest(server.EC_PARAM_INVALID, fmt.Sprintf("invalid %s filter value '%s'", key, val[0]), err))
			}
			// re-use the block height -> time slice because it's already loaded
			// into memory, the binary search should be faster than a block query
			switch cond.Mode {
			case pack.FilterModeRange:
				// use cond.From and con.To
				from, to := cond.From.(time.Time), cond.To.(time.Time)
				var fromBlock, toBlock int64
				if !from.After(bestTime) {
					fromBlock = ctx.Indexer.LookupBlockHeightFromTime(ctx.Context, from)
				} else {
					nDiff := int64(from.Sub(bestTime) / params.BlockTime())
					fromBlock = bestHeight + nDiff
				}
				if !to.After(bestTime) {
					toBlock = ctx.Indexer.LookupBlockHeightFromTime(ctx.Context, to)
				} else {
					nDiff := int64(to.Sub(bestTime) / params.BlockTime())
					toBlock = bestHeight + nDiff
				}
				q = q.AndRange(field, fromBlock, toBlock)
			default:
				// cond.Value is time.Time
				valueTime := cond.Value.(time.Time)
				var valueBlock int64
				if !valueTime.After(bestTime) {
					valueBlock = ctx.Indexer.LookupBlockHeightFromTime(ctx.Context, valueTime)
				} else {
					nDiff := int64(valueTime.Sub(bestTime) / params.BlockTime())
					valueBlock = bestHeight + nDiff
				}
				q = q.And(field, cond.Mode, valueBlock)
			}

		case "creator":
			// parse address and lookup id
			// valid filter modes: eq, in
			// 1 resolve account_id from account table
			// 2 add eq/in cond: account_id
			// 3 cache result in map (for output)
			switch mode {
			case pack.FilterModeEqual, pack.FilterModeNotEqual:
				// single-account lookup and compile condition
				addr, err := tezos.ParseAddress(val[0])
				if err != nil || !addr.IsValid() {
					panic(server.EBadRequest(server.EC_PARAM_INVALID, fmt.Sprintf("invalid address '%s'", val[0]), err))
				}
				acc, err := ctx.Indexer.LookupAccount(ctx, addr)
				if err != nil && err != model.ErrNoAccount {
					panic(server.EBadRequest(server.EC_PARAM_INVALID, fmt.Sprintf("invalid address '%s'", val[0]), err))
				}
				// Note: when not found we insert an always false condition
				if acc == nil || acc.RowId == 0 {
					q = q.And(field, mode, uint64(math.MaxUint64))
				} else {
					// add id as extra condition
					q = q.And(field, mode, acc.RowId)
				}
			case pack.FilterModeIn, pack.FilterModeNotIn:
				// multi-address lookup and compile condition
				ids := make([]uint64, 0)
				for _, v := range strings.Split(val[0], ",") {
					addr, err := tezos.ParseAddress(v)
					if err != nil || !addr.IsValid() {
						panic(server.EBadRequest(server.EC_PARAM_INVALID, fmt.Sprintf("invalid address '%s'", v), err))
					}
					acc, err := ctx.Indexer.LookupAccount(ctx, addr)
					if err != nil && err != model.ErrNoAccount {
						panic(server.EBadRequest(server.EC_PARAM_INVALID, fmt.Sprintf("invalid address '%s'", v), err))
					}
					// skip not found account
					if acc == nil || acc.RowId == 0 {
						continue
					}
					// collect list of account ids
					ids = append(ids, acc.RowId.U64())
				}
				// Note: when list is empty (no accounts were found, the match will
				//       always be false and return no result as expected)
				q = q.And(contractSourceNames[prefix], mode, ids)
			default:
				panic(server.EBadRequest(server.EC_PARAM_INVALID, fmt.Sprintf("invalid filter mode '%s' for column '%s'", mode, prefix), nil))
			}
		case "iface_hash", "code_hash", "storage_hash":
			switch mode {
			case pack.FilterModeEqual, pack.FilterModeNotEqual:
				u, err := util.DecodeU64String(val[0])
				if err != nil {
					panic(server.EBadRequest(server.EC_PARAM_INVALID, fmt.Sprintf("invalid hash '%s'", val[0]), err))
				}
				q = q.And(field, mode, u)
			case pack.FilterModeIn, pack.FilterModeNotIn:
				// multi-hash lookup
				hashes := make([]uint64, 0)
				for _, v := range strings.Split(val[0], ",") {
					u, err := util.DecodeU64String(v)
					if err != nil {
						panic(server.EBadRequest(server.EC_PARAM_INVALID, fmt.Sprintf("invalid hash '%s'", v), err))
					}
					hashes = append(hashes, u.U64())
				}
				q = q.And(field, mode, hashes)
			default:
				panic(server.EBadRequest(server.EC_PARAM_INVALID, fmt.Sprintf("invalid filter mode '%s' for column '%s'", mode, prefix), nil))
			}

		default:
			// translate long column name used in query to short column name used in packs
			if short, ok := contractSourceNames[prefix]; !ok {
				panic(server.EBadRequest(server.EC_PARAM_INVALID, fmt.Sprintf("unknown column '%s'", prefix), nil))
			} else {
				key = strings.Replace(key, prefix, short, 1)
			}

			// the same field name may appear multiple times, in which case conditions
			// are combined like any other condition with logical AND
			for _, v := range val {
				if prefix == "storage_burn" {
					fvals := make([]string, 0)
					for _, vv := range strings.Split(v, ",") {
						fval, err := strconv.ParseFloat(vv, 64)
						if err != nil {
							panic(server.EBadRequest(server.EC_PARAM_INVALID, fmt.Sprintf("invalid %s filter value '%s'", key, vv), err))
						}
						fvals = append(fvals, strconv.FormatInt(params.ConvertAmount(fval), 10))
					}
					v = strings.Join(fvals, ",")
				}
				if cond, err := pack.ParseCondition(key, v, table.Fields()); err != nil {
					panic(server.EBadRequest(server.EC_PARAM_INVALID, fmt.Sprintf("invalid %s filter value '%s'", key, v), err))
				} else {
					q = q.AndCondition(cond)
				}
			}
		}
	}

	var (
		count  int
		lastId uint64
	)

	// start := time.Now()
	// ctx.Log.Tracef("Streaming max %d rows from %s", args.Limit, args.Table)
	// defer func() {
	// 	ctx.Log.Tracef("Streamed %d rows in %s", count, time.Since(start))
	// }()

	// Step 1: query database
	res, err := table.Query(ctx, q)
	if err != nil {
		panic(server.EInternal(server.EC_DATABASE, "query failed", err))
	}
	// ctx.Log.Tracef("Processing result with %d rows %d cols", res.Rows(), res.Cols())
	defer res.Close()

	// prepare return type marshalling
	contract := &Contract{
		verbose: args.Verbose,
		columns: args.Columns,
		params:  params,
		ctx:     ctx,
	}

	// prepare response stream
	ctx.StreamResponseHeaders(http.StatusOK, mimetypes[args.Format])

	switch args.Format {
	case "json":
		enc := json.NewEncoder(ctx.ResponseWriter)
		enc.SetIndent("", "")
		enc.SetEscapeHTML(false)

		// open JSON array
		_, _ = io.WriteString(ctx.ResponseWriter, "[")
		// close JSON array on panic
		defer func() {
			if e := recover(); e != nil {
				_, _ = io.WriteString(ctx.ResponseWriter, "]")
				panic(e)
			}
		}()

		// run query and stream results
		var needComma bool
		err = res.Walk(func(r pack.Row) error {
			if needComma {
				_, _ = io.WriteString(ctx.ResponseWriter, ",")
			} else {
				needComma = true
			}
			contract.Contract.Reset()
			if err := r.Decode(contract); err != nil {
				return err
			}
			if err := enc.Encode(contract); err != nil {
				return err
			}
			count++
			lastId = contract.RowId.U64()
			if args.Limit > 0 && count == int(args.Limit) {
				return io.EOF
			}
			return nil
		})
		// close JSON bracket
		_, _ = io.WriteString(ctx.ResponseWriter, "]")
		// ctx.Log.Tracef("JSON encoded %d rows", count)

	case "csv":
		enc := csv.NewEncoder(ctx.ResponseWriter)
		// use custom header columns and order
		if len(args.Columns) > 0 {
			err = enc.EncodeHeader(args.Columns, nil)
		}
		if err == nil {
			// run query and stream results
			err = res.Walk(func(r pack.Row) error {
				contract.Contract.Reset()
				if err := r.Decode(contract); err != nil {
					return err
				}
				if err := enc.EncodeRecord(contract); err != nil {
					return err
				}
				count++
				lastId = contract.RowId.U64()
				if args.Limit > 0 && count == int(args.Limit) {
					return io.EOF
				}
				return nil
			})
		}
		// ctx.Log.Tracef("CSV Encoded %d rows", count)
	}

	// without new records, cursor remains the same as input (may be empty)
	cursor := args.Cursor
	if lastId > 0 {
		cursor = strconv.FormatUint(lastId, 10)
	}

	// write error (except EOF), cursor and count as http trailer
	ctx.StreamTrailer(cursor, count, err)

	// streaming return
	return nil, -1
}
