package main

import "testing"

func TestResolveCreateSuperAdminConfigUsesEnvDefaults(t *testing.T) {
	t.Setenv("ADMIN_EMAIL", "admin@example.com")
	t.Setenv("ADMIN_PASSWORD", "Password!123")
	t.Setenv("ADMIN_NAME", "Env Admin")

	config, err := resolveCreateSuperAdminConfig([]string{})
	if err != nil {
		t.Fatalf("resolveCreateSuperAdminConfig returned error: %v", err)
	}

	if config.Email != "admin@example.com" {
		t.Fatalf("email = %q", config.Email)
	}
	if config.Password != "Password!123" {
		t.Fatal("password was not read from ADMIN_PASSWORD")
	}
	if config.Name != "Env Admin" {
		t.Fatalf("name = %q", config.Name)
	}
}

func TestResolveCreateSuperAdminConfigAllowsFlagOverrides(t *testing.T) {
	t.Setenv("ADMIN_EMAIL", "admin@example.com")
	t.Setenv("ADMIN_PASSWORD", "Password!123")
	t.Setenv("ADMIN_NAME", "Env Admin")

	config, err := resolveCreateSuperAdminConfig([]string{
		"--email", "flag@example.com",
		"--password", "FlagPassword!123",
		"--name", "Flag Admin",
		"--update-password",
	})
	if err != nil {
		t.Fatalf("resolveCreateSuperAdminConfig returned error: %v", err)
	}

	if config.Email != "flag@example.com" {
		t.Fatalf("email = %q", config.Email)
	}
	if config.Password != "FlagPassword!123" {
		t.Fatal("password flag was not used")
	}
	if config.Name != "Flag Admin" {
		t.Fatalf("name = %q", config.Name)
	}
	if !config.UpdatePassword {
		t.Fatal("update password flag was not set")
	}
}

func TestResolveCreateSuperAdminConfigRequiresEmailAndPassword(t *testing.T) {
	t.Setenv("ADMIN_EMAIL", "")
	t.Setenv("ADMIN_PASSWORD", "")

	if _, err := resolveCreateSuperAdminConfig([]string{}); err == nil {
		t.Fatal("expected error")
	}
}

func TestResolveClearOrphanUserTenantMembershipsConfig(t *testing.T) {
	config, err := resolveClearOrphanUserTenantMembershipsConfig([]string{"--dry-run"})
	if err != nil {
		t.Fatalf("resolveClearOrphanUserTenantMembershipsConfig returned error: %v", err)
	}

	if !config.DryRun {
		t.Fatal("dry-run flag was not set")
	}
}
