// internal/models/claim_verification.go
package models

import (
	"time"

	"gorm.io/gorm"
)

// ClaimVerification represents verification data for a claim
type ClaimVerification struct {
	ID              uint           `gorm:"primaryKey" json:"id"`
	ClaimID         uint           `gorm:"not null;uniqueIndex" json:"claim_id"`
	Claim           Claim          `gorm:"foreignKey:ClaimID" json:"claim,omitempty"`
	SimilarityScore float64        `gorm:"type:decimal(5,2);default:0" json:"similarity_score"` // Percentage match (0-100)
	MatchedKeywords string         `gorm:"type:text" json:"matched_keywords"`                   // Keywords that matched
	VerificationNotes string       `gorm:"type:text" json:"verification_notes"`                 // Manager's notes
	IsAutoMatched   bool           `gorm:"default:false" json:"is_auto_matched"`                // Was it auto-matched?
	CreatedAt       time.Time      `json:"created_at"`
	UpdatedAt       time.Time      `json:"updated_at"`
	DeletedAt       gorm.DeletedAt `gorm:"index" json:"-"`
}

// TableName specifies the table name for ClaimVerification model
func (ClaimVerification) TableName() string {
	return "claim_verifications"
}

// IsHighMatch checks if similarity score is high (>= 70%)
func (cv *ClaimVerification) IsHighMatch() bool {
	return cv.SimilarityScore >= 70.0
}

// IsMediumMatch checks if similarity score is medium (50-69%)
func (cv *ClaimVerification) IsMediumMatch() bool {
	return cv.SimilarityScore >= 50.0 && cv.SimilarityScore < 70.0
}

// IsLowMatch checks if similarity score is low (< 50%)
func (cv *ClaimVerification) IsLowMatch() bool {
	return cv.SimilarityScore < 50.0
}

// GetMatchLevel returns the match level as string
func (cv *ClaimVerification) GetMatchLevel() string {
	if cv.IsHighMatch() {
		return "high"
	} else if cv.IsMediumMatch() {
		return "medium"
	}
	return "low"
}

// ClaimVerificationResponse represents verification data for API responses
type ClaimVerificationResponse struct {
	ID              uint      `json:"id"`
	ClaimID         uint      `json:"claim_id"`
	SimilarityScore float64   `json:"similarity_score"`
	MatchLevel      string    `json:"match_level"`
	MatchedKeywords string    `json:"matched_keywords"`
	VerificationNotes string  `json:"verification_notes"`
	IsAutoMatched   bool      `json:"is_auto_matched"`
	CreatedAt       time.Time `json:"created_at"`
}

// ToResponse converts ClaimVerification to ClaimVerificationResponse
func (cv *ClaimVerification) ToResponse() ClaimVerificationResponse {
	return ClaimVerificationResponse{
		ID:                cv.ID,
		ClaimID:           cv.ClaimID,
		SimilarityScore:   cv.SimilarityScore,
		MatchLevel:        cv.GetMatchLevel(),
		MatchedKeywords:   cv.MatchedKeywords,
		VerificationNotes: cv.VerificationNotes,
		IsAutoMatched:     cv.IsAutoMatched,
		CreatedAt:         cv.CreatedAt,
	}
}