package models

import (
	"time"

	"codeberg.org/mapleopentech/monorepo/cloud/maplepress-backend/internal/domain/tenant"
)

// TenantByStatus represents the tenants_by_status table
// Query pattern: List tenants by status (e.g., active, inactive, suspended)
// Primary key: (status, id) - status is partition key, id is clustering key
type TenantByStatus struct {
	Status    string    `db:"status"`
	ID        string    `db:"id"`
	Name      string    `db:"name"`
	Slug      string    `db:"slug"`
	CreatedAt time.Time `db:"created_at"`
	UpdatedAt time.Time `db:"updated_at"`

	// CWE-359: IP address tracking for GDPR compliance
	CreatedFromIPAddress    string    `db:"created_from_ip_address"`
	CreatedFromIPTimestamp  time.Time `db:"created_from_ip_timestamp"`
	ModifiedFromIPAddress   string    `db:"modified_from_ip_address"`
	ModifiedFromIPTimestamp time.Time `db:"modified_from_ip_timestamp"`
}

// ToTenant converts table model to domain entity
func (t *TenantByStatus) ToTenant() *tenant.Tenant {
	return &tenant.Tenant{
		ID:        t.ID,
		Name:      t.Name,
		Slug:      t.Slug,
		Status:    tenant.Status(t.Status),
		CreatedAt: t.CreatedAt,
		UpdatedAt: t.UpdatedAt,

		// CWE-359: IP address tracking
		CreatedFromIPAddress:    t.CreatedFromIPAddress,
		CreatedFromIPTimestamp:  t.CreatedFromIPTimestamp,
		ModifiedFromIPAddress:   t.ModifiedFromIPAddress,
		ModifiedFromIPTimestamp: t.ModifiedFromIPTimestamp,
	}
}

// FromTenantByStatus converts domain entity to table model
func FromTenantByStatus(t *tenant.Tenant) *TenantByStatus {
	return &TenantByStatus{
		Status:    string(t.Status),
		ID:        t.ID,
		Name:      t.Name,
		Slug:      t.Slug,
		CreatedAt: t.CreatedAt,
		UpdatedAt: t.UpdatedAt,

		// CWE-359: IP address tracking
		CreatedFromIPAddress:    t.CreatedFromIPAddress,
		CreatedFromIPTimestamp:  t.CreatedFromIPTimestamp,
		ModifiedFromIPAddress:   t.ModifiedFromIPAddress,
		ModifiedFromIPTimestamp: t.ModifiedFromIPTimestamp,
	}
}
