"""Professional profile completion for job seekers."""

SECTIONS = [
    ('basics', 'Photo & Basics', 'fa-id-card'),
    ('about', 'About Me', 'fa-pen-to-square'),
    ('experience', 'Experience', 'fa-briefcase'),
    ('education', 'Education & Certifications', 'fa-graduation-cap'),
    ('skills', 'Skills & Languages', 'fa-screwdriver-wrench'),
    ('preferences', 'Career Preferences', 'fa-bullseye'),
    ('cv', 'CV, Links & Portfolio', 'fa-file-lines'),
    ('privacy', 'Privacy & Contact', 'fa-user-shield'),
    ('preview', 'Preview', 'fa-eye'),
]


def completion_items(p):
    return [
        ('basics', 'Basic information', 'basics', bool(p.full_name and p.headline and p.location)),
        ('photo', 'Profile photo', 'basics', bool(p.display_avatar_url)),
        ('summary', 'Professional summary', 'about', len((p.bio or '').strip()) >= 50),
        ('industry', 'Industry', 'basics', bool(p.industry or p.category)),
        ('experience', 'Work experience', 'experience', p.experiences.exists() or bool((p.experience_summary or '').strip())),
        ('education', 'Education', 'education', p.education_items.exists() or bool((p.education or '').strip())),
        ('skills', 'Skills (at least 3)', 'skills', len(p.skill_list) >= 3),
        ('languages', 'Languages', 'skills', p.languages.exists()),
        ('certifications', 'Certifications', 'education', p.certifications.exists()),
        ('career', 'Career preferences', 'preferences', bool(p.target_title_list or p.preferred_industry_list)),
        ('employment', 'Employment preferences and availability', 'preferences',
         bool((p.employment_type_list or p.work_arrangement_list) and p.availability_timeline)),
        ('salary', 'Salary expectations', 'preferences', bool(p.salary_min or p.salary_negotiable or p.salary_private)),
        ('cv', 'CV', 'cv', bool(p.cv_file)),
        ('links', 'Professional links', 'cv', any([p.linkedin_url, p.website_url, p.portfolio_url, p.github_url, p.other_link_url])),
        ('portfolio', 'Projects or portfolio', 'cv', p.projects.exists()),
        ('references', 'References', 'privacy', p.references.exists()),
        ('accuracy', 'Confirm your profile is accurate', 'privacy', p.confirm_accuracy),
    ]


def completion(profile):
    items = completion_items(profile)
    done = sum(1 for *_, ok in items if ok)
    per = {}
    for _k, _l, section, ok in items:
        total, have = per.get(section, (0, 0))
        per[section] = (total + 1, have + (1 if ok else 0))
    return {
        'percent': round(100 * done / len(items)),
        '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.items()},
    }
