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 scotia struct{}

func NewScotia() ports.APIPorts {
	return &scotia{}
}

func (sct scotia) Scrape(ctx context.Context, page playwright.Page, log *slog.Logger) (insts []*domain.History, err error) {
	tout := 120000.00
	log = log.With("scrapper", "scotia")
	if _, err := page.Goto(config.GetSCTAURL(), 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()

	firstRow := page.Locator(".bns--table > tbody:nth-child(1) > tr:nth-child(2)")
	secondRow := page.Locator(".bns--table > tbody:nth-child(1) > tr:nth-child(3)")

	// the same institution has 2 prices for dollar sell
	// first row is onsite selling dollars
	// second row is online selling dollars
	buyOnsite := firstRow.Locator("td:nth-child(3)")
	sellOnsite := firstRow.Locator("td:nth-child(4)")

	// the first row has 4 elements
	// but the second row only has 3
	buyOnline := secondRow.Locator("td:nth-child(2)")
	sellOnline := secondRow.Locator("td:nth-child(3)")

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

	ventaOnsiteSTR, err := sellOnsite.InnerText()
	if err != nil {
		log.Error("could not get venta string", "err", err)
		return nil, err
	}
	instOnsite := &domain.History{
		Name:   "scotiabank",
		Parser: "scotia",
		Parsed: time.Now().Unix(),
	}

	instOnsite.Venta = helpers.Normalize(ventaOnsiteSTR)
	instOnsite.Compra = helpers.Normalize(compraOnsiteSTR)
	if instOnsite.Compra == 0 || instOnsite.Venta == 0 {
		return nil, fmt.Errorf("scotia: institution not parsed: %v", instOnsite)
	}

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

	ventaOnlineSTR, err := sellOnline.InnerText()
	if err != nil {
		log.Error("could not get venta online string", "err", err)
		return nil, err
	}

	instOnline := &domain.History{
		Name:   "scotiabank cambio online",
		Parser: "scotia",
		Parsed: time.Now().Unix(),
	}
	instOnline.Venta = helpers.Normalize(ventaOnlineSTR)
	instOnline.Compra = helpers.Normalize(compraOnlineSTR)

	if instOnline.Compra == 0 || instOnline.Venta == 0 {
		return nil, fmt.Errorf("scotia: institution not parsed: %v", instOnline)
	}

	insts = append(insts, instOnline, instOnsite)
	return insts, nil
}
