package reports

import (
	"context"

	"github.com/shopspring/decimal"
)

// BreakdownFilter carries pagination, search, date range, and tenant store scoping.
type BreakdownFilter struct {
	Page     int
	PerPage  int
	Search   string
	DateFrom string
	DateTo   string
	StoreIDs []int64
}

// SalesByCustomerRow represents aggregated customer sales metrics.
type SalesByCustomerRow struct {
	CustomerCode string          `json:"customer_code"`
	CustomerName string          `json:"customer_name"`
	Orders       int             `json:"orders"`
	Qty          decimal.Decimal `json:"qty"`
	Omset        decimal.Decimal `json:"omset"`
	Discount     decimal.Decimal `json:"discount"`
	HPP          decimal.Decimal `json:"hpp"`
	Profit       decimal.Decimal `json:"profit"`
}

// SalesByProductRow represents aggregated product sales and returns.
type SalesByProductRow struct {
	ProductCode string          `json:"product_code"`
	ProductName string          `json:"product_name"`
	Qty         decimal.Decimal `json:"qty"`
	Omset       decimal.Decimal `json:"omset"`
	ReturQty    decimal.Decimal `json:"retur_qty"`
	ReturAmount decimal.Decimal `json:"retur_amount"`
}

// PurchasesBySupplierRow represents aggregated supplier purchasing metrics.
type PurchasesBySupplierRow struct {
	SupplierName string          `json:"supplier_name"`
	Orders       int             `json:"orders"`
	Qty          decimal.Decimal `json:"qty"`
	Total        decimal.Decimal `json:"total"`
	Paid         decimal.Decimal `json:"paid"`
	Remaining    decimal.Decimal `json:"remaining"`
}

// PurchasesByProductRow represents aggregated product purchase orders and receipts.
type PurchasesByProductRow struct {
	ProductCode string          `json:"product_code"`
	ProductName string          `json:"product_name"`
	Qty         decimal.Decimal `json:"qty"`
	Received    decimal.Decimal `json:"received"`
	Total       decimal.Decimal `json:"total"`
}

// AgingFilter carries pagination and search for receivables/payables aging.
type AgingFilter struct {
	Page    int
	PerPage int
	Search  string
}

// AgingRow represents an open bill with age in days and aging bucket.
type AgingRow struct {
	Number    string          `json:"number"`
	Party     string          `json:"party"`
	BillDate  string          `json:"bill_date"`
	AgeDays   int             `json:"age_days"`
	Bucket    string          `json:"bucket"`
	Total     decimal.Decimal `json:"total"`
	Remaining decimal.Decimal `json:"remaining"`
}

// StockValuationFilter carries pagination, search, and optional warehouse scoping.
type StockValuationFilter struct {
	Page         int
	PerPage      int
	Search       string
	WarehouseIDs []int64
}

// StockValuationRow represents warehouse stock valuation at cost and sell price.
type StockValuationRow struct {
	ProductCode string          `json:"product_code"`
	ProductName string          `json:"product_name"`
	WarehouseID int64           `json:"warehouse_id"`
	Qty         decimal.Decimal `json:"qty"`
	AvgCost     decimal.Decimal `json:"avg_cost"`
	Value       decimal.Decimal `json:"value"`
	SellPrice   decimal.Decimal `json:"sell_price"`
}

// StockMovementsFilter carries pagination, search, date range, and optional warehouse scoping.
type StockMovementsFilter struct {
	Page         int
	PerPage      int
	Search       string
	DateFrom     string
	DateTo       string
	WarehouseIDs []int64
}

// StockMovementRow represents a single inventory movement entry.
type StockMovementRow struct {
	Date        string          `json:"date"`
	Number      string          `json:"number"`
	Type        string          `json:"type"`
	ProductCode string          `json:"product_code"`
	ProductName string          `json:"product_name"`
	Qty         decimal.Decimal `json:"qty"`
	After       decimal.Decimal `json:"after"`
	Reference   string          `json:"reference"`
}

// StockCardFilter carries parameters to query the ledger/card of a single product.
type StockCardFilter struct {
	ProductID    int64
	WarehouseID  int64
	DateFrom     string
	DateTo       string
	WarehouseIDs []int64
}

// StockCardRow represents an entry in the product stock card ledger.
type StockCardRow struct {
	Date      string          `json:"date"`
	Number    string          `json:"number"`
	Type      string          `json:"type"`
	In        decimal.Decimal `json:"in"`
	Out       decimal.Decimal `json:"out"`
	Balance   decimal.Decimal `json:"balance"`
	Reference string          `json:"reference"`
}

// DetailedReportsStore extends Store with detailed breakdown and aging reporting queries.
type DetailedReportsStore interface {
	SalesByCustomer(ctx context.Context, f BreakdownFilter) ([]SalesByCustomerRow, int, error)
	SalesByProduct(ctx context.Context, f BreakdownFilter) ([]SalesByProductRow, int, error)
	PurchasesBySupplier(ctx context.Context, f BreakdownFilter) ([]PurchasesBySupplierRow, int, error)
	PurchasesByProduct(ctx context.Context, f BreakdownFilter) ([]PurchasesByProductRow, int, error)
	ReceivablesAging(ctx context.Context, f AgingFilter) ([]AgingRow, int, error)
	PayablesAging(ctx context.Context, f AgingFilter) ([]AgingRow, int, error)
	StockValuation(ctx context.Context, f StockValuationFilter) ([]StockValuationRow, int, error)
	StockMovements(ctx context.Context, f StockMovementsFilter) ([]StockMovementRow, int, error)
	StockCard(ctx context.Context, f StockCardFilter) ([]StockCardRow, error)
}
