package entity

import (
	"github.com/google/uuid"
	"time"
)

// UserClientAccess represents many-to-many relationship between Users and Clients
// This allows users to have access to multiple clients (multi-tenant access)
type UserClientAccess struct {
	ID          uint       `json:"id" gorm:"primaryKey;type:int4;autoIncrement"`
	UserId      uint       `json:"user_id" gorm:"type:int4;not null;index:idx_user_client_access"`
	ClientId    uuid.UUID  `json:"client_id" gorm:"type:UUID;not null;index:idx_user_client_access"`
	
	// Access control
	AccessType  string     `json:"access_type" gorm:"type:varchar;default:'read'"` // 'read', 'write', 'admin', 'owner'
	CanManage   *bool      `json:"can_manage" gorm:"type:bool;default:false"` // Can manage client settings
	CanDelegate *bool      `json:"can_delegate" gorm:"type:bool;default:false"` // Can give access to other users
	
	// If user has access to parent, they automatically have access to all sub-clients
	IncludeSubClients *bool `json:"include_sub_clients" gorm:"type:bool;default:false"`
	
	// Relationships
	User        *Users    `json:"user,omitempty" gorm:"foreignKey:UserId;references:ID"`
	Client      *Clients  `json:"client,omitempty" gorm:"foreignKey:ClientId;references:ID"`
	
	GrantedById *uint     `json:"granted_by_id" gorm:"type:int4"` // Who granted this access
	GrantedBy   *Users    `json:"granted_by,omitempty" gorm:"foreignKey:GrantedById;references:ID"`
	
	IsActive    *bool     `json:"is_active" gorm:"type:bool;default:true"`
	CreatedAt   time.Time `json:"created_at" gorm:"default:now()"`
	UpdatedAt   time.Time `json:"updated_at" gorm:"default:now()"`
}

// TableName overrides the table name
func (UserClientAccess) TableName() string {
	return "user_client_access"
}
