package vimenca

import (
	"context"
	"log/slog"
	"os"
	"time"

	"github.com/maximotejeda/us_dop_db/db"
	"github.com/maximotejeda/us_dop_scrapper/helpers"
	"github.com/playwright-community/playwright-go"
)

var (
	uri = os.Getenv("VIMENCA")
)

func Scrape(ctx context.Context, page playwright.Page, log *slog.Logger) (inst *db.History, err error) {
	tout := 120000.00
	log = log.With("scrapper", "vimenca")
	if _, err := page.Goto(uri, playwright.PageGotoOptions{
		Timeout:   &tout,
		WaitUntil: playwright.WaitUntilStateLoad,
	}); err != nil {
		log.Error("could not get info", "error", err)
		return nil, err
	}

	currencyTable := page.Locator(".bns--table")
	currencyTable.WaitFor()

	infoContainer := page.Locator(".layout-uikit > div:nth-child(1)")

	buyInfo := infoContainer.Locator(".purchaseValue")
	sellInfo := infoContainer.Locator(".saleValue")

	compraSTR, err := buyInfo.InnerText()
	if err != nil {
		log.Error("could not get compra str", "err", err)
		return nil, err
	}

	ventaSTR, err := sellInfo.InnerText()
	if err != nil {
		log.Error("could not get venta string", "err", err)
		return nil, err
	}
	inst = &db.History{
		Name:   "banco vimenca",
		Parser: "vimenca",
		Parsed: time.Now().UTC(),
	}

	inst.Venta = helpers.Normalize(ventaSTR)
	inst.Compra = helpers.Normalize(compraSTR)
	log.Info("institution", "value", inst)
	return inst, nil
}

func ExecParser(
	ctx context.Context,
	db *db.DB,
	browser *playwright.Browser,
	log *slog.Logger) (err error) {
	t := true
	ua := helpers.NewMobileUA()
	b := *browser
	page, err := b.NewPage(playwright.BrowserNewPageOptions{
		UserAgent: &ua,
		//	IsMobile:  &t,
		HasTouch: &t,
		Viewport: &playwright.Size{
			Width:  412,
			Height: 915,
		},
		Screen: &playwright.Size{
			Width:  412,
			Height: 915,
		},
	})
	if err != nil {
		log.Error("creating page", "error", err)
		os.Exit(1)
	}
	ctx, cancel := context.WithTimeout(ctx, 6*time.Minute)
	defer page.Close()
	defer cancel()
	inst, err := Scrape(ctx, page, log)
	// here we execute db operations
	if err != nil {
		return err
	}

	err = db.Inspect(*inst)

	return err
}
