import type { AccountsBasic$result, Customers$result, Team$result } from '$houdini';
import { fromGlobalId } from './relay';

export interface AccountAddressInfo {
	accountId: string;
	accountName: string;
	addressId: string;
	addressName: string;
	streetAddress: string | null;
	city: string | null;
	state: string | null;
	zipCode: string | null;
}

export interface CustomerInfo {
	customerId: string;
	customerName: string;
}

/**
 * Builds a lookup map from accountAddressId (UUID) to account/address info.
 * The accounts data has addresses with Relay IDs, but services/projects have UUID addressIds.
 */
export function buildAccountAddressLookup(
	accounts: AccountsBasic$result['accounts'] | null | undefined
): Map<string, AccountAddressInfo> {
	const lookup = new Map<string, AccountAddressInfo>();

	if (!accounts) return lookup;

	for (const account of accounts) {
		const accountId = fromGlobalId(account.id);
		const accountName = account.name;

		// Add primary address if exists
		if (account.primaryAddress) {
			const addressId = fromGlobalId(account.primaryAddress.id);
			lookup.set(addressId, {
				accountId,
				accountName,
				addressId,
				addressName: 'Primary Service Address',
				streetAddress: account.primaryAddress.streetAddress ?? null,
				city: account.primaryAddress.city ?? null,
				state: account.primaryAddress.state ?? null,
				zipCode: account.primaryAddress.zipCode ?? null
			});
		}

		// Add all addresses from the addresses array
		if (account.addresses) {
			for (const address of account.addresses) {
				const addressId = fromGlobalId(address.id);
				lookup.set(addressId, {
					accountId,
					accountName,
					addressId,
					addressName: address.name || 'Primary Service Address',
					streetAddress: address.streetAddress ?? null,
					city: address.city ?? null,
					state: address.state ?? null,
					zipCode: address.zipCode ?? null
				});
			}
		}
	}

	return lookup;
}

/**
 * Builds a lookup map from customerId (UUID) to customer info.
 */
export function buildCustomerLookup(
	customers: Customers$result['customers'] | null | undefined
): Map<string, CustomerInfo> {
	const lookup = new Map<string, CustomerInfo>();

	if (!customers) return lookup;

	for (const customer of customers) {
		const customerId = fromGlobalId(customer.id);
		lookup.set(customerId, {
			customerId,
			customerName: customer.name
		});
	}

	return lookup;
}

/**
 * Formats an address for display.
 */
export function formatAddress(info: {
	streetAddress?: string | null;
	city?: string | null;
	state?: string | null;
	zipCode?: string | null;
}): string | null {
	const { streetAddress, city, state, zipCode } = info;
	const cityStateZip = [city, state].filter(Boolean).join(', ') + (zipCode ? ` ${zipCode}` : '');
	const parts = [streetAddress, cityStateZip].filter(Boolean);
	return parts.length > 0 ? parts.join(', ') : null;
}

/**
 * Builds a Set of non-ADMIN team member IDs (UUIDs).
 * Used to filter team member counts on services/projects.
 */
export function buildNonAdminTeamIds(
	teamProfiles: Team$result['teamProfiles'] | null | undefined
): Set<string> {
	const nonAdminIds = new Set<string>();

	if (!teamProfiles) return nonAdminIds;

	for (const profile of teamProfiles) {
		if (profile.role !== 'ADMIN') {
			const id = fromGlobalId(profile.id);
			nonAdminIds.add(id);
		}
	}

	return nonAdminIds;
}

export interface TeamMemberInfo {
	id: string; // UUID
	fullName: string;
	role: string;
}

/**
 * Builds a lookup map from team member UUID to their info.
 * The teamProfiles have Relay IDs, but services/projects have UUIDs in teamMembers.pk.
 */
export function buildTeamMemberLookup(
	teamProfiles: Team$result['teamProfiles'] | null | undefined
): Map<string, TeamMemberInfo> {
	const lookup = new Map<string, TeamMemberInfo>();

	if (!teamProfiles) return lookup;

	for (const profile of teamProfiles) {
		const id = fromGlobalId(profile.id);
		lookup.set(id, {
			id,
			fullName: profile.fullName ?? 'Unknown',
			role: profile.role
		});
	}

	return lookup;
}
