package web

import (
	"context"
	"testing"
)

func TestSocialIdentitySchemaSupportsNullablePasswordAndProviderIdentity(t *testing.T) {
	pool, cleanup := setupTestDB(t)
	defer cleanup()

	ctx := context.Background()

	var userID string
	if err := pool.QueryRow(ctx, `
		INSERT INTO users (email, password_hash)
		VALUES ('social-schema@example.com', NULL)
		RETURNING id::text
	`).Scan(&userID); err != nil {
		t.Fatalf("insert social-only user with NULL password_hash: %v", err)
	}

	if _, err := pool.Exec(ctx, `
		INSERT INTO user_identities (
			user_id, provider, provider_subject, email, email_verified, display_name, avatar_url, last_login_at
		)
		VALUES (
			$1, 'google', 'google-sub-1', 'social-schema@example.com', true,
			'Social Schema', 'https://example.com/avatar.png', now()
		)
	`, userID); err != nil {
		t.Fatalf("insert provider identity: %v", err)
	}

	var count int
	if err := pool.QueryRow(ctx, `
		SELECT COUNT(*)
		FROM user_identities
		WHERE provider = 'google'
		  AND provider_subject = 'google-sub-1'
		  AND email_verified = true
	`).Scan(&count); err != nil {
		t.Fatalf("count provider identity: %v", err)
	}
	if count != 1 {
		t.Fatalf("identity count = %d; want 1", count)
	}
}
