package models

import (
	"time"

	"github.com/google/uuid"
)

// PaymentMethodAnalyticsRequest represents the request for payment method analytics
type PaymentMethodAnalyticsRequest struct {
	OrganizationID uuid.UUID  `validate:"required"`
	OutletID       *uuid.UUID `validate:"omitempty"`
	DateFrom       time.Time  `validate:"required"`
	DateTo         time.Time  `validate:"required"`
	GroupBy        string     `validate:"omitempty,oneof=day hour week month"`
}

// PaymentMethodAnalyticsResponse represents the response for payment method analytics
type PaymentMethodAnalyticsResponse struct {
	OrganizationID uuid.UUID                    `json:"organization_id"`
	OutletID       *uuid.UUID                   `json:"outlet_id,omitempty"`
	DateFrom       time.Time                    `json:"date_from"`
	DateTo         time.Time                    `json:"date_to"`
	GroupBy        string                       `json:"group_by"`
	Summary        PaymentMethodSummary         `json:"summary"`
	Data           []PaymentMethodAnalyticsData `json:"data"`
}

// PaymentMethodSummary represents the summary of payment method analytics
type PaymentMethodSummary struct {
	TotalAmount       float64 `json:"total_amount"`
	TotalOrders       int64   `json:"total_orders"`
	TotalPayments     int64   `json:"total_payments"`
	AverageOrderValue float64 `json:"average_order_value"`
}

// PaymentMethodAnalyticsData represents individual payment method analytics data
type PaymentMethodAnalyticsData struct {
	PaymentMethodID   uuid.UUID `json:"payment_method_id"`
	PaymentMethodName string    `json:"payment_method_name"`
	PaymentMethodType string    `json:"payment_method_type"`
	TotalAmount       float64   `json:"total_amount"`
	OrderCount        int64     `json:"order_count"`
	PaymentCount      int64     `json:"payment_count"`
	Percentage        float64   `json:"percentage"`
}

// SalesAnalyticsRequest represents the request for sales analytics
type SalesAnalyticsRequest struct {
	OrganizationID uuid.UUID  `validate:"required"`
	OutletID       *uuid.UUID `validate:"omitempty"`
	DateFrom       time.Time  `validate:"required"`
	DateTo         time.Time  `validate:"required"`
	GroupBy        string     `validate:"omitempty,oneof=day hour week month"`
}

// SalesAnalyticsResponse represents the response for sales analytics
type SalesAnalyticsResponse struct {
	OrganizationID uuid.UUID            `json:"organization_id"`
	OutletID       *uuid.UUID           `json:"outlet_id,omitempty"`
	DateFrom       time.Time            `json:"date_from"`
	DateTo         time.Time            `json:"date_to"`
	GroupBy        string               `json:"group_by"`
	Summary        SalesSummary         `json:"summary"`
	Data           []SalesAnalyticsData `json:"data"`
}

// SalesSummary represents the summary of sales analytics
type SalesSummary struct {
	TotalSales        float64 `json:"total_sales"`
	TotalOrders       int64   `json:"total_orders"`
	TotalItems        int64   `json:"total_items"`
	AverageOrderValue float64 `json:"average_order_value"`
	TotalTax          float64 `json:"total_tax"`
	TotalDiscount     float64 `json:"total_discount"`
	NetSales          float64 `json:"net_sales"`
}

// SalesAnalyticsData represents individual sales analytics data point
type SalesAnalyticsData struct {
	Date     time.Time `json:"date"`
	Sales    float64   `json:"sales"`
	Orders   int64     `json:"orders"`
	Items    int64     `json:"items"`
	Tax      float64   `json:"tax"`
	Discount float64   `json:"discount"`
	NetSales float64   `json:"net_sales"`
}

// ProductAnalyticsRequest represents the request for product analytics
type ProductAnalyticsRequest struct {
	OrganizationID uuid.UUID  `validate:"required"`
	OutletID       *uuid.UUID `validate:"omitempty"`
	DateFrom       time.Time  `validate:"required"`
	DateTo         time.Time  `validate:"required"`
	Limit          int        `validate:"min=1,max=100"`
}

// ProductAnalyticsResponse represents the response for product analytics
type ProductAnalyticsResponse struct {
	OrganizationID uuid.UUID              `json:"organization_id"`
	OutletID       *uuid.UUID             `json:"outlet_id,omitempty"`
	DateFrom       time.Time              `json:"date_from"`
	DateTo         time.Time              `json:"date_to"`
	Data           []ProductAnalyticsData `json:"data"`
}

// ProductAnalyticsData represents individual product analytics data
type ProductAnalyticsData struct {
	ProductID    uuid.UUID `json:"product_id"`
	ProductName  string    `json:"product_name"`
	CategoryID   uuid.UUID `json:"category_id"`
	CategoryName string    `json:"category_name"`
	QuantitySold int64     `json:"quantity_sold"`
	Revenue      float64   `json:"revenue"`
	AveragePrice float64   `json:"average_price"`
	OrderCount   int64     `json:"order_count"`
}

// ProductAnalyticsPerCategoryRequest represents the request for product analytics per category
type ProductAnalyticsPerCategoryRequest struct {
	OrganizationID uuid.UUID  `validate:"required"`
	OutletID       *uuid.UUID `validate:"omitempty"`
	DateFrom       time.Time  `validate:"required"`
	DateTo         time.Time  `validate:"required"`
}

// ProductAnalyticsPerCategoryResponse represents the response for product analytics per category
type ProductAnalyticsPerCategoryResponse struct {
	OrganizationID uuid.UUID                           `json:"organization_id"`
	OutletID       *uuid.UUID                          `json:"outlet_id,omitempty"`
	DateFrom       time.Time                           `json:"date_from"`
	DateTo         time.Time                           `json:"date_to"`
	Data           []ProductAnalyticsPerCategoryData   `json:"data"`
}

// ProductAnalyticsPerCategoryData represents individual category analytics data
type ProductAnalyticsPerCategoryData struct {
	CategoryID   uuid.UUID `json:"category_id"`
	CategoryName string    `json:"category_name"`
	TotalRevenue float64   `json:"total_revenue"`
	TotalQuantity int64    `json:"total_quantity"`
	ProductCount int64     `json:"product_count"`
	OrderCount   int64     `json:"order_count"`
}

// DashboardAnalyticsRequest represents the request for dashboard analytics
type DashboardAnalyticsRequest struct {
	OrganizationID uuid.UUID  `validate:"required"`
	OutletID       *uuid.UUID `validate:"omitempty"`
	DateFrom       time.Time  `validate:"required"`
	DateTo         time.Time  `validate:"required"`
}

// DashboardAnalyticsResponse represents the response for dashboard analytics
type DashboardAnalyticsResponse struct {
	OrganizationID uuid.UUID                    `json:"organization_id"`
	OutletID       *uuid.UUID                   `json:"outlet_id,omitempty"`
	DateFrom       time.Time                    `json:"date_from"`
	DateTo         time.Time                    `json:"date_to"`
	Overview       DashboardOverview            `json:"overview"`
	TopProducts    []ProductAnalyticsData       `json:"top_products"`
	PaymentMethods []PaymentMethodAnalyticsData `json:"payment_methods"`
	RecentSales    []SalesAnalyticsData         `json:"recent_sales"`
}

// DashboardOverview represents the overview data for dashboard
type DashboardOverview struct {
	TotalSales        float64 `json:"total_sales"`
	TotalOrders       int64   `json:"total_orders"`
	AverageOrderValue float64 `json:"average_order_value"`
	TotalCustomers    int64   `json:"total_customers"`
	VoidedOrders      int64   `json:"voided_orders"`
	RefundedOrders    int64   `json:"refunded_orders"`
}

// ProfitLossAnalyticsRequest represents the request for profit and loss analytics
type ProfitLossAnalyticsRequest struct {
	OrganizationID uuid.UUID  `validate:"required"`
	OutletID       *uuid.UUID `validate:"omitempty"`
	DateFrom       time.Time  `validate:"required"`
	DateTo         time.Time  `validate:"required"`
	GroupBy        string     `validate:"omitempty,oneof=day hour week month"`
}

// ProfitLossAnalyticsResponse represents the response for profit and loss analytics
type ProfitLossAnalyticsResponse struct {
	OrganizationID uuid.UUID           `json:"organization_id"`
	OutletID       *uuid.UUID          `json:"outlet_id,omitempty"`
	DateFrom       time.Time           `json:"date_from"`
	DateTo         time.Time           `json:"date_to"`
	GroupBy        string              `json:"group_by"`
	Summary        ProfitLossSummary   `json:"summary"`
	Data           []ProfitLossData    `json:"data"`
	ProductData    []ProductProfitData `json:"product_data"`
}

// ProfitLossSummary represents the summary of profit and loss analytics
type ProfitLossSummary struct {
	TotalRevenue       float64 `json:"total_revenue"`
	TotalCost          float64 `json:"total_cost"`
	GrossProfit        float64 `json:"gross_profit"`
	GrossProfitMargin  float64 `json:"gross_profit_margin"`
	TotalTax           float64 `json:"total_tax"`
	TotalDiscount      float64 `json:"total_discount"`
	NetProfit          float64 `json:"net_profit"`
	NetProfitMargin    float64 `json:"net_profit_margin"`
	TotalOrders        int64   `json:"total_orders"`
	AverageProfit      float64 `json:"average_profit"`
	ProfitabilityRatio float64 `json:"profitability_ratio"`
}

// ProfitLossData represents individual profit and loss data point by time period
type ProfitLossData struct {
	Date              time.Time `json:"date"`
	Revenue           float64   `json:"revenue"`
	Cost              float64   `json:"cost"`
	GrossProfit       float64   `json:"gross_profit"`
	GrossProfitMargin float64   `json:"gross_profit_margin"`
	Tax               float64   `json:"tax"`
	Discount          float64   `json:"discount"`
	NetProfit         float64   `json:"net_profit"`
	NetProfitMargin   float64   `json:"net_profit_margin"`
	Orders            int64     `json:"orders"`
}

// ProductProfitData represents profit data for individual products
type ProductProfitData struct {
	ProductID         uuid.UUID `json:"product_id"`
	ProductName       string    `json:"product_name"`
	CategoryID        uuid.UUID `json:"category_id"`
	CategoryName      string    `json:"category_name"`
	QuantitySold      int64     `json:"quantity_sold"`
	Revenue           float64   `json:"revenue"`
	Cost              float64   `json:"cost"`
	GrossProfit       float64   `json:"gross_profit"`
	GrossProfitMargin float64   `json:"gross_profit_margin"`
	AveragePrice      float64   `json:"average_price"`
	AverageCost       float64   `json:"average_cost"`
	ProfitPerUnit     float64   `json:"profit_per_unit"`
}
