package models

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

type LostItem struct {
	ID          uint           `gorm:"primaryKey" json:"id"`
	UserID      uint           `gorm:"not null" json:"user_id"`
	User        User           `gorm:"foreignKey:UserID" json:"user,omitempty"`
	Name        string         `gorm:"type:varchar(100);not null" json:"name"`
	CategoryID  uint           `gorm:"not null" json:"category_id"`
	Category    Category       `gorm:"foreignKey:CategoryID" json:"category,omitempty"`
	Color       string         `gorm:"type:varchar(50)" json:"color"`
	Location    string         `gorm:"type:varchar(200)" json:"location"`
	Description string         `gorm:"type:text;not null" json:"description"`
	DateLost    time.Time      `gorm:"not null" json:"date_lost"`
	Status      string         `gorm:"type:varchar(50);default:'active'" json:"status"`
	MatchedAt   *time.Time     `json:"matched_at"`
	
	// NEW: Direct claim fields
	DirectClaimID *uint      `json:"direct_claim_id"`
	DirectClaim   *Claim     `gorm:"foreignKey:DirectClaimID" json:"direct_claim,omitempty"`
	
	CreatedAt   time.Time      `json:"created_at"`
	UpdatedAt   time.Time      `json:"updated_at"`
	DeletedAt   gorm.DeletedAt `gorm:"index" json:"-"`
	MatchResults []MatchResult `gorm:"foreignKey:LostItemID" json:"match_results,omitempty"`
}

func (LostItem) TableName() string {
	return "lost_items"
}

const (
	LostItemStatusActive    = "active"
	LostItemStatusFound     = "found"
	LostItemStatusExpired   = "expired"
	LostItemStatusClosed    = "closed"
	LostItemStatusClaimed   = "claimed"    // NEW: Status ketika ada yang klaim langsung ke owner
	LostItemStatusCompleted = "completed"  // NEW: Status ketika owner confirm sudah terima barang
)

func (l *LostItem) BeforeCreate(tx *gorm.DB) error {
	if l.Status == "" {
		l.Status = LostItemStatusActive
	}
	return nil
}

func (l *LostItem) IsActive() bool {
	return l.Status == LostItemStatusActive
}

func (l *LostItem) IsClaimed() bool {
	return l.Status == LostItemStatusClaimed
}

type LostItemResponse struct {
	ID                uint           `json:"id"`
	UserID            uint           `json:"user_id"`
	UserName          string         `json:"user_name"`
	Name              string         `json:"name"`
	CategoryID        uint           `json:"category_id"`        // ✅ TAMBAHKAN INI
	Category          string         `json:"category"`
	Color             string         `json:"color"`
	Location          string         `json:"location"`
	Description       string         `json:"description"`
	DateLost          time.Time      `json:"date_lost"`
	Status            string         `json:"status"`
	CreatedAt         time.Time      `json:"created_at"`
	DirectClaimID     *uint          `json:"direct_claim_id,omitempty"`
	DirectClaimStatus string         `json:"direct_claim_status,omitempty"`
	DirectClaim       *ClaimResponse `json:"direct_claim,omitempty"` 
}

func (l *LostItem) ToResponse() LostItemResponse {
	userName := ""
	if l.User.ID != 0 {
		userName = l.User.Name
	}
	categoryName := ""
	if l.Category.ID != 0 {
		categoryName = l.Category.Name
	}
	directClaimStatus := ""
	var directClaimResp *ClaimResponse
	if l.DirectClaim != nil {
		directClaimStatus = l.DirectClaim.Status
		resp := l.DirectClaim.ToResponse()
		directClaimResp = &resp
	}
	return LostItemResponse{
		ID:                l.ID,
		UserID:            l.UserID,
		UserName:          userName,
		Name:              l.Name,
		CategoryID:        l.CategoryID,                    // ✅ TAMBAHKAN INI
		Category:          categoryName,
		Color:             l.Color,
		Location:          l.Location,
		Description:       l.Description,
		DateLost:          l.DateLost,
		Status:            l.Status,
		CreatedAt:         l.CreatedAt,
		DirectClaimID:     l.DirectClaimID,
		DirectClaimStatus: directClaimStatus,
		DirectClaim:       directClaimResp,
	}
}