package models

import (
	"time"

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

// TenantByID represents the tenants_by_id table
// Query pattern: Get tenant by ID
// Primary key: id
type TenantByID struct {
	ID        string    `db:"id"`
	Name      string    `db:"name"`
	Slug      string    `db:"slug"`
	Status    string    `db:"status"`
	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 *TenantByID) 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,
	}
}

// FromTenant converts domain entity to table model
func FromTenant(t *tenant.Tenant) *TenantByID {
	return &TenantByID{
		ID:        t.ID,
		Name:      t.Name,
		Slug:      t.Slug,
		Status:    string(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,
	}
}
