package crawler

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

	"github.com/maximotejeda/us_dop_scrapper/config"
	"github.com/maximotejeda/us_dop_scrapper/helpers"
	"github.com/maximotejeda/us_dop_scrapper/internal/application/core/domain"
	"github.com/maximotejeda/us_dop_scrapper/internal/ports"
	"github.com/playwright-community/playwright-go"
)

type bcd struct{}

func NewBCD() ports.APIPorts {
	return &bcd{}
}

func (b bcd) Scrape(ctx context.Context, page playwright.Page, log *slog.Logger) (insts []*domain.History, err error) {
	log = log.With("scrapper", "bcd")
	tout := 90000.00
	if _, err = page.Goto(config.GetBCDURL(), playwright.PageGotoOptions{
		Timeout:   &tout,
		WaitUntil: playwright.WaitUntilStateLoad,
	}); err != nil {
		log.Error("could not get info", "error", err)
		return nil, err
	}
	compraLocator := page.Locator("span#actualPurchaseValue")

	compraLocator.WaitFor(playwright.LocatorWaitForOptions{
		Timeout: &tout,
		State:   playwright.WaitForSelectorStateVisible,
	})

	ventaLocator := page.Locator("span#actualSellingValue")

	compra, err := compraLocator.TextContent()
	if err != nil {
		log.Error("locating compra", "err", err)
		return nil, err
	}
	venta, err := ventaLocator.TextContent()
	if err != nil {
		log.Error("locating venta", "err", err)
		return nil, err
	}

	inst := &domain.History{
		Parser: "bcd",
		Name:   "banco central dominicano",
		Parsed: time.Now().Unix(),
	}

	inst.Compra = helpers.Normalize(compra)
	inst.Venta = helpers.Normalize(venta)

	if inst.Compra == 0 || inst.Venta == 0 {
		return nil, fmt.Errorf("bcd: institution not parsed compra or venta cant be 0")
	}
	return []*domain.History{inst}, nil
}
