export interface ProfileTextFields {
  fullName?: string;
  headline?: string;
  bio?: string;
  jobTitle?: string;
  company?: string;
  location?: string;
}

export class ProfileEnhancementService {
  enhance(fields: ProfileTextFields): ProfileTextFields {
    return {
      fullName: fields.fullName ? this.polishName(fields.fullName) : undefined,
      headline: fields.headline ? this.polishHeadline(fields.headline) : undefined,
      bio: fields.bio ? this.polishBio(fields.bio) : undefined,
      jobTitle: fields.jobTitle ? this.polishTitle(fields.jobTitle) : undefined,
      company: fields.company ? this.polishTitle(fields.company) : undefined,
      location: fields.location ? this.polishLocation(fields.location) : undefined,
    };
  }

  /** Preserve LinkedIn About text with minimal edits (bio = About). */
  polishLinkedInAbout(value: string): string {
    return value
      .replace(/\r\n/g, '\n')
      .replace(/\u00a0/g, ' ')
      .replace(/[ \t]+\n/g, '\n')
      .replace(/\n{3,}/g, '\n\n')
      .trim();
  }

  enhanceLinkedInImport(fields: ProfileTextFields): ProfileTextFields {
    const enhanced = this.enhance({
      ...fields,
      bio: undefined,
    });

    return {
      ...enhanced,
      bio: fields.bio ? this.polishLinkedInAbout(fields.bio) : undefined,
    };
  }

  buildDisplayHeadline(jobTitle?: string | null, company?: string | null, headline?: string | null): string | null {
    const polished = this.enhance({ jobTitle: jobTitle ?? undefined, company: company ?? undefined, headline: headline ?? undefined });
    if (polished.headline) return polished.headline;
    if (polished.jobTitle && polished.company) return `${polished.jobTitle} at ${polished.company}`;
    return polished.jobTitle ?? polished.company ?? null;
  }

  private polishName(value: string): string {
    return value
      .trim()
      .replace(/\s+/g, ' ')
      .split(' ')
      .map((part) => part.charAt(0).toUpperCase() + part.slice(1).toLowerCase())
      .join(' ');
  }

  private polishHeadline(value: string): string {
    let text = this.cleanText(value);
    text = text.replace(/#\w+/g, '').replace(/\s+/g, ' ').trim();
    if (text.length > 120) {
      const cut = text.slice(0, 117).replace(/\s+\S*$/, '');
      text = `${cut}…`;
    }
    return this.sentenceCase(text);
  }

  private polishBio(value: string): string {
    let text = value
      .replace(/\r\n/g, '\n')
      .replace(/[•·▪◦]/g, '\n')
      .split('\n')
      .map((line) => this.cleanText(line))
      .filter(Boolean);

    if (text.length > 1) {
      text = text.map((line) => this.sentenceCase(line));
    }

    let bio = text.join('\n\n');
    if (bio.length > 4000) {
      bio = `${bio.slice(0, 3997).replace(/\s+\S*$/, '')}…`;
    }
    return bio;
  }

  private polishTitle(value: string): string {
    const cleaned = this.cleanText(value);
    if (!cleaned) return cleaned;
    const lowerWords = new Set(['at', 'and', 'of', 'for', 'in', 'the']);
    return cleaned
      .split(' ')
      .map((word, index) => {
        const lower = word.toLowerCase();
        if (index > 0 && lowerWords.has(lower)) return lower;
        return word.charAt(0).toUpperCase() + word.slice(1);
      })
      .join(' ');
  }

  private polishLocation(value: string): string {
    return this.cleanText(value).replace(/\s*,\s*/g, ', ');
  }

  private cleanText(value: string): string {
    return value.trim().replace(/\s+/g, ' ');
  }

  private sentenceCase(value: string): string {
    if (!value) return value;
    return value.charAt(0).toUpperCase() + value.slice(1);
  }
}
