package caasmmodel

import (
	"time"

	coremodel "security-center-backend/internal/model"
	"security-center-backend/internal/service/directory"
)

type CloudProvider string

const (
	CloudProviderAWS     CloudProvider = "aws"
	CloudProviderGCP     CloudProvider = "gcp"
	CloudProviderAzure   CloudProvider = "azure"
	CloudProviderAlibaba CloudProvider = "alibaba"
	CloudProviderGoDaddy CloudProvider = "godaddy"
	CloudProviderOther   CloudProvider = "other"
)

type CloudAccountStatus string

const (
	CloudAccountStatusActive   CloudAccountStatus = "active"
	CloudAccountStatusInactive CloudAccountStatus = "inactive"
)

type CloudAccount struct {
	coremodel.Base
	Name                string             `gorm:"size:255" json:"name"`
	Provider            CloudProvider      `gorm:"size:32" json:"provider"`
	ExternalID          string             `gorm:"size:255;index" json:"external_id"`
	Status              CloudAccountStatus `gorm:"size:32" json:"status"`
	SyncIntervalMinutes int                `gorm:"default:60" json:"sync_interval_minutes"`
	LastSyncAt          *time.Time         `json:"last_sync_at"`
	ErrorMessage        string             `gorm:"type:text" json:"error_message"`
	Metadata            coremodel.JSONB    `gorm:"type:json" json:"metadata"`
	EnableAWSConfigSync bool               `gorm:"default:false" json:"enable_aws_config_sync"`
}

type AssetType string

const (
	AssetTypeCompute     AssetType = "compute"
	AssetTypeStorage     AssetType = "storage"
	AssetTypeDatabase    AssetType = "database"
	AssetTypeNetwork     AssetType = "network"
	AssetTypeApplication AssetType = "application"
	AssetTypeIdentity    AssetType = "identity"
	AssetTypeOther       AssetType = "other"
)

type AssetStatus string

const (
	AssetStatusActive   AssetStatus = "active"
	AssetStatusInactive AssetStatus = "inactive"
)

type Asset struct {
	coremodel.Base
	CloudAccountID  *uint64 `gorm:"uniqueIndex:idx_assets_cloud_account_native,priority:1;index:idx_assets_cloud_account" json:"cloud_account_id"`
	CloudAccount    CloudAccount
	NativeID        string                `gorm:"size:255;uniqueIndex:idx_assets_cloud_account_native,priority:2;index:idx_assets_native_id" json:"native_id"`
	Name            string                `gorm:"size:255" json:"name"`
	AssetType       AssetType             `gorm:"size:32;index:idx_asset_type_env,priority:1" json:"asset_type"`
	Service         string                `gorm:"size:64;index" json:"service"` // Specific cloud service: S3, EC2, RDS, ELB, etc.
	Criticality     string                `gorm:"size:16" json:"criticality"`
	Status          AssetStatus           `gorm:"size:16" json:"status"`
	OwnerEmployeeID *uint64               `gorm:"index" json:"owner_employee_id,omitempty"`
	DepartmentID    *string               `gorm:"size:255;index" json:"department_id,omitempty"`
	OwnerEmployee   *directory.Employee   `gorm:"foreignKey:OwnerEmployeeID" json:"owner_employee,omitempty"`
	Department      *directory.Department `gorm:"foreignKey:DepartmentID;references:LarkDepartmentID" json:"department,omitempty"`
	Environment     string                `gorm:"size:32;index:idx_asset_type_env,priority:2" json:"environment"`
	Region          string                `gorm:"size:32" json:"region"`
	Provider        string                `gorm:"size:32" json:"provider"`
	Metadata        coremodel.JSONB       `gorm:"type:json" json:"metadata"`
	IPAssets        []IPAsset             `gorm:"foreignKey:AssetID" json:"ip_assets,omitempty"`
	FirstSeenAt     *time.Time            `json:"first_seen_at"`
	LastSeenAt      *time.Time            `json:"last_seen_at"`
}

type IPAsset struct {
	coremodel.Base
	AssetID     uint64 `gorm:"uniqueIndex:idx_ip_assets_asset_ip,priority:1" json:"asset_id"`
	Asset       Asset
	IPAddress   string          `gorm:"size:64;uniqueIndex:idx_ip_assets_asset_ip,priority:2;index:idx_ip_assets_ip" json:"ip_address"`
	Hostname    string          `gorm:"size:255" json:"hostname"`
	GeoLocation coremodel.JSONB `gorm:"type:json" json:"geo_location"`
	IsPublic    bool            `json:"is_public"`
	FirstSeenAt *time.Time      `json:"first_seen_at"`
	LastSeenAt  *time.Time      `json:"last_seen_at"`
}

type DomainAsset struct {
	coremodel.Base
	AssetID                   uint64 `gorm:"uniqueIndex:idx_domain_assets_asset_domain,priority:1;index:idx_domain_assets_asset" json:"asset_id"`
	Asset                     Asset
	DomainName                string                `gorm:"size:255;uniqueIndex:idx_domain_assets_asset_domain,priority:2;index:idx_domain_assets_domain_name" json:"domain_name"`
	DomainClass               string                `gorm:"size:64;default:unknown;uniqueIndex:idx_domain_assets_asset_domain,priority:3;index:idx_domain_assets_class" json:"domain_class,omitempty"`
	Registrar                 string                `gorm:"size:255" json:"registrar"`
	ExpiresAt                 *time.Time            `json:"expires_at"`
	ZoneID                    string                `gorm:"size:255" json:"zone_id"`
	ZoneType                  string                `gorm:"size:32" json:"zone_type"`
	Status                    string                `gorm:"size:64" json:"status"`
	AutoRenew                 bool                  `json:"auto_renew"`
	TTLDefault                uint                  `json:"ttl_default"`
	NameServers               coremodel.StringArray `gorm:"type:json" json:"name_servers"`
	NameServersLastChangeAt   *time.Time            `json:"name_servers_last_change_at"`
	NameServersLastChangeFrom coremodel.RawJSON     `gorm:"type:json" json:"name_servers_last_change_from"`
	NameServersLastChangeTo   coremodel.RawJSON     `gorm:"type:json" json:"name_servers_last_change_to"`
	ForwardingCfg             coremodel.JSONB       `gorm:"type:json" json:"forwarding_cfg"`
	PeeringCfg                coremodel.JSONB       `gorm:"type:json" json:"peering_cfg"`
	RoutingPolicy             coremodel.JSONB       `gorm:"type:json" json:"routing_policy"`
	RegistrantContact         coremodel.JSONB       `gorm:"type:json" json:"registrant_contact"`
	DNSHealth                 coremodel.JSONB       `gorm:"type:json" json:"dns_health"`
	DNSRecords                coremodel.JSONB       `gorm:"type:json" json:"dns_records"`
	OwnerEmployeeID           *uint64               `gorm:"index" json:"owner_employee_id,omitempty"`
	DepartmentID              *string               `gorm:"size:255;index" json:"department_id,omitempty"`
	OwnerEmployee             *directory.Employee   `gorm:"foreignKey:OwnerEmployeeID" json:"owner_employee,omitempty"`
	Department                *directory.Department `gorm:"foreignKey:DepartmentID;references:LarkDepartmentID" json:"department,omitempty"`
	LastSyncedAt              *time.Time            `json:"last_synced_at"`
	FirstSeenAt               *time.Time            `json:"first_seen_at"`
	LastSeenAt                *time.Time            `json:"last_seen_at"`
}

const (
	DomainClassUnknown           = "unknown"
	DomainClassAuthoritativeZone = "hosted_zone"
	DomainClassDNSRecord         = "record_derived_name"
	DomainClassServiceHostname   = "service_hostname"
	DomainClassMappedCustom      = "mapped_custom_domain"
)
