package queries

import (
	"context"
	"database/sql"
)

/*
============================================================

	HEADER GETIRME — OrderHeader struct’ına uygun
	============================================================
*/
type OrderHeaderDB struct {
	OrderHeaderID string
	OrderNumber   string
	CurrAccCode   string
	CurrAccName   string
	DocCurrency   string
	OrderDate     sql.NullTime
	Description   sql.NullString
	InternalDesc  sql.NullString
	OfficeCode    sql.NullString
	CreatedUser   sql.NullString
}

func GetOrderHeaderDB(ctx context.Context, db *sql.DB, id string) (*OrderHeaderDB, error) {
	q := `
SELECT 
    CAST(h.OrderHeaderID AS varchar(36)),
    h.OrderNumber,
    h.CurrAccCode,
    d.CurrAccDescription,
    h.DocCurrencyCode,
    h.OrderDate,
    h.Description,
    h.InternalDescription,
    h.OfficeCode,
    h.CreatedUserName
FROM BAGGI_V3.dbo.trOrderHeader AS h
LEFT JOIN BAGGI_V3.dbo.cdCurrAccDesc AS d
    ON h.CurrAccCode = d.CurrAccCode
WHERE h.OrderHeaderID = @p1
`
	row := db.QueryRowContext(ctx, q, id)

	var h OrderHeaderDB
	err := row.Scan(
		&h.OrderHeaderID,
		&h.OrderNumber,
		&h.CurrAccCode,
		&h.CurrAccName,
		&h.DocCurrency,
		&h.OrderDate,
		&h.Description,
		&h.InternalDesc,
		&h.OfficeCode,
		&h.CreatedUser,
	)
	if err != nil {
		return nil, err
	}
	return &h, nil
}

/*
============================================================

	SATIRLARI GETIRME — OrderLineRaw struct’ına uygun
	============================================================
*/
type OrderLineRawDB struct {
	OrderLineID         sql.NullString
	ItemCode            string
	ColorCode           string
	ItemDim1Code        sql.NullString
	ItemDim2Code        sql.NullString
	Qty1                sql.NullFloat64
	Price               sql.NullFloat64
	DocCurrencyCode     sql.NullString
	DeliveryDate        sql.NullTime
	LineDescription     sql.NullString
	UrunAnaGrubu        sql.NullString
	UrunAltGrubu        sql.NullString
	IsClosed            sql.NullBool
	WithHoldingTaxType  sql.NullString
	DOVCode             sql.NullString
	PlannedDateOfLading sql.NullTime
	CostCenterCode      sql.NullString
	VatCode             sql.NullString
	VatRate             sql.NullFloat64
}

func GetOrderLinesDB(ctx context.Context, db *sql.DB, id string) ([]OrderLineRawDB, error) {

	q := `
SELECT
    CAST(L.OrderLineID AS varchar(36)),
    L.ItemCode,
    L.ColorCode,
    L.ItemDim1Code,
    L.ItemDim2Code,
    L.Qty1,
    ISNULL(CD.Price, 0) AS Price,
    ISNULL(CD.CurrencyCode, ISNULL(L.DocCurrencyCode, 'TRY')) AS DocCurrencyCode,
    L.DeliveryDate,
    L.LineDescription,
    P.ProductAtt01Desc,
    P.ProductAtt02Desc,
    L.IsClosed,
    L.WithHoldingTaxTypeCode,
    L.DOVCode,
    L.PlannedDateOfLading,
    L.CostCenterCode,
    L.VatCode,
    L.VatRate
FROM BAGGI_V3.dbo.trOrderLine AS L
LEFT JOIN BAGGI_V3.dbo.trOrderLineCurrency AS CD
    ON CD.OrderLineID = L.OrderLineID
    AND CD.CurrencyCode = ISNULL(NULLIF(LTRIM(RTRIM(L.DocCurrencyCode)), ''), 'TRY')
LEFT JOIN ProductFilterWithDescription('TR') AS P
    ON LTRIM(RTRIM(P.ProductCode)) = LTRIM(RTRIM(L.ItemCode))
WHERE L.OrderHeaderID = @p1
ORDER BY L.SortOrder, L.OrderLineID
`

	rows, err := db.QueryContext(ctx, q, id)
	if err != nil {
		return nil, err
	}
	defer rows.Close()

	var out []OrderLineRawDB

	for rows.Next() {
		var r OrderLineRawDB
		err = rows.Scan(
			&r.OrderLineID,
			&r.ItemCode,
			&r.ColorCode,
			&r.ItemDim1Code,
			&r.ItemDim2Code,
			&r.Qty1,
			&r.Price,
			&r.DocCurrencyCode,
			&r.DeliveryDate,
			&r.LineDescription,
			&r.UrunAnaGrubu,
			&r.UrunAltGrubu,
			&r.IsClosed,
			&r.WithHoldingTaxType,
			&r.DOVCode,
			&r.PlannedDateOfLading,
			&r.CostCenterCode,
			&r.VatCode,
			&r.VatRate,
		)
		if err != nil {
			return nil, err
		}
		out = append(out, r)
	}
	return out, nil
}
