'use client';

import { apiClient } from '@/lib/apiClient';

/**
 * Membership role types - these are defined locally to avoid core dependencies
 */
export type MembershipRole = 'owner' | 'admin' | 'steward' | 'member';
export type MembershipStatus = 'active' | 'inactive' | 'pending';

/**
 * Lightweight league membership model for UI.
 */
export interface LeagueMembership {
  id: string;
  leagueId: string;
  driverId: string;
  role: MembershipRole;
  status: MembershipStatus;
  joinedAt: string;
}

// In-memory cache for memberships (populated via API calls)
const leagueMemberships = new Map<string, LeagueMembership[]>();

/**
 * Get a specific membership from cache.
 */
export function getMembership(leagueId: string, driverId: string): LeagueMembership | null {
  const list = leagueMemberships.get(leagueId);
  if (!list) return null;
  return list.find((m) => m.driverId === driverId) ?? null;
}

/**
 * Get all members of a league from cache.
 */
export function getLeagueMembers(leagueId: string): LeagueMembership[] {
  return [...(leagueMemberships.get(leagueId) ?? [])];
}

/**
 * Fetch and cache memberships for a league via API.
 */
export async function fetchLeagueMemberships(leagueId: string): Promise<LeagueMembership[]> {
  try {
    const result = await apiClient.leagues.getMemberships(leagueId);
    const memberships: LeagueMembership[] = result.members.map(member => ({
      id: `${member.driverId}-${leagueId}`, // Generate ID since API doesn't provide it
      leagueId,
      driverId: member.driverId,
      role: member.role as MembershipRole,
      status: 'active' as MembershipStatus, // Assume active since API returns current members
      joinedAt: member.joinedAt,
    }));
    setLeagueMemberships(leagueId, memberships);
    return memberships;
  } catch (error) {
    console.error('Failed to fetch league memberships:', error);
    return [];
  }
}

/**
 * Set memberships in cache (for use after API calls).
 */
export function setLeagueMemberships(leagueId: string, memberships: LeagueMembership[]): void {
  leagueMemberships.set(leagueId, memberships);
}

/**
 * Clear cached memberships for a league.
 */
export function clearLeagueMemberships(leagueId: string): void {
  leagueMemberships.delete(leagueId);
}

/**
 * Derive a driver's primary league from cached memberships.
 * Prefers any active membership and returns the first matching league.
 */
export function getPrimaryLeagueIdForDriver(driverId: string): string | null {
  for (const [leagueId, members] of leagueMemberships.entries()) {
    if (members.some((m) => m.driverId === driverId && m.status === 'active')) {
      return leagueId;
    }
  }
  return null;
}

/**
 * Check if a driver is owner or admin of a league.
 */
export function isOwnerOrAdmin(leagueId: string, driverId: string): boolean {
  const membership = getMembership(leagueId, driverId);
  if (!membership) return false;
  return membership.role === 'owner' || membership.role === 'admin';
}