package graphql

func (r *Reader) isScalarType(typeName string, ctx *parseContext) bool {
	// Built-in GraphQL scalars
	builtInScalars := map[string]bool{
		"ID":      true,
		"String":  true,
		"Int":     true,
		"Float":   true,
		"Boolean": true,
	}

	if builtInScalars[typeName] {
		return true
	}

	// Custom scalars declared in the schema
	if ctx.customScalars[typeName] {
		return true
	}

	// Common custom scalars (even if not declared)
	commonCustomScalars := map[string]bool{
		"DateTime": true,
		"JSON":     true,
		"Date":     true,
		"Time":     true,
		"Upload":   true,
		"Decimal":  true,
	}

	return commonCustomScalars[typeName]
}

func (r *Reader) graphQLTypeToSQL(gqlType, fieldName, typeName string) string {
	// Check for ID type with configurable mapping
	if gqlType == "ID" {
		// Check metadata for ID type preference
		if r.options.Metadata != nil {
			// Global idType setting
			if idType, ok := r.options.Metadata["idType"].(string); ok {
				if idType == "uuid" {
					return "uuid"
				}
			}

			// Per-type ID mapping
			if typeIdMappings, ok := r.options.Metadata["typeIdMappings"].(map[string]string); ok {
				if idType, ok := typeIdMappings[typeName]; ok {
					if idType == "uuid" {
						return "uuid"
					}
				}
			}
		}

		return "bigint" // Default
	}

	// Custom scalar mappings
	if r.options.Metadata != nil {
		if customMappings, ok := r.options.Metadata["customScalarMappings"].(map[string]string); ok {
			if sqlType, ok := customMappings[gqlType]; ok {
				return sqlType
			}
		}
	}

	// Built-in custom scalar mappings
	customScalars := map[string]string{
		"DateTime": "timestamp",
		"JSON":     "jsonb",
		"Date":     "date",
		"Time":     "time",
		"Decimal":  "numeric",
		"Upload":   "bytea",
	}
	if sqlType, ok := customScalars[gqlType]; ok {
		return sqlType
	}

	// Standard scalar mappings
	typeMap := map[string]string{
		"String":  "text",
		"Int":     "integer",
		"Float":   "double precision",
		"Boolean": "boolean",
	}

	if sqlType, ok := typeMap[gqlType]; ok {
		return sqlType
	}

	// If not a known scalar, assume it's an enum or custom type
	// Return as-is (might be an enum)
	return gqlType
}
