"""Company profile completion: what is filled in, what is missing, and where to fix it."""

SECTIONS = [
    ('info', 'Company Information', 'fa-building'),
    ('about', 'About', 'fa-pen-to-square'),
    ('locations', 'Locations', 'fa-location-dot'),
    ('careers', 'Careers', 'fa-user-tie'),
    ('brand', 'Employer Brand', 'fa-heart'),
    ('products', 'Products & Services', 'fa-boxes-stacked'),
    ('contact', 'Contact & Social', 'fa-address-book'),
    ('gallery', 'Gallery', 'fa-images'),
    ('verification', 'Verification', 'fa-shield-halved'),
    ('preview', 'Preview', 'fa-eye'),
]

# The seven steps an employer walks through, and which tabs belong to each.
STEPS = [
    (1, 'Tell us who your company is', ['info']),
    (2, 'Tell us what your company does', ['about']),
    (3, 'Tell us where you operate', ['locations']),
    (4, 'Tell professionals about your workplace and careers', ['careers', 'brand']),
    (5, 'Add your products, services and online presence', ['products', 'contact', 'gallery']),
    (6, 'Verify your company', ['verification']),
    (7, 'Preview your public company profile', ['preview']),
]


def completion_items(company):
    """Ordered checklist of (key, label, section, done)."""
    c = company
    return [
        ('logo', 'Logo', 'info', bool(c.display_logo_url)),
        ('industry', 'Industry and company type', 'info', bool(c.industry and c.company_type)),
        ('short', 'Short description', 'about', bool(c.short_description.strip())),
        ('about', 'About the company', 'about', bool((c.description or '').strip())),
        ('cover', 'Cover image', 'info', bool(c.display_cover_url)),
        ('products', 'Products & services', 'products', c.products.exists() or bool(c.specialization_list)),
        ('location', 'Head office location', 'locations', bool(c.country and c.city)),
        ('contact', 'Contact information', 'contact', bool(c.email)),
        ('careers', 'Careers', 'careers', bool(c.careers_overview.strip() or c.why_work_with_us.strip())),
        ('brand', 'Employer brand', 'brand', bool(c.culture.strip() or c.value_list or c.benefit_list)),
        ('gallery', 'Company gallery', 'gallery', c.photos.exists()),
        ('verification', 'Verification details', 'verification', bool(c.registered_name and c.registration_number)),
        ('rep', 'Company representative', 'verification', bool(c.rep_name and c.rep_position and c.rep_email)),
    ]


def completion(company):
    items = completion_items(company)
    done = sum(1 for *_, ok in items if ok)
    percent = round(100 * done / len(items)) if items else 0
    per_section = {}
    for _key, _label, section, ok in items:
        total, have = per_section.get(section, (0, 0))
        per_section[section] = (total + 1, have + (1 if ok else 0))
    return {
        'percent': percent,
        'items': [{'key': k, 'label': l, 'section': s, 'done': ok} for k, l, s, ok in items],
        'missing': [{'key': k, 'label': l, 'section': s} for k, l, s, ok in items if not ok],
        'done_count': done,
        'total': len(items),
        'section_done': {s: have == total for s, (total, have) in per_section.items()},
    }
