package sec

import (
	"bytes"
	"encoding/json"
	"fmt"
	"log"
	"net/http"
	"net/http/httputil"
)

type AutoGenerated struct {
	Took     int        `json:"took"`
	TimedOut bool       `json:"timed_out"`
	Shards   Shards     `json:"_shards"`
	Hits     HitsParent `json:"hits"`
	// Query    Query      `json:"query"`
}
type Shards struct {
	Total      int `json:"total"`
	Successful int `json:"successful"`
	Skipped    int `json:"skipped"`
	Failed     int `json:"failed"`
}
type Total struct {
	Value    int    `json:"value"`
	Relation string `json:"relation"`
}
type Source struct {
	Entity      string `json:"entity"`
	EntityWords string `json:"entity_words"`
	Tickers     string `json:"tickers"`
	Rank        int    `json:"rank"`
}

type Hits struct {
	Index  string  `json:"_index"`
	Type   string  `json:"_type"`
	ID     string  `json:"_id"`
	Score  float64 `json:"_score"`
	Source Source  `json:"_source,omitempty"`
}

type HitsParent struct {
	Total    Total   `json:"total"`
	MaxScore float64 `json:"max_score"`
	Hits     []Hits  `json:"hits"`
}
type Entity struct {
	Query    string `json:"query"`
	Operator string `json:"operator"`
}
type Match struct {
	Entity Entity `json:"entity"`
}
type Tickers struct {
	Query    string `json:"query"`
	Operator string `json:"operator"`
	Boost    int    `json:"boost"`
}
type Exists struct {
	Field string `json:"field"`
	Boost int    `json:"boost"`
}
type Should struct {
	Match  Match  `json:"match,omitempty"`
	Match0 Match0 `json:"match,omitempty"`
	Exists Exists `json:"exists,omitempty"`
}
type ID struct {
	Query string `json:"query"`
}
type Match0 struct {
	ID ID `json:"_id"`
}
type Bool struct {
	Should []Should `json:"should"`
}
type Must struct {
	Bool Bool `json:"bool"`
}

// make post request to https://efts.sec.gov/LATEST/search-index
func SearchIndex(company string) {
	url := "https://efts.sec.gov/LATEST/search-index"
	values := map[string]string{"keysTyped": company, "narrow": "true"}
	jsonValue, _ := json.Marshal(values)
	req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonValue))
	if err != nil {
		// Adjust error handling here to meet application requrirements.
		fmt.Println("Error: ", err)
		log.Fatal(err)
	}
	req.Header.Set("Accept", "application/x-www-form-urlencoded; charset=UTF-8")
	// authority: efts.sec.gov
	req.Header.Set("Authority", "efts.sec.gov")
	req.Header.Set("origin", "https://www.sec.gov")
	req.Header.Set("referer", "https://www.sec.gov/edgar/searchedgar/companysearch.html")
	req.Header.Set("User-Agent", "Sample dli-invest dlcoding@gmail.com")
	req.Header.Set("Accept-Encoding", "gzip, deflate")
	req.Header.Set("Host", "www.sec.gov")
	defer req.Body.Close()

	client := &http.Client{}
	resp, err := client.Do(req)
	if err != nil {
		// Adjust error handling here to meet application requrirements.
		fmt.Println("Error: ", err)
		log.Fatal(err)
	}

	b, err := httputil.DumpResponse(resp, true)

	var response AutoGenerated
	// print resp body
	fmt.Println(string(b))
	json.Unmarshal(b, &response)
	// fmt.Println(response)
}
