package secfilings

import (
	"encoding/json"
	"errors"
	"fmt"
	"fonchain-fiee/api/secFilings"
	"fonchain-fiee/pkg/model/login"
	"fonchain-fiee/pkg/service"
	"net/http"
	"net/url"

	"github.com/gin-gonic/gin"
	"github.com/gin-gonic/gin/binding"
)

func CreateSecFiling(c *gin.Context) {
	var req secFilings.CreateSecFilingReq
	if err := c.ShouldBindBodyWith(&req, binding.JSON); err != nil {
		service.Error(c, err)
		return
	}
	userInfo := login.GetUserInfoFromC(c)
	req.Operator = userInfo.Name
	req.OperatorId = int32(userInfo.ID)
	res, err := service.SecFilingProvider.CreateSecFiling(c, &req)
	if err != nil {
		service.Error(c, err)
		return
	}
	service.Success(c, res)
}
func UpdateSecFiling(c *gin.Context) {
	var req secFilings.UpdateSecFilingReq
	if err := c.ShouldBindBodyWith(&req, binding.JSON); err != nil {
		service.Error(c, err)
		return
	}
	userInfo := login.GetUserInfoFromC(c)
	req.Operator = userInfo.Name
	req.OperatorId = int32(userInfo.ID)
	res, err := service.SecFilingProvider.UpdateSecFiling(c, &req)
	if err != nil {
		service.Error(c, err)
		return
	}
	service.Success(c, res)
}
func UpdateSecFilingStatus(c *gin.Context) {
	var req secFilings.UpdateSecFilingStatusReq
	if err := c.ShouldBindBodyWith(&req, binding.JSON); err != nil {
		service.Error(c, err)
		return
	}
	userInfo := login.GetUserInfoFromC(c)
	req.Operator = userInfo.Name
	req.OperatorId = int32(userInfo.ID)
	res, err := service.SecFilingProvider.UpdateSecFilingStatus(c, &req)
	if err != nil {
		service.Error(c, err)
		return
	}
	service.Success(c, res)
}
func ListSecFiling(c *gin.Context) {
	var req secFilings.ListSecFilingReq
	if err := c.ShouldBindBodyWith(&req, binding.JSON); err != nil {
		service.Error(c, err)
		return
	}
	res, err := service.SecFilingProvider.ListSecFiling(c, &req)
	if err != nil {
		service.Error(c, err)
		return
	}
	service.Success(c, res)
}
func DetailSecFiling(c *gin.Context) {
	var req secFilings.DetailSecFilingReq
	if err := c.ShouldBindBodyWith(&req, binding.JSON); err != nil {
		service.Error(c, err)
		return
	}
	res, err := service.SecFilingProvider.DetailSecFiling(c, &req)
	if err != nil {
		service.Error(c, err)
		return
	}
	service.Success(c, res)
}
func DeleteSecFiling(c *gin.Context) {
	var req secFilings.DeleteSecFilingReq
	if err := c.ShouldBindBodyWith(&req, binding.JSON); err != nil {
		service.Error(c, err)
		return
	}
	userInfo := login.GetUserInfoFromC(c)
	req.Operator = userInfo.Name
	req.OperatorId = int32(userInfo.ID)
	res, err := service.SecFilingProvider.DeleteSecFiling(c, &req)
	if err != nil {
		service.Error(c, err)
		return
	}
	service.Success(c, res)
}

type GetSecFilingsReq struct {
	FilingDate string `json:"filingDate"`
	Page       string `json:"page"`
	Limit      string `json:"limit"`
}
type GetSecFilingsData struct {
	Symbol       string `json:"symbol"`
	Cik          string `json:"cik"`
	FilingDate   string `json:"filingDate"`
	AcceptedDate string `json:"acceptedDate"`
	FormType     string `json:"formType"`
	Link         string `json:"link"`
	FinalLink    string `json:"finalLink"`
	IsSelect     bool   `json:"isSelect"`
}
type GetSecFilingsResp struct {
	//Symbol       string `json:"symbol"`
	//Cik          string `json:"cik"`
	FilingDate string `json:"filingDate"`
	Form       string `json:"form"`
	//Description     string `json:"description"`
	//FormDescription string `json:"formDescription"`
	FileLink string `json:"fileLink"`
	IsSelect bool   `json:"isSelect"`
}

func GetSecFilings(c *gin.Context) {
	var req GetSecFilingsReq
	if err := c.ShouldBindBodyWith(&req, binding.JSON); err != nil {
		service.Error(c, err)
		return
	}
	if req.FilingDate == "" {
		service.Error(c, errors.New("filing_date is empty"))
		return
	}
	fmt.Println(req.FilingDate)
	symbol := "MINM"
	beginDate := req.FilingDate
	endDate := req.FilingDate
	page := "0"
	limit := "100"
	if req.Page != "" {
		page = req.Page
	}
	if req.Limit != "" {
		limit = req.Limit
	}
	baseURL := "https://financialmodelingprep.com/stable/sec-filings-search/symbol"
	params := url.Values{}
	params.Add("symbol", symbol)
	params.Add("from", beginDate)
	params.Add("to", endDate)
	params.Add("page", page)
	params.Add("limit", limit)
	params.Add("apikey", "lBQWUwdjRmfHVZ7e3I1trWe4jw0ZtDct")

	fullURL := fmt.Sprintf("%s?%s", baseURL, params.Encode())
	resp, err := http.Get(fullURL)
	if err != nil {
		service.Error(c, err)
		return
	}
	defer resp.Body.Close()
	if resp.StatusCode != http.StatusOK {
		service.Error(c, fmt.Errorf("http status code is not 200"))
		return
	}
	var result []GetSecFilingsData
	err = json.NewDecoder(resp.Body).Decode(&result)
	if err != nil {
		service.Error(c, err)
		return
	}
	var data []*GetSecFilingsResp
	fmt.Println(122222)
	for _, v := range result {
		res, err := service.SecFilingProvider.IsSecFilingExist(c, &secFilings.IsSecFilingExistReq{
			FilingDate: req.FilingDate,
			Form:       v.FormType,
			FinalLink:  v.FinalLink,
		})
		if err != nil {
			service.Error(c, err)
			return
		}
		data = append(data, &GetSecFilingsResp{
			FilingDate: req.FilingDate,
			Form:       v.FormType,
			FileLink:   v.FinalLink,
			IsSelect:   res.Exist,
		})
	}
	service.Success(c, data)
}
