import { Injectable } from '@nestjs/common';
import { UserReferralRelationshipRepository } from '../../infrastructure/repositories/user-referral-relationship.repository';
import { UserReferralRelationship } from '../../domain/entities/user-referral-relationship.entity';

export interface CircleMember {
  relationshipId: string;
  referredUserId: string;
  level: 1 | 2;
  status: string;
  joinedAt: Date;
  activatedAt: Date | null;
}

export interface GetMyCircleResult {
  items: CircleMember[];
  total: number;
}

@Injectable()
export class GetMyCircleUseCase {
  constructor(private readonly relRepo: UserReferralRelationshipRepository) {}

  async execute(userId: string, limit = 20, offset = 0): Promise<GetMyCircleResult> {
    const { items, total } = await this.relRepo.findByReferrerUserId(userId, limit, offset);
    return {
      items: items.map((r: UserReferralRelationship) => ({
        relationshipId: r.id,
        referredUserId: r.referredUserId,
        level: r.level,
        status: r.status,
        joinedAt: r.createdAt,
        activatedAt: r.activatedAt,
      })),
      total,
    };
  }
}
