package models

import (
	"time"

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

// TenantBySlug represents the tenants_by_slug table
// Query pattern: Get tenant by slug (URL-friendly identifier)
// Primary key: slug
type TenantBySlug struct {
	Slug      string    `db:"slug"`
	ID        string    `db:"id"`
	Name      string    `db:"name"`
	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 *TenantBySlug) 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,
	}
}

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