package company

import (
	"tm/pkg/mongo"

	"go.mongodb.org/mongo-driver/bson/primitive"
)

// CompanyStatus represents company account status
type CompanyStatus string

const (
	CompanyStatusActive    CompanyStatus = "active"
	CompanyStatusInactive  CompanyStatus = "inactive"
	CompanyStatusSuspended CompanyStatus = "suspended"
	CompanyStatusPending   CompanyStatus = "pending"
)

// CompanyType represents the type of company
type CompanyType string

const (
	CompanyTypePrivate    CompanyType = "private"
	CompanyTypePublic     CompanyType = "public"
	CompanyTypeGovernment CompanyType = "government"
	CompanyTypeNgo        CompanyType = "ngo"
	CompanyTypeStartup    CompanyType = "startup"
)

// Company represents a company in the tender management system
type Company struct {
	mongo.Model        `bson:",inline"`
	Name               string        `bson:"name" json:"name"`
	Type               CompanyType   `bson:"type" json:"type"`
	Status             CompanyStatus `bson:"status" json:"status"`
	RegistrationNumber string        `bson:"registration_number" json:"registration_number"`
	TaxID              string        `bson:"tax_id" json:"tax_id"`
	Industry           string        `bson:"industry" json:"industry"`
	Description        *string       `bson:"description,omitempty" json:"description,omitempty"`
	Website            *string       `bson:"website,omitempty" json:"website,omitempty"`

	// Business information
	EmployeeCount *int     `bson:"employee_count,omitempty" json:"employee_count,omitempty"`
	AnnualRevenue *float64 `bson:"annual_revenue,omitempty" json:"annual_revenue,omitempty"`
	FoundedYear   *int     `bson:"founded_year,omitempty" json:"founded_year,omitempty"`

	// Address information
	Address *Address `bson:"address,omitempty" json:"address,omitempty"`

	// Contact information
	ContactPerson *ContactPerson `bson:"contact_person,omitempty" json:"contact_person,omitempty"`
	Phone         *string        `bson:"phone,omitempty" json:"phone,omitempty"`
	Email         *string        `bson:"email,omitempty" json:"email,omitempty"`

	// Tags for categorization and search
	Tags *CompanyTags `bson:"tags,omitempty" json:"tags,omitempty"`

	// Verification and compliance
	IsVerified      bool    `bson:"is_verified" json:"is_verified"`
	IsCompliant     bool    `bson:"is_compliant" json:"is_compliant"`
	ComplianceNotes *string `bson:"compliance_notes,omitempty" json:"compliance_notes,omitempty"`

	// Settings
	Language string `bson:"language" json:"language"` // Default: "en"
	Currency string `bson:"currency" json:"currency"` // Default: "USD"
	Timezone string `bson:"timezone" json:"timezone"` // Default: "UTC"

	// Audit fields
	CreatedBy *string `bson:"created_by,omitempty" json:"created_by,omitempty"`
	UpdatedBy *string `bson:"updated_by,omitempty" json:"updated_by,omitempty"`
}

// SetID sets the company ID (implements IDSetter interface)
func (c *Company) SetID(id string) {
	c.ID, _ = primitive.ObjectIDFromHex(id)
}

// GetID returns the company ID (implements IDGetter interface)
func (c *Company) GetID() string {
	return c.ID.Hex()
}

// SetCreatedAt sets the created timestamp (implements Timestampable interface)
func (c *Company) SetCreatedAt(timestamp int64) {
	c.CreatedAt = timestamp
}

// SetUpdatedAt sets the updated timestamp (implements Timestampable interface)
func (c *Company) SetUpdatedAt(timestamp int64) {
	c.UpdatedAt = timestamp
}

// GetCreatedAt returns the created timestamp (implements Timestampable interface)
func (c *Company) GetCreatedAt() int64 {
	return c.CreatedAt
}

// GetUpdatedAt returns the updated timestamp (implements Timestampable interface)
func (c *Company) GetUpdatedAt() int64 {
	return c.UpdatedAt
}

// Address represents a company's address
type Address struct {
	Street     string `bson:"street" json:"street"`
	City       string `bson:"city" json:"city"`
	State      string `bson:"state" json:"state"`
	PostalCode string `bson:"postal_code" json:"postal_code"`
	Country    string `bson:"country" json:"country"`
}

// ContactPerson represents a contact person for the company
type ContactPerson struct {
	FirstName string  `bson:"first_name" json:"first_name"`
	LastName  string  `bson:"last_name" json:"last_name"`
	FullName  string  `bson:"full_name" json:"full_name"`
	Position  string  `bson:"position" json:"position"`
	Email     string  `bson:"email" json:"email"`
	Phone     string  `bson:"phone" json:"phone"`
	Mobile    *string `bson:"mobile,omitempty" json:"mobile,omitempty"`
	IsPrimary bool    `bson:"is_primary" json:"is_primary"`
}

// CompanyTags represents tags for company categorization and search
type CompanyTags struct {
	// CPV codes (Common Procurement Vocabulary)
	CPVCodes []string `bson:"cpv_codes,omitempty" json:"cpv_codes,omitempty"`

	// Categories
	Categories []string `bson:"categories,omitempty" json:"categories,omitempty"`

	// Keywords for search and matching
	Keywords []string `bson:"keywords,omitempty" json:"keywords,omitempty"`

	// Specializations
	Specializations []string `bson:"specializations,omitempty" json:"specializations,omitempty"`

	// Certifications
	Certifications []string `bson:"certifications,omitempty" json:"certifications,omitempty"`
}

// CompanyResponse represents the company data sent in API responses
type CompanyResponse struct {
	ID                 string         `json:"id"`
	Name               string         `json:"name"`
	Type               string         `json:"type"`
	Status             string         `json:"status"`
	RegistrationNumber string         `json:"registration_number"`
	TaxID              string         `json:"tax_id"`
	Industry           string         `json:"industry"`
	Description        *string        `json:"description,omitempty"`
	Website            *string        `json:"website,omitempty"`
	EmployeeCount      *int           `json:"employee_count,omitempty"`
	AnnualRevenue      *float64       `json:"annual_revenue,omitempty"`
	FoundedYear        *int           `json:"founded_year,omitempty"`
	Address            *Address       `json:"address,omitempty"`
	ContactPerson      *ContactPerson `json:"contact_person,omitempty"`
	Phone              *string        `json:"phone,omitempty"`
	Email              *string        `json:"email,omitempty"`
	Tags               *CompanyTags   `json:"tags,omitempty"`
	IsVerified         bool           `json:"is_verified"`
	IsCompliant        bool           `json:"is_compliant"`
	ComplianceNotes    *string        `json:"compliance_notes,omitempty"`
	Language           string         `json:"language"`
	Currency           string         `json:"currency"`
	Timezone           string         `json:"timezone"`
	CreatedAt          int64          `json:"created_at"`
	UpdatedAt          int64          `json:"updated_at"`
	CreatedBy          *string        `json:"created_by,omitempty"`
	UpdatedBy          *string        `json:"updated_by,omitempty"`
}

// ToResponse converts Company to CompanyResponse
func (c *Company) ToResponse() *CompanyResponse {
	return &CompanyResponse{
		ID:                 c.ID.Hex(),
		Name:               c.Name,
		Type:               string(c.Type),
		Status:             string(c.Status),
		RegistrationNumber: c.RegistrationNumber,
		TaxID:              c.TaxID,
		Industry:           c.Industry,
		Description:        c.Description,
		Website:            c.Website,
		EmployeeCount:      c.EmployeeCount,
		AnnualRevenue:      c.AnnualRevenue,
		FoundedYear:        c.FoundedYear,
		Address:            c.Address,
		ContactPerson:      c.ContactPerson,
		Phone:              c.Phone,
		Email:              c.Email,
		Tags:               c.Tags,
		IsVerified:         c.IsVerified,
		IsCompliant:        c.IsCompliant,
		ComplianceNotes:    c.ComplianceNotes,
		Language:           c.Language,
		Currency:           c.Currency,
		Timezone:           c.Timezone,
		CreatedAt:          c.CreatedAt,
		UpdatedAt:          c.UpdatedAt,
		CreatedBy:          c.CreatedBy,
		UpdatedBy:          c.UpdatedBy,
	}
}

type CompanyProfileResponse struct {
	ID                 string `json:"id"`
	Name               string `json:"name"`
	RegistrationNumber string `json:"registration_number"`
	Industry           string `json:"industry"`
	FoundedYear        *int   `json:"founded_year,omitempty"`
	CreatedAt          int64  `json:"created_at"`
}

// ToResponse converts Company to CompanyResponse
func (c *Company) ToProfileResponse() *CompanyProfileResponse {
	return &CompanyProfileResponse{
		ID:                 c.ID.Hex(),
		Name:               c.Name,
		RegistrationNumber: c.RegistrationNumber,
		Industry:           c.Industry,
		FoundedYear:        c.FoundedYear,
		CreatedAt:          c.CreatedAt,
	}
}
