/**
 * Base repository interface for data access operations
 */
export interface IRepository<T, K = string> {
  /**
   * Find entity by ID
   */
  findById(id: K): Promise<T | null>;
  
  /**
   * Find all entities with optional filters
   */
  findAll(filters?: Record<string, any>): Promise<T[]>;
  
  /**
   * Create new entity
   */
  create(entity: Omit<T, 'id'>): Promise<T>;
  
  /**
   * Update existing entity
   */
  update(id: K, updates: Partial<T>): Promise<T>;
  
  /**
   * Delete entity by ID
   */
  delete(id: K): Promise<void>;
  
  /**
   * Check if entity exists
   */
  exists(id: K): Promise<boolean>;
  
  /**
   * Count entities with optional filters
   */
  count(filters?: Record<string, any>): Promise<number>;
}

/**
 * Repository with pagination support
 */
export interface IPaginatedRepository<T, K = string> extends IRepository<T, K> {
  /**
   * Find entities with pagination
   */
  findPaginated(
    page: number,
    limit: number,
    filters?: Record<string, any>
  ): Promise<{
    data: T[];
    total: number;
    page: number;
    limit: number;
    totalPages: number;
  }>;
}

/**
 * Base repository implementation
 */
export abstract class BaseRepository<T, K = string> implements IRepository<T, K> {
  abstract findById(id: K): Promise<T | null>;
  abstract findAll(filters?: Record<string, any>): Promise<T[]>;
  abstract create(entity: Omit<T, 'id'>): Promise<T>;
  abstract update(id: K, updates: Partial<T>): Promise<T>;
  abstract delete(id: K): Promise<void>;
  
  async exists(id: K): Promise<boolean> {
    const entity = await this.findById(id);
    return entity !== null;
  }
  
  async count(filters?: Record<string, any>): Promise<number> {
    const entities = await this.findAll(filters);
    return entities.length;
  }
}