package models

import (
	"gorm.io/gorm"
	"time"

	"github.com/google/uuid"
)

// Equipment équipement musical dans la base de données
type Equipment struct {
	ID             uuid.UUID              `json:"id" gorm:"type:uuid;primaryKey"`
	UserID         uuid.UUID              `json:"user_id" gorm:"type:uuid;not null;index"`
	Title          string                 `json:"title" gorm:"not null"`
	Description    string                 `json:"description" gorm:"not null"`
	EquipmentType  string                 `json:"equipment_type" gorm:"not null;index"`
	Brand          string                 `json:"brand" gorm:"not null;index"`
	Model          string                 `json:"model" gorm:"not null"`
	Year           *int                   `json:"year,omitempty"`
	Condition      string                 `json:"condition" gorm:"not null"`
	Price          float64                `json:"price" gorm:"not null"`
	Currency       string                 `json:"currency" gorm:"not null;default:'EUR'"`
	Location       string                 `json:"location" gorm:"not null"`
	Images         []string               `json:"images" gorm:"type:jsonb"`
	Specifications map[string]interface{} `json:"specifications" gorm:"type:jsonb"`
	IsForSale      bool                   `json:"is_for_sale" gorm:"not null;default:false"`
	IsForTrade     bool                   `json:"is_for_trade" gorm:"not null;default:false"`
	Status         string                 `json:"status" gorm:"not null;default:'active'"`
	ShippingInfo   *ShippingInfo          `json:"shipping_info" gorm:"type:jsonb"`
	Warranty       *WarrantyInfo          `json:"warranty" gorm:"type:jsonb"`
	Views          int64                  `json:"views" gorm:"not null;default:0"`
	Favorites      int64                  `json:"favorites" gorm:"not null;default:0"`
	CreatedAt      time.Time              `json:"created_at" gorm:"autoCreateTime"`
	UpdatedAt      time.Time              `json:"updated_at" gorm:"autoUpdateTime"`
}

// HardwareSale vente d'équipement
type HardwareSale struct {
	ID              uuid.UUID  `json:"id" gorm:"type:uuid;primaryKey"`
	EquipmentID     uuid.UUID  `json:"equipment_id" gorm:"type:uuid;not null;index"`
	SellerID        uuid.UUID  `json:"seller_id" gorm:"type:uuid;not null;index"`
	BuyerID         uuid.UUID  `json:"buyer_id" gorm:"type:uuid;not null;index"`
	Price           float64    `json:"price" gorm:"not null"`
	Currency        string     `json:"currency" gorm:"not null;default:'EUR'"`
	PaymentMethod   string     `json:"payment_method" gorm:"not null"`
	ShippingAddress *Address   `json:"shipping_address" gorm:"type:jsonb"`
	Status          string     `json:"status" gorm:"not null;default:'active'"`
	Notes           string     `json:"notes,omitempty"`
	TransactionID   string     `json:"transaction_id,omitempty"`
	ProcessedAt     *time.Time `json:"processed_at,omitempty"`
	CreatedAt       time.Time  `json:"created_at" gorm:"autoCreateTime"`
	UpdatedAt       time.Time  `json:"updated_at" gorm:"autoUpdateTime"`
}

// EquipmentTrade échange d'équipement
type EquipmentTrade struct {
	ID                   uuid.UUID  `json:"id" gorm:"type:uuid;primaryKey"`
	OfferedEquipmentID   uuid.UUID  `json:"offered_equipment_id" gorm:"type:uuid;not null;index"`
	RequestedEquipmentID uuid.UUID  `json:"requested_equipment_id" gorm:"type:uuid;not null;index"`
	OfferedByUserID      uuid.UUID  `json:"offered_by_user_id" gorm:"type:uuid;not null;index"`
	RequestedByUserID    uuid.UUID  `json:"requested_by_user_id" gorm:"type:uuid;not null;index"`
	Message              string     `json:"message,omitempty"`
	CashOffer            *float64   `json:"cash_offer,omitempty"`
	Status               string     `json:"status" gorm:"not null;default:'pending'"`
	AcceptedAt           *time.Time `json:"accepted_at,omitempty"`
	RejectedAt           *time.Time `json:"rejected_at,omitempty"`
	CreatedAt            time.Time  `json:"created_at" gorm:"autoCreateTime"`
	UpdatedAt            time.Time  `json:"updated_at" gorm:"autoUpdateTime"`
}

// HardwareOffer offre pour un équipement
type HardwareOffer struct {
	ID          uuid.UUID  `json:"id" gorm:"type:uuid;primaryKey"`
	EquipmentID uuid.UUID  `json:"equipment_id" gorm:"type:uuid;not null;index"`
	BuyerID     uuid.UUID  `json:"buyer_id" gorm:"type:uuid;not null;index"`
	OfferAmount float64    `json:"offer_amount" gorm:"not null"`
	Currency    string     `json:"currency" gorm:"not null;default:'EUR'"`
	Message     string     `json:"message,omitempty"`
	Status      string     `json:"status" gorm:"not null;default:'pending'"`
	ExpiresAt   *time.Time `json:"expires_at,omitempty"`
	AcceptedAt  *time.Time `json:"accepted_at,omitempty"`
	RejectedAt  *time.Time `json:"rejected_at,omitempty"`
	CreatedAt   time.Time  `json:"created_at" gorm:"autoCreateTime"`
	UpdatedAt   time.Time  `json:"updated_at" gorm:"autoUpdateTime"`
}

// Structures de données
type ShippingInfo struct {
	Method        string  `json:"method"`
	Cost          float64 `json:"cost"`
	Currency      string  `json:"currency"`
	EstimatedDays int     `json:"estimated_days"`
	Tracking      bool    `json:"tracking"`
}

type WarrantyInfo struct {
	Type        string     `json:"type"`
	Duration    int        `json:"duration"` // en mois
	Description string     `json:"description"`
	ExpiresAt   *time.Time `json:"expires_at,omitempty"`
}

type Address struct {
	Street     string `json:"street"`
	City       string `json:"city"`
	State      string `json:"state"`
	PostalCode string `json:"postal_code"`
	Country    string `json:"country"`
}

// TableName spécifie le nom de la table pour Equipment
func (Equipment) TableName() string {
	return "equipment"
}

// TableName spécifie le nom de la table pour HardwareSale
func (HardwareSale) TableName() string {
	return "hardware_sales"
}

// TableName spécifie le nom de la table pour EquipmentTrade
func (EquipmentTrade) TableName() string {
	return "equipment_trades"
}

// TableName spécifie le nom de la table pour HardwareOffer
func (HardwareOffer) TableName() string {
	return "hardware_offers"
}

// BeforeCreate hook GORM pour générer UUID si non défini
func (m *Equipment) BeforeCreate(tx *gorm.DB) error {
	if m.ID == uuid.Nil {
		m.ID = uuid.New()
	}
	return nil
}

// BeforeCreate hook GORM pour générer UUID si non défini
func (m *HardwareSale) BeforeCreate(tx *gorm.DB) error {
	if m.ID == uuid.Nil {
		m.ID = uuid.New()
	}
	return nil
}

// BeforeCreate hook GORM pour générer UUID si non défini
func (m *EquipmentTrade) BeforeCreate(tx *gorm.DB) error {
	if m.ID == uuid.Nil {
		m.ID = uuid.New()
	}
	return nil
}

// BeforeCreate hook GORM pour générer UUID si non défini
func (m *HardwareOffer) BeforeCreate(tx *gorm.DB) error {
	if m.ID == uuid.Nil {
		m.ID = uuid.New()
	}
	return nil
}
