package infra

import (
	"fmt"
	"strings"
)

// CRUDGenerateTableSQL genera el DDL CREATE TABLE IF NOT EXISTS correspondiente a un CRUDResource.
// Incluye siempre: id TEXT PRIMARY KEY, created_at TEXT NOT NULL, updated_at TEXT NOT NULL.
// Si el recurso es SoftDelete, agrega una columna deleted_at TEXT (nullable).
// Cada CRUDField se mapea a su tipo SQLite y aplica NOT NULL, UNIQUE y DEFAULT segun corresponda.
func CRUDGenerateTableSQL(res CRUDResource) string {
	var sb strings.Builder
	sb.WriteString(fmt.Sprintf("CREATE TABLE IF NOT EXISTS %s (\n", res.Table))
	sb.WriteString("    id TEXT PRIMARY KEY")
	for _, f := range res.Fields {
		sb.WriteString(",\n    ")
		sb.WriteString(f.Name)
		sb.WriteString(" ")
		sb.WriteString(strings.ToUpper(f.Type))
		if f.Required {
			sb.WriteString(" NOT NULL")
		}
		if f.Unique {
			sb.WriteString(" UNIQUE")
		}
		if f.Default != "" {
			sb.WriteString(" DEFAULT ")
			sb.WriteString(f.Default)
		}
	}
	sb.WriteString(",\n    created_at TEXT NOT NULL")
	sb.WriteString(",\n    updated_at TEXT NOT NULL")
	if res.SoftDelete {
		sb.WriteString(",\n    deleted_at TEXT")
	}
	sb.WriteString("\n);\n")
	return sb.String()
}
