package queries

import (
	"bssapp-backend/db"
	"bssapp-backend/models"
	"context"
	"database/sql"
	"fmt"
	"strings"
)

// DETAIL (ALT TABLO)
func GetStatementDetails(ctx context.Context, belgeNo string) ([]models.StatementDetail, error) {
	query := `
SELECT
    CONVERT(varchar(10), a.InvoiceDate, 23) AS Belge_Tarihi,
    a.InvoiceNumber                         AS Belge_Ref_Numarasi,
    COALESCE(MAX(AnaGrupDesc.AttributeDescription), '')   AS Urun_Ana_Grubu,
    COALESCE(MAX(AltGrupDesc.AttributeDescription), '')   AS Urun_Alt_Grubu,
    COALESCE(MAX(GarsonDesc.AttributeDescription), '')    AS Yetiskin_Garson,
    COALESCE(MAX(FitDesc.AttributeDescription), '')       AS Fit,
    COALESCE(MAX(KisaKarDesc.AttributeDescription), '')   AS Icerik,
    a.ItemCode       AS Urun_Kodu,
    a.ColorCode      AS Urun_Rengi,
    SUM(a.Qty1)      AS Toplam_Adet,
    CAST(
        SUM(a.Qty1 * ABS(a.Doc_Price)) / NULLIF(SUM(a.Qty1), 0)
        AS numeric(18,2)
    ) AS Toplam_Fiyat,
    CAST(SUM(a.Qty1 * ABS(a.Doc_Price)) AS numeric(18,2)) AS Toplam_Tutar
FROM AllInvoicesWithAttributes a
LEFT JOIN prItemAttribute AnaGrup
    ON a.ItemCode = AnaGrup.ItemCode AND AnaGrup.AttributeTypeCode = 1
LEFT JOIN cdItemAttributeDesc AnaGrupDesc
    ON AnaGrup.AttributeTypeCode = AnaGrupDesc.AttributeTypeCode
   AND AnaGrup.AttributeCode     = AnaGrupDesc.AttributeCode
   AND AnaGrup.ItemTypeCode      = AnaGrupDesc.ItemTypeCode

LEFT JOIN prItemAttribute AltGrup
    ON a.ItemCode = AltGrup.ItemCode AND AltGrup.AttributeTypeCode = 2
LEFT JOIN cdItemAttributeDesc AltGrupDesc
    ON AltGrup.AttributeTypeCode = AltGrupDesc.AttributeTypeCode
   AND AltGrup.AttributeCode     = AltGrupDesc.AttributeCode
   AND AltGrup.ItemTypeCode      = AltGrupDesc.ItemTypeCode

LEFT JOIN prItemAttribute Garson
    ON a.ItemCode = Garson.ItemCode AND Garson.AttributeTypeCode = 44
LEFT JOIN cdItemAttributeDesc GarsonDesc
    ON Garson.AttributeTypeCode = GarsonDesc.AttributeTypeCode
   AND Garson.AttributeCode     = GarsonDesc.AttributeCode
   AND Garson.ItemTypeCode      = GarsonDesc.ItemTypeCode

LEFT JOIN prItemAttribute FitTbl
    ON a.ItemCode = FitTbl.ItemCode AND FitTbl.AttributeTypeCode = 38
LEFT JOIN cdItemAttributeDesc FitDesc
    ON FitTbl.AttributeTypeCode = FitDesc.AttributeTypeCode
   AND FitTbl.AttributeCode     = FitDesc.AttributeCode
   AND FitTbl.ItemTypeCode      = FitDesc.ItemTypeCode

LEFT JOIN prItemAttribute KisaKar
    ON a.ItemCode = KisaKar.ItemCode AND KisaKar.AttributeTypeCode = 41
LEFT JOIN cdItemAttributeDesc KisaKarDesc
    ON KisaKar.AttributeTypeCode = KisaKarDesc.AttributeTypeCode
   AND KisaKar.AttributeCode     = KisaKarDesc.AttributeCode
   AND KisaKar.ItemTypeCode      = KisaKarDesc.ItemTypeCode
WHERE LTRIM(RTRIM(a.InvoiceNumber)) = LTRIM(RTRIM(@BelgeNo))
GROUP BY a.InvoiceDate, a.InvoiceNumber, a.ItemCode, a.ColorCode
ORDER BY Belge_Tarihi, Belge_Ref_Numarasi, Urun_Kodu;`

	rows, err := db.MssqlDB.QueryContext(ctx, query,
		sql.Named("BelgeNo", strings.TrimSpace(belgeNo)),
	)
	if err != nil {
		return nil, fmt.Errorf("detay sorgu hatasi: %v", err)
	}
	defer rows.Close()

	var results []models.StatementDetail
	for rows.Next() {
		var d models.StatementDetail
		if err := rows.Scan(
			&d.BelgeTarihi,
			&d.BelgeRefNumarasi,
			&d.UrunAnaGrubu,
			&d.UrunAltGrubu,
			&d.YetiskinGarson,
			&d.Fit,
			&d.Icerik,
			&d.UrunKodu,
			&d.UrunRengi,
			&d.ToplamAdet,
			&d.ToplamFiyat,
			&d.ToplamTutar,
		); err != nil {
			return nil, err
		}
		results = append(results, d)
	}
	return results, nil
}
