package main

import (
	"fmt"

	"github.com/go-playground/validator/v10"
)

type User struct {
	Name     string `validate:"required,min=2,max=100"`
	Email    string `validate:"required,email"`
	Age      int    `validate:"gte=0,lte=130"`
	Password string `validate:"required,min=8"`
}

type Address struct {
	Street  string `validate:"required"`
	City    string `validate:"required"`
	Country string `validate:"required,iso3166_1_alpha2"`
	ZipCode string `validate:"required"`
}

type Registration struct {
	User    User    `validate:"required"`
	Address Address `validate:"required"`
}

func main() {
	validate := validator.New()

	// Valid user
	validUser := User{
		Name:     "Alice Smith",
		Email:    "alice@example.com",
		Age:      25,
		Password: "securepass123",
	}

	if err := validate.Struct(validUser); err != nil {
		fmt.Printf("Validation failed: %v\n", err)
	} else {
		fmt.Println("Valid user: OK")
	}

	// Invalid user - demonstrates error handling
	invalidUser := User{
		Name:     "A",                 // too short
		Email:    "not-an-email",      // invalid email
		Age:      150,                 // out of range
		Password: "short",             // too short
	}

	if err := validate.Struct(invalidUser); err != nil {
		fmt.Println("\nInvalid user errors:")
		for _, err := range err.(validator.ValidationErrors) {
			fmt.Printf("  - Field '%s': failed '%s' validation\n",
				err.Field(), err.Tag())
		}
	}

	// Nested struct validation
	reg := Registration{
		User: User{
			Name:     "Bob",
			Email:    "bob@example.com",
			Age:      30,
			Password: "password123",
		},
		Address: Address{
			Street:  "123 Main St",
			City:    "New York",
			Country: "US",
			ZipCode: "10001",
		},
	}

	if err := validate.Struct(reg); err != nil {
		fmt.Printf("\nRegistration errors: %v\n", err)
	} else {
		fmt.Println("\nValid registration: OK")
	}
}
