import { Company } from '@core/identity/domain/entities/Company';
import { CompanyRepository } from '@core/identity/domain/repositories/CompanyRepository';

/**
 * In-memory implementation of ICompanyRepository for testing
 */
export class InMemoryCompanyRepository implements CompanyRepository {
  private companies: Map<string, Company> = new Map();

  create(company: Pick<Company, 'getName' | 'getOwnerUserId' | 'getContactEmail'>): Company {
    // Create a new Company instance with generated ID
    const contactEmail = company.getContactEmail();
    return Company.create({
      name: company.getName(),
      ownerUserId: company.getOwnerUserId(),
      ...(contactEmail !== undefined ? { contactEmail } : {}),
    });
  }

  async save(company: Company): Promise<void> {
    this.companies.set(company.getId(), company);
  }

  async delete(id: string): Promise<void> {
    this.companies.delete(id);
  }

  async findById(id: string): Promise<Company | null> {
    return this.companies.get(id) || null;
  }

  async findByOwnerUserId(userId: string): Promise<Company | null> {
    for (const company of this.companies.values()) {
      if (company.getOwnerUserId().toString() === userId) {
        return company;
      }
    }
    return null;
  }

  // Helper method for testing to get all companies
  getAll(): Company[] {
    return Array.from(this.companies.values());
  }

  // Helper method for testing to clear all
  clear(): void {
    this.companies.clear();
  }
}