import { type PropertyType } from '@prisma/client';

export const DUPLICATE_DETECTOR = Symbol('DUPLICATE_DETECTOR');

/** A candidate that may be a duplicate of a newly created listing */
export interface DuplicateCandidate {
  listingId: string;
  propertyId: string;
  title: string;
  address: string;
  district: string;
  distanceMeters: number;
  /**
   * Address-level match signal. `true` when the candidate's
   * `addressNormalized` matches the input address exactly. Used to escalate
   * a soft warning to a hard (409) conflict.
   */
  addressMatch: boolean;
  /**
   * Agent-level match signal. `true` when the candidate was posted by the
   * same agent as the input listing. Only considered for hard duplicates.
   */
  sameAgent: boolean;
  propertyType: PropertyType;
}

export interface DuplicateCheckParams {
  /** Exclude this property from results (the one being created) */
  excludePropertyId?: string;
  latitude: number;
  longitude: number;
  /**
   * Normalized address produced by {@link Address#normalized}. When present
   * it is used for the hard-duplicate equality check; when absent only the
   * soft-warning (nearby) path is exercised.
   */
  addressNormalized?: string;
  /** Same-agent scope for hard-duplicate detection. */
  agentId?: string;
  propertyType: PropertyType;
  /** Max distance in meters to search for duplicates (default: 50) */
  radiusMeters?: number;
}

export interface IDuplicateDetector {
  /** Find existing listings that may be duplicates of the given property */
  findDuplicates(params: DuplicateCheckParams): Promise<DuplicateCandidate[]>;
}
