"""Plan benefit definitions - limits and flags applied when a subscription is active."""
from dataclasses import dataclass
from typing import Optional


@dataclass(frozen=True)
class SeekerPlanBenefits:
    tier: str
    is_featured: bool
    saved_jobs_limit: Optional[int]
    priority_job_alerts: bool
    full_application_history: bool
    profile_insights: bool
    shortlist_notifications: bool


CV_LIBRARY_PLAN_SLUGS = frozenset({
    'talent-cv-basic',
    'talent-cv-plus',
    'talent-cv-unlimited',
})


@dataclass(frozen=True)
class CompanyPlanBenefits:
    tier: str
    is_featured: bool
    job_posts_per_month: Optional[int]
    cv_profile_views_per_month: Optional[int]
    cv_downloads_per_month: Optional[int]
    can_download_cv: bool
    can_access_cv_library: bool
    can_message: bool
    auto_feature_jobs: bool
    can_access_ats: bool = False


DEFAULT_SEEKER_FREE = SeekerPlanBenefits(
    tier='free',
    is_featured=False,
    saved_jobs_limit=10,
    priority_job_alerts=False,
    full_application_history=False,
    profile_insights=False,
    shortlist_notifications=False,
)

SEEKER_PLAN_BENEFITS = {
    'seeker-free-professional': DEFAULT_SEEKER_FREE,
    'seeker-professional-plus': SeekerPlanBenefits(
        tier='gold',
        is_featured=True,
        saved_jobs_limit=None,
        priority_job_alerts=True,
        full_application_history=True,
        profile_insights=True,
        shortlist_notifications=True,
    ),
}

DEFAULT_COMPANY_NO_SUB = CompanyPlanBenefits(
    tier='free',
    is_featured=False,
    job_posts_per_month=0,
    cv_profile_views_per_month=0,
    cv_downloads_per_month=0,
    can_download_cv=False,
    can_access_cv_library=False,
    can_message=False,
    auto_feature_jobs=False,
)

COMPANY_PLAN_BENEFITS = {
    'company-starter': CompanyPlanBenefits(
        tier='starter',
        is_featured=False,
        job_posts_per_month=2,
        cv_profile_views_per_month=5,
        cv_downloads_per_month=0,
        can_download_cv=False,
        can_access_cv_library=False,
        can_message=False,
        auto_feature_jobs=False,
    ),
    'company-professional': CompanyPlanBenefits(
        tier='pro',
        is_featured=False,
        job_posts_per_month=5,
        cv_profile_views_per_month=20,
        cv_downloads_per_month=20,
        can_download_cv=True,
        can_access_cv_library=False,
        can_message=True,
        auto_feature_jobs=False,
        can_access_ats=True,
    ),
    'company-recruiter-pro': CompanyPlanBenefits(
        tier='premium',
        is_featured=True,
        job_posts_per_month=None,
        cv_profile_views_per_month=None,
        cv_downloads_per_month=100,
        can_download_cv=True,
        can_access_cv_library=False,
        can_message=True,
        auto_feature_jobs=False,
        can_access_ats=True,
    ),
    'company-enterprise': CompanyPlanBenefits(
        tier='enterprise',
        is_featured=True,
        job_posts_per_month=None,
        cv_profile_views_per_month=None,
        cv_downloads_per_month=None,
        can_download_cv=True,
        can_access_cv_library=False,
        can_message=True,
        auto_feature_jobs=True,
        can_access_ats=True,
    ),
    'talent-cv-basic': CompanyPlanBenefits(
        tier='pro',
        is_featured=False,
        job_posts_per_month=0,
        cv_profile_views_per_month=40,
        cv_downloads_per_month=40,
        can_download_cv=True,
        can_access_cv_library=True,
        can_message=True,
        auto_feature_jobs=False,
    ),
    'talent-cv-plus': CompanyPlanBenefits(
        tier='premium',
        is_featured=False,
        job_posts_per_month=0,
        cv_profile_views_per_month=100,
        cv_downloads_per_month=100,
        can_download_cv=True,
        can_access_cv_library=True,
        can_message=True,
        auto_feature_jobs=False,
    ),
    'talent-cv-unlimited': CompanyPlanBenefits(
        tier='enterprise',
        is_featured=False,
        job_posts_per_month=0,
        cv_profile_views_per_month=None,
        cv_downloads_per_month=None,
        can_download_cv=True,
        can_access_cv_library=True,
        can_message=True,
        auto_feature_jobs=False,
    ),
}


def get_seeker_plan_benefits(plan_slug):
    return SEEKER_PLAN_BENEFITS.get(plan_slug, DEFAULT_SEEKER_FREE)


def get_company_plan_benefits(plan_slug):
    return COMPANY_PLAN_BENEFITS.get(plan_slug, DEFAULT_COMPANY_NO_SUB)


def merge_company_benefits(*plan_slugs):
    """Combine recruitment + talent add-on limits when both exist."""
    merged = DEFAULT_COMPANY_NO_SUB
    for slug in plan_slugs:
        if not slug:
            continue
        plan = get_company_plan_benefits(slug)
        merged = CompanyPlanBenefits(
            tier=_higher_company_tier(merged.tier, plan.tier),
            is_featured=merged.is_featured or plan.is_featured,
            job_posts_per_month=_max_optional(merged.job_posts_per_month, plan.job_posts_per_month),
            cv_profile_views_per_month=_max_optional(
                merged.cv_profile_views_per_month,
                plan.cv_profile_views_per_month,
            ),
            cv_downloads_per_month=_max_optional(
                merged.cv_downloads_per_month,
                plan.cv_downloads_per_month,
            ),
            can_download_cv=merged.can_download_cv or plan.can_download_cv,
            can_access_cv_library=merged.can_access_cv_library or plan.can_access_cv_library,
            can_message=merged.can_message or plan.can_message,
            auto_feature_jobs=merged.auto_feature_jobs or plan.auto_feature_jobs,
            can_access_ats=merged.can_access_ats or plan.can_access_ats,
        )
    return merged


def plan_includes_cv_library(plan_slug):
    return plan_slug in CV_LIBRARY_PLAN_SLUGS


def _max_optional(left, right):
    if left is None or right is None:
        return None
    return max(left, right)


_TIER_RANK = {'free': 0, 'starter': 1, 'pro': 2, 'premium': 3, 'enterprise': 4}


def _higher_company_tier(left, right):
    return left if _TIER_RANK.get(left, 0) >= _TIER_RANK.get(right, 0) else right
