"""Deduplicate job text sections for display (legacy imports often repeat the same block)."""

import re

_WHITESPACE = re.compile(r'\s+')


def normalize_text(text):
    if not text:
        return ''
    return _WHITESPACE.sub(' ', str(text).strip()).lower()


def parse_lines(text):
    if not text:
        return []
    normalized = str(text).replace('\r\n', '\n').replace('\r', '\n')
    return [line.strip() for line in normalized.split('\n') if line.strip()]


def _dedupe_lines(lines, seen_lines):
    result = []
    for line in lines:
        key = normalize_text(line)
        if not key or key in seen_lines:
            continue
        seen_lines.add(key)
        result.append(line)
    return result


def build_job_display_content(job):
    """
    Return job body content without repeating the same text across sections.

    Legacy imports often copy description into responsibilities/requirements.
    """
    description = (job.description or '').strip()
    how_to_apply = (job.how_to_apply or '').strip()

    seen_blocks = set()
    seen_lines = set()
    description_key = normalize_text(description)

    if description:
        seen_blocks.add(normalize_text(description))
        for line in parse_lines(description):
            seen_lines.add(normalize_text(line))

    list_items = {}
    for field in ('responsibilities', 'requirements', 'nice_to_have', 'benefits'):
        raw = (getattr(job, field, '') or '').strip()
        block_key = normalize_text(raw)
        if block_key and block_key in seen_blocks:
            list_items[field] = []
            continue
        # Legacy imports also stored a truncated copy of the description: the section just repeats it.
        if block_key and description_key and block_key[:-1] in description_key:
            list_items[field] = []
            continue
        if block_key:
            seen_blocks.add(block_key)
        list_items[field] = _dedupe_lines(parse_lines(raw), seen_lines)

    how_key = normalize_text(how_to_apply)
    show_how_to_apply = bool(how_key and how_key not in seen_blocks)

    return {
        'description': description,
        'list_items': list_items,
        'how_to_apply': how_to_apply if show_how_to_apply else '',
        'show_how_to_apply': show_how_to_apply,
    }
