package domain

import (
	"encoding/json"
	"time"
)

type CRMCustomer struct {
	ID               string     `json:"id" db:"id"`
	TenantID         string     `json:"tenant_id" db:"tenant_id"`
	Name             string     `json:"name" db:"name"`
	Email            string     `json:"email" db:"email"`
	Phone            string     `json:"phone" db:"phone"`
	Company          string     `json:"company" db:"company"`
	Position         string     `json:"position" db:"position"`
	Address          string     `json:"address" db:"address"`
	City             string     `json:"city" db:"city"`
	State            string     `json:"state" db:"state"`
	ZipCode          string     `json:"zip_code" db:"zip_code"`
	Country          string     `json:"country" db:"country"`
	Notes            string     `json:"notes" db:"notes"`
	Tags             []string   `json:"tags" db:"tags"`
	LogoURL          string     `json:"logo_url" db:"logo_url"`
	IsActive         bool       `json:"is_active" db:"is_active"`
	CreatedBy        string     `json:"created_by" db:"created_by"`
	CreatedAt        time.Time  `json:"created_at" db:"created_at"`
	UpdatedAt        time.Time  `json:"updated_at" db:"updated_at"`
	PasswordHash     string     `json:"-" db:"password_hash"`
	HasPortalAccess  bool       `json:"has_portal_access" db:"has_portal_access"`
	PortalLastLogin  *time.Time `json:"portal_last_login,omitempty" db:"portal_last_login"`
	PortalCreatedAt  *time.Time `json:"portal_created_at,omitempty" db:"portal_created_at"`
}

type CRMList struct {
	ID          string    `json:"id" db:"id"`
	TenantID    string    `json:"tenant_id" db:"tenant_id"`
	CustomerID  *string   `json:"customer_id" db:"customer_id"`
	FunnelID    *string   `json:"funnel_id" db:"funnel_id"`
	Name        string    `json:"name" db:"name"`
	Description string    `json:"description" db:"description"`
	Color       string    `json:"color" db:"color"`
	CreatedBy   string    `json:"created_by" db:"created_by"`
	CreatedAt   time.Time `json:"created_at" db:"created_at"`
	UpdatedAt   time.Time `json:"updated_at" db:"updated_at"`
}

type CRMCustomerList struct {
	CustomerID string    `json:"customer_id" db:"customer_id"`
	ListID     string    `json:"list_id" db:"list_id"`
	AddedAt    time.Time `json:"added_at" db:"added_at"`
	AddedBy    string    `json:"added_by" db:"added_by"`
}

// DTO com informações extras
type CRMCustomerWithLists struct {
	CRMCustomer
	Lists []CRMList `json:"lists"`
}

type CRMListWithCustomers struct {
	CRMList
	CustomerName  string `json:"customer_name"`
	CustomerCount int    `json:"customer_count"`
	LeadCount     int    `json:"lead_count"`
}

// ==================== LEADS ====================

type CRMLead struct {
	ID         string          `json:"id" db:"id"`
	TenantID   string          `json:"tenant_id" db:"tenant_id"`
	CustomerID *string         `json:"customer_id" db:"customer_id"`
	FunnelID   *string         `json:"funnel_id" db:"funnel_id"`
	StageID    *string         `json:"stage_id" db:"stage_id"`
	Name       string          `json:"name" db:"name"`
	Email      string          `json:"email" db:"email"`
	Phone      string          `json:"phone" db:"phone"`
	Source     string          `json:"source" db:"source"`
	SourceMeta json.RawMessage `json:"source_meta" db:"source_meta"`
	Status     string          `json:"status" db:"status"`
	Notes      string          `json:"notes" db:"notes"`
	Tags       []string        `json:"tags" db:"tags"`
	IsActive   bool            `json:"is_active" db:"is_active"`
	CreatedBy  string          `json:"created_by" db:"created_by"`
	CreatedAt  time.Time       `json:"created_at" db:"created_at"`
	UpdatedAt  time.Time       `json:"updated_at" db:"updated_at"`
}

type CRMFunnel struct {
	ID          string    `json:"id" db:"id"`
	TenantID    string    `json:"tenant_id" db:"tenant_id"`
	Name        string    `json:"name" db:"name"`
	Description string    `json:"description" db:"description"`
	IsDefault   bool      `json:"is_default" db:"is_default"`
	CreatedAt   time.Time `json:"created_at" db:"created_at"`
	UpdatedAt   time.Time `json:"updated_at" db:"updated_at"`
}

type CRMFunnelStage struct {
	ID          string    `json:"id" db:"id"`
	FunnelID    string    `json:"funnel_id" db:"funnel_id"`
	Name        string    `json:"name" db:"name"`
	Description string    `json:"description" db:"description"`
	Color       string    `json:"color" db:"color"`
	OrderIndex  int       `json:"order_index" db:"order_index"`
	CreatedAt   time.Time `json:"created_at" db:"created_at"`
	UpdatedAt   time.Time `json:"updated_at" db:"updated_at"`
}

type CRMFunnelWithStages struct {
	CRMFunnel
	Stages []CRMFunnelStage `json:"stages"`
}

type CRMLeadList struct {
	LeadID  string    `json:"lead_id" db:"lead_id"`
	ListID  string    `json:"list_id" db:"list_id"`
	AddedAt time.Time `json:"added_at" db:"added_at"`
	AddedBy string    `json:"added_by" db:"added_by"`
}

type CRMLeadWithLists struct {
	CRMLead
	Lists []CRMList `json:"lists"`
}

type CRMShareToken struct {
	ID         string    `json:"id" db:"id"`
	TenantID   string    `json:"tenant_id" db:"tenant_id"`
	CustomerID string    `json:"customer_id" db:"customer_id"`
	Token      string    `json:"token" db:"token"`
	ExpiresAt  *time.Time `json:"expires_at" db:"expires_at"`
	CreatedBy  string    `json:"created_by" db:"created_by"`
	CreatedAt  time.Time `json:"created_at" db:"created_at"`
}
